feat: process profile image

This commit is contained in:
Zamitto 2024-09-13 16:33:27 -03:00
parent a295003ad4
commit 797a09f583
12 changed files with 207 additions and 104 deletions

View file

@ -4,6 +4,7 @@ import json
import urllib.parse
import psutil
from torrent_downloader import TorrentDownloader
from profile_image_processor import ProfileImageProcessor
torrent_port = sys.argv[1]
http_port = sys.argv[2]
@ -73,16 +74,30 @@ class Handler(BaseHTTPRequestHandler):
def do_POST(self):
global torrent_downloader
if self.path == "/action":
if self.headers.get(self.rpc_password_header) != rpc_password:
self.send_response(401)
if self.headers.get(self.rpc_password_header) != rpc_password:
self.send_response(401)
self.end_headers()
return
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data.decode('utf-8'))
if self.path == "/profile-image":
parsed_image_path = data['image_path']
try:
parsed_image_path, mime_type = ProfileImageProcessor.process_image(parsed_image_path)
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({'imagePath': parsed_image_path, 'mimeType': mime_type}).encode('utf-8'))
except:
self.send_response(400)
self.end_headers()
return
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data.decode('utf-8'))
elif self.path == "/action":
if torrent_downloader is None:
torrent_downloader = TorrentDownloader(torrent_port)
@ -99,6 +114,10 @@ class Handler(BaseHTTPRequestHandler):
self.send_response(200)
self.end_headers()
else:
self.send_response(404)
self.end_headers()
if __name__ == "__main__":
httpd = HTTPServer(("", int(http_port)), Handler)