utkarsh134 commited on
Commit
484e55b
·
verified ·
1 Parent(s): 5c9b137

Upload ico_convert.py

Browse files
Files changed (1) hide show
  1. app/services/ico_convert.py +47 -0
app/services/ico_convert.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import logging
3
+ from typing import Dict
4
+ from PIL import Image
5
+ from fastapi import UploadFile
6
+
7
+ from app.utils.reach_upload import upload_to_reach
8
+
9
+ # Configure logging
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
+
13
+ async def convert_to_ico(file: UploadFile) -> Dict:
14
+ """
15
+ Convert an image to ICO format and upload it to Reach platform.
16
+
17
+ Args:
18
+ file: UploadFile object containing the image
19
+
20
+ Returns:
21
+ Dict containing the upload result with URL
22
+ """
23
+ try:
24
+ # Read file content
25
+ image_data = await file.read()
26
+
27
+ # Convert image bytes to PIL Image
28
+ image = Image.open(io.BytesIO(image_data))
29
+
30
+ # Create ICO version (32x32)
31
+ ico_buffer = io.BytesIO()
32
+ ico_image = image.resize((32, 32), Image.Resampling.LANCZOS)
33
+ ico_image.save(ico_buffer, format='ICO', sizes=[(32, 32)])
34
+ ico_buffer.seek(0)
35
+
36
+ # Upload ICO to Reach platform
37
+ result = upload_to_reach(ico_buffer.getvalue(), file.filename, 'ico', 1)
38
+
39
+ logger.info(f"Successfully converted and uploaded ICO for {file.filename}")
40
+
41
+ return {
42
+ 'url': result['url']
43
+ }
44
+
45
+ except Exception as e:
46
+ logger.error(f"Error converting image to ICO: {str(e)}", exc_info=True)
47
+ raise Exception(f"Failed to convert image to ICO: {str(e)}")