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)

View file

@ -0,0 +1,39 @@
from PIL import Image
import tempfile
import os, uuid
class ProfileImageProcessor:
@staticmethod
def get_parsed_image_data(image_path):
Image.MAX_IMAGE_PIXELS = 933120000
image = Image.open(image_path)
try:
image.seek(1)
except EOFError:
mime_type = image.get_format_mimetype()
return image_path, mime_type
else:
newUUID = str(uuid.uuid4())
new_image_path = os.path.join(tempfile.gettempdir(), newUUID) + ".webp"
image.save(new_image_path)
new_image = Image.open(new_image_path)
mime_type = new_image.get_format_mimetype()
return new_image_path, mime_type
@staticmethod
def process_image(image_path):
return ProfileImageProcessor.get_parsed_image_data(image_path)
if __name__ == "__main__":
result = ProfileImageProcessor.get_parsed_image_data("D:\Imagens\807b5c4b02e765bb4930b7c66662ef4b.gif")
print(result)
result = ProfileImageProcessor.get_parsed_image_data("D:/Imagens/20240416_233352~2.png")
print(result)