Skip to content

Commit

Permalink
Allow downloading individual files from Drive, not just directories
Browse files Browse the repository at this point in the history
  • Loading branch information
apdavison committed Jan 25, 2024
1 parent 357a741 commit 2456e02
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions api/simqueue/data_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,38 @@ def get_download_url(cls, drive_uri, user):
dir_obj = target_repository.get_dir(remote_path)
# todo: add option to overwrite files
except DoesNotExist:
errmsg = (
f"Tried to get download URL for {remote_path} in collab {collab_name} "
f"from {ebrains_drive_client.server} but this path does not exist. "
f"(Drive URI was {drive_uri})"
)
raise SourceFileDoesNotExist(errmsg)
# generate a random name but repeatable name for the temporary file
dir_obj = None
try:
file_obj = target_repository.get_file(remote_path)
except DoesNotExist:
errmsg = (
f"Tried to get download URL for {remote_path} in collab {collab_name} "
f"from {ebrains_drive_client.server} but this path does not exist. "
f"(Drive URI was {drive_uri})"
)
raise SourceFileDoesNotExist(errmsg)

# generate a random but repeatable name for the temporary file
try:
os.makedirs(settings.TMP_FILE_ROOT, exist_ok=True)
except PermissionError as err:
raise Exception(os.getcwd()) from err
zipfile_name = f"{uuid.uuid5(uuid.NAMESPACE_URL, drive_uri)}.zip"
if zipfile_name not in os.listdir(settings.TMP_FILE_ROOT):
# download zip of Drive directory contents
local_zipfile_path = os.path.join(settings.TMP_FILE_ROOT, zipfile_name)
_response = dir_obj.download(local_zipfile_path)
# todo: check the response

return f"{settings.TMP_FILE_URL}/{zipfile_name}"

if dir_obj:
tmp_file_name = f"{uuid.uuid5(uuid.NAMESPACE_URL, drive_uri)}.zip"
if tmp_file_name not in os.listdir(settings.TMP_FILE_ROOT):
# download zip of Drive directory contents
local_tmp_file_path = os.path.join(settings.TMP_FILE_ROOT, tmp_file_name)
_response = dir_obj.download(local_tmp_file_path)
# todo: check the response
else:
extension = remote_path.split(".")[-1]
tmp_file_name = f"{uuid.uuid5(uuid.NAMESPACE_URL, drive_uri)}.{extension}"
if tmp_file_name not in os.listdir(settings.TMP_FILE_ROOT):
local_tmp_file_path = os.path.join(settings.TMP_FILE_ROOT, tmp_file_name)
_response = file_obj.download(local_tmp_file_path)

return f"{settings.TMP_FILE_URL}/{tmp_file_name}"


class EBRAINSBucket:
Expand Down

0 comments on commit 2456e02

Please sign in to comment.