def extract(self, path: Path, clean: bool = True): """ Extract the archive content. Parameters ---------- path : Path A non-existing directory path where the archive content is extracted clean : bool (default: True) Whether the archive content has to be cleaned from Mac OS hidden files (.DS_STORE, __MACOSX). """ if path.exists() and not path.is_dir(): raise ArchiveError( f"{self} cannot be extracted in {path} because " f"it already exists or it is not a directory" ) try: shutil.unpack_archive(self.absolute(), path, self._format.name) except shutil.ReadError as e: raise ArchiveError(str(e)) if clean: bad_filenames = ['.DS_STORE', '__MACOSX'] for bad_filename in bad_filenames: for bad_path in path.rglob(bad_filename): bad_path.unlink(missing_ok=True)
def mksymlink(self, path: Path, target: Path): """Make a symlink from path to target (with notifications)""" try: path.symlink_to( target, target_is_directory=target.is_dir() ) except (FileNotFoundError, FileExistsError, OSError) as e: self.notify(ImportEventType.FILE_ERROR, path, exception=e) raise FileErrorProblem(path)
def export_file(path: Path = Depends(filepath_parameter)): """ Export a file. All files in the server base path can be exported. """ if path.is_dir(): # TODO: zip and return the folder archive ? raise NotAFileProblem(path) return FileResponse(path, media_type="application/octet-stream", filename=path.name)