11import os
22import shutil
3- import imghdr
43import _thread
54import copy
65from typing import List
1211from fastapi import UploadFile ,HTTPException ,status
1312from .storage .googleCloud import upload_image_file_to_google_storage ,upload_video_file_to_google_storage
1413from .storage .s3 import upload_image_file_to_s3_storage ,upload_video_file_to_s3_storage
15- from ffmpeg import probe
14+ from .helpers .detectFileExtension import magic_extensions
15+ import sys
1616
17- def save_upload_file_tmp (upload_file : UploadFile ) -> Path :
17+ def save_upload_file_tmp (upload_file : None , raw_data_file = None ) -> Path :
1818 try :
19- suffix = Path (upload_file .filename ).suffix
20- with NamedTemporaryFile (delete = False , suffix = suffix ) as tmp :
21- shutil .copyfileobj (upload_file .file , tmp )
22- tmp_path = Path (tmp .name )
19+ if raw_data_file :
20+ with NamedTemporaryFile (delete = False , suffix = None ) as tmp :
21+ shutil .copyfileobj (raw_data_file .raw , tmp )
22+
23+ else :
24+ with NamedTemporaryFile (delete = False , suffix = None ) as tmp :
25+ shutil .copyfileobj (upload_file .file , tmp )
26+
27+ extension = magic_extensions (Path (tmp .name ))
28+ final_temp_file = tmp .name + extension
29+ os .rename (Path (tmp .name ), final_temp_file )
30+
2331 except :
2432 raise HTTPException (status_code = status .HTTP_503_SERVICE_UNAVAILABLE , detail = 'Impossible to manipulate the file' )
2533 finally :
26- upload_file .file .close ()
27- return tmp_path
34+ if upload_file :
35+ upload_file .file .close ()
36+ else :
37+ raw_data_file .close ()
38+ return Path (final_temp_file ), extension
2839
2940
30- def handle_upload_image_file (thumbnail , upload_file : UploadFile ):
41+ def handle_upload_image_file (thumbnail , upload_file : None , raw_data_file = None ):
3142 try :
32- tmp_path = save_upload_file_tmp (upload_file )
33- if imghdr .what (tmp_path ):
43+ tmp_path , file_extension = save_upload_file_tmp (upload_file , raw_data_file )
44+
45+ if file_extension [1 :] in os .environ .get ('IMAGE_AllOWED_FILE_FORMAT' ).split (',' ):
3446
35- imagePaths = resize_image (tmp_path , imghdr . what ( tmp_path ) , thumbnail , os .environ .get ('IMAGE_CONVERTING_PREFERED_FORMAT' ))
47+ imagePaths = resize_image (tmp_path , file_extension [ 1 :] , thumbnail , os .environ .get ('IMAGE_CONVERTING_PREFERED_FORMAT' ))
3648
3749 if os .environ .get ('PREFERED_STORAGE' ) == 'google' :
3850 _thread .start_new_thread (upload_image_file_to_google_storage , (copy .deepcopy (imagePaths ),))
@@ -56,11 +68,10 @@ def handle_upload_image_file(thumbnail, upload_file: UploadFile):
5668 tmp_path .unlink () # Delete the temp file
5769
5870
59- def handle_multiple_image_file_uploads (FILES : List [UploadFile ], workers : int ):
71+ def handle_multiple_image_file_uploads (FILES : List [UploadFile ], workers : int , thumbnail : bool ):
6072 # We can use a with statement to ensure threads are cleaned up promptly
6173 with concurrent .futures .ThreadPoolExecutor (max_workers = workers ) as executor :
62- # Start the load operations and mark each future with its FILES thumbnail is default for this function
63- future_to_url = {executor .submit (handle_upload_image_file , True , eachFile ): eachFile for eachFile in FILES }
74+ future_to_url = {executor .submit (handle_upload_image_file , eachFile , thumbnail ): eachFile for eachFile in FILES }
6475 result = []
6576 for future in concurrent .futures .as_completed (future_to_url ):
6677 try :
@@ -70,15 +81,12 @@ def handle_multiple_image_file_uploads(FILES: List[UploadFile], workers: int):
7081 return result
7182
7283
73- def handle_upload_video_file (optimize , upload_file : UploadFile ):
84+ def handle_upload_video_file (optimize , upload_file : None , raw_data_file = None ):
85+
7486 try :
75- tmp_path = save_upload_file_tmp (upload_file )
76- videoFileCheck = probe (tmp_path ).get ('format' )
77- if videoFileCheck .get ('format_name' ):
78- # Checks for video file type also is possible to restict for only mp4 format by checking major_brand
79- # videoFileCheck.get('tags').get('major_brand') == 'mp42'
80- if os .environ .get ('VIDEO_AllOWED_FILE_FORMAT' ) in videoFileCheck .get ('format_name' ).split (',' ):
87+ tmp_path , file_extension = save_upload_file_tmp (upload_file , raw_data_file )
8188
89+ if file_extension [1 :] in os .environ .get ('VIDEO_AllOWED_FILE_FORMAT' ).split (',' ):
8290 videoPaths = video_file_FFMPEG (tmp_path , optimize )
8391
8492 if os .environ .get ('PREFERED_STORAGE' ) == 'google' :
@@ -100,8 +108,6 @@ def handle_upload_video_file(optimize, upload_file: UploadFile):
100108 videoPaths ['storage' ] = os .environ .get ('PREFERED_STORAGE' )
101109
102110 return videoPaths
103- else :
104- raise HTTPException (status_code = status .HTTP_503_SERVICE_UNAVAILABLE , detail = 'Format not granted' )
105111 else :
106112 raise HTTPException (status_code = status .HTTP_503_SERVICE_UNAVAILABLE , detail = 'Not valid format' )
107113 except :
0 commit comments