Refactor get_asset_file_or_stream

pull/1496/head
Piero Toffanin 2024-05-11 15:01:26 -04:00
rodzic 35fc60aa2c
commit 8468fdff5c
3 zmienionych plików z 11 dodań i 10 usunięć

Wyświetl plik

@ -365,19 +365,20 @@ class TaskDownloads(TaskNestedView):
# Check and download # Check and download
try: try:
asset_fs, is_zipstream = task.get_asset_file_or_zipstream(asset) asset_fs = task.get_asset_file_or_stream(asset)
except FileNotFoundError: except FileNotFoundError:
raise exceptions.NotFound(_("Asset does not exist")) raise exceptions.NotFound(_("Asset does not exist"))
if not is_zipstream and not os.path.isfile(asset_fs): is_stream = not isinstance(asset_fs, str)
if not is_stream and not os.path.isfile(asset_fs):
raise exceptions.NotFound(_("Asset does not exist")) raise exceptions.NotFound(_("Asset does not exist"))
download_filename = request.GET.get('filename', get_asset_download_filename(task, asset)) download_filename = request.GET.get('filename', get_asset_download_filename(task, asset))
if not is_zipstream: if is_stream:
return download_file_response(request, asset_fs, 'attachment', download_filename=download_filename)
else:
return download_file_stream(request, asset_fs, 'attachment', download_filename=download_filename) return download_file_stream(request, asset_fs, 'attachment', download_filename=download_filename)
else:
return download_file_response(request, asset_fs, 'attachment', download_filename=download_filename)
""" """
Raw access to the task's asset folder resources Raw access to the task's asset folder resources

Wyświetl plik

@ -450,16 +450,16 @@ class Task(models.Model):
return False return False
def get_asset_file_or_zipstream(self, asset): def get_asset_file_or_stream(self, asset):
""" """
Get a stream to an asset Get a stream to an asset
:param asset: one of ASSETS_MAP keys :param asset: one of ASSETS_MAP keys
:return: (path|stream, is_zipstream:bool) :return: (path|stream)
""" """
if asset in self.ASSETS_MAP: if asset in self.ASSETS_MAP:
value = self.ASSETS_MAP[asset] value = self.ASSETS_MAP[asset]
if isinstance(value, str): if isinstance(value, str):
return self.assets_path(value), False return self.assets_path(value)
elif isinstance(value, dict): elif isinstance(value, dict):
if 'deferred_path' in value and 'deferred_compress_dir' in value: if 'deferred_path' in value and 'deferred_compress_dir' in value:
@ -469,7 +469,7 @@ class Task(models.Model):
paths = [p for p in paths if os.path.basename(p['fs']) not in value['deferred_exclude_files']] paths = [p for p in paths if os.path.basename(p['fs']) not in value['deferred_exclude_files']]
if len(paths) == 0: if len(paths) == 0:
raise FileNotFoundError("No files available for download") raise FileNotFoundError("No files available for download")
return zipfly.ZipStream(paths), True return zipfly.ZipStream(paths)
else: else:
raise FileNotFoundError("{} is not a valid asset (invalid dict values)".format(asset)) raise FileNotFoundError("{} is not a valid asset (invalid dict values)".format(asset))
else: else:

Wyświetl plik

@ -343,7 +343,7 @@ class ShareTaskView(TaskView):
settings = get_settings(request) settings = get_settings(request)
available_assets = [task.get_asset_file_or_zipstream(f)[0] for f in list(set(task.available_assets) & set(DRONEDB_ASSETS))] available_assets = [task.get_asset_file_or_stream(f) for f in list(set(task.available_assets) & set(DRONEDB_ASSETS))]
if 'textured_model.zip' in task.available_assets: if 'textured_model.zip' in task.available_assets:
texture_files = [join(task.assets_path('odm_texturing'), f) for f in listdir(task.assets_path('odm_texturing')) if isfile(join(task.assets_path('odm_texturing'), f))] texture_files = [join(task.assets_path('odm_texturing'), f) for f in listdir(task.assets_path('odm_texturing')) if isfile(join(task.assets_path('odm_texturing'), f))]