@@ -285,7 +285,6 @@ class Unpacker(object):
285285 """
286286
287287 ERROR_FILE_SUFFIX = ".unpack_error.log"
288- PROGRESS_PREFIX = PROGRESS_PREFIX
289288
290289 def __init__ (
291290 self ,
@@ -399,13 +398,10 @@ def extract_archive(
399398 if top_level :
400399 self ._status_entries_done (len (filenames ), len (filenames ))
401400
402- def _is_embedded_archive (self , filename : str , filepath : Path ) -> bool :
403- return self ._is_nested_archive (filename , filepath )
404-
405401 def _is_accepted_extracted_file (self , filename : str , filepath : Path ) -> bool :
406402 if filepath .is_dir ():
407403 return True
408- if self ._is_embedded_archive (filename , filepath ):
404+ if self ._is_nested_archive (filename , filepath ):
409405 return True
410406 file_ext , _ , mime_type = self .get_file_info (filepath .name , filepath .parent )
411407 if file_ext in TEXT_EXTENSIONS :
@@ -450,25 +446,11 @@ def get_file_info(filename: str, path: Path) -> Tuple[str, Path, Optional[str]]:
450446
451447 return file_ext , filepath , mime_type
452448
453- def _has_accepted_format (self , path : Path , filepath : str ) -> bool :
454- # Same extension fallback as for archive members
455- if filepath .split ("." )[- 1 ].lower () in TEXT_EXTENSIONS :
449+ def _has_accepted_format (self , filepath : Union [Path , str ]) -> bool :
450+ p = Path (filepath )
451+ file_ext , _ , mime_type = self .get_file_info (p .name , p .parent )
452+ if file_ext in TEXT_EXTENSIONS or mime_type in self .accepted_mime_types :
456453 return True
457- try :
458- py_magic = from_path (filepath )
459- mime_type = py_magic .mime_type ()
460- except (CantMatchTypeError , Exception ):
461- mime_type = None
462- if mime_type in self .accepted_mime_types :
463- return True
464- logger .info (
465- "File format not accepted '%s' '%s' , user_id '%s'" ,
466- str (path ),
467- str (filepath ),
468- str (self .user_id ),
469- )
470- path_error = str (path .joinpath (filepath .lstrip (os .path .sep )))
471- self .list_errors .update ({"Not accepted" : path_error })
472454 return False
473455
474456 def unpack_zip (self , input_path : Path , path : Path ) -> None :
@@ -478,7 +460,7 @@ def unpack_zip(self, input_path: Path, path: Path) -> None:
478460 "r" ,
479461 allowZip64 = True ,
480462 filter_func = self ._is_accepted_extracted_file ,
481- is_embedded_archive = self ._is_embedded_archive ,
463+ is_embedded_archive = self ._is_nested_archive ,
482464 ) as archive :
483465 self .extract_archive (archive , str (input_path ), path )
484466 except Exception as e :
@@ -492,7 +474,7 @@ def unpack_tar(self, input_path: Path, path: Path) -> None:
492474 input_path ,
493475 "r" ,
494476 filter_func = self ._is_accepted_extracted_file ,
495- is_embedded_archive = self ._is_embedded_archive ,
477+ is_embedded_archive = self ._is_nested_archive ,
496478 ) as archive :
497479 self .extract_archive (archive , str (input_path ), path )
498480 except Exception as e :
@@ -804,8 +786,14 @@ def _dispatch_nested(self, compressed_path: Path, to_path: Path) -> None:
804786 elif self ._is_gz (compressed_path .as_posix ()):
805787 with self ._one_level_deeper ():
806788 self .unpack_gz (compressed_path , to_path )
807- elif not self ._has_accepted_format (to_path , str ( compressed_path ) ):
789+ elif not self ._has_accepted_format (compressed_path ):
808790 os .remove (compressed_path )
791+ logger .info (
792+ "File format not accepted '%s' '%s' , user_id '%s'" ,
793+ str (to_path ),
794+ str (compressed_path ),
795+ str (self .user_id ),
796+ )
809797 self .list_errors .update ({"Not accepted" : str (compressed_path )})
810798 logger .info (f"Unpack done for { compressed_path } ->{ to_path } " )
811799
0 commit comments