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 ),))
@@ -49,18 +61,18 @@ def handle_upload_image_file(thumbnail, upload_file: UploadFile):
4961 imagePaths ['thumbnail' ] = os .environ .get ('API_URL' ) + os .environ .get ('IMAGE_THUMBNAIL_LOCAL_PATH' ) + imagePaths ['thumbnail' ] if imagePaths .get ('thumbnail' ) else None
5062
5163 imagePaths ['storage' ] = os .environ .get ('PREFERED_STORAGE' )
64+ imagePaths ['file_name' ] = imagePaths ['original' ].split ('/' )[- 1 ]
5265 return imagePaths
5366 else :
5467 raise HTTPException (status_code = status .HTTP_503_SERVICE_UNAVAILABLE , detail = 'The file format not supported' )
5568 finally :
5669 tmp_path .unlink () # Delete the temp file
5770
5871
59- def handle_multiple_image_file_uploads (FILES : List [UploadFile ], workers : int ):
72+ def handle_multiple_image_file_uploads (FILES : List [UploadFile ], workers : int , thumbnail : bool ):
6073 # We can use a with statement to ensure threads are cleaned up promptly
6174 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 }
75+ future_to_url = {executor .submit (handle_upload_image_file , eachFile , thumbnail ): eachFile for eachFile in FILES }
6476 result = []
6577 for future in concurrent .futures .as_completed (future_to_url ):
6678 try :
@@ -70,15 +82,12 @@ def handle_multiple_image_file_uploads(FILES: List[UploadFile], workers: int):
7082 return result
7183
7284
73- def handle_upload_video_file (optimize , upload_file : UploadFile ):
85+ def handle_upload_video_file (optimize , upload_file : None , raw_data_file = None ):
86+
7487 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 (',' ):
88+ tmp_path , file_extension = save_upload_file_tmp (upload_file , raw_data_file )
8189
90+ if file_extension [1 :] in os .environ .get ('VIDEO_AllOWED_FILE_FORMAT' ).split (',' ):
8291 videoPaths = video_file_FFMPEG (tmp_path , optimize )
8392
8493 if os .environ .get ('PREFERED_STORAGE' ) == 'google' :
@@ -98,10 +107,9 @@ def handle_upload_video_file(optimize, upload_file: UploadFile):
98107 videoPaths ['optimized' ] = os .environ .get ('API_URL' ) + os .environ .get ('VIDEO_OPTIMIZED_LOCAL_PATH' ) + videoPaths ['optimized' ] if videoPaths .get ('optimized' ) else None
99108
100109 videoPaths ['storage' ] = os .environ .get ('PREFERED_STORAGE' )
110+ videoPaths ['file_name' ] = videoPaths ['original' ].split ('/' )[- 1 ]
101111
102112 return videoPaths
103- else :
104- raise HTTPException (status_code = status .HTTP_503_SERVICE_UNAVAILABLE , detail = 'Format not granted' )
105113 else :
106114 raise HTTPException (status_code = status .HTTP_503_SERVICE_UNAVAILABLE , detail = 'Not valid format' )
107115 except :
0 commit comments