Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor file retrieval methods to fix zip handling #85

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions hsclient/hydroshare.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,19 +1359,38 @@ def retrieve_string(self, path):

def retrieve_file(self, path, save_path=""):
file = self.get(path, status_code=200, allow_redirects=True)
filename = path.split("/")[-1]
downloaded_file = os.path.join(save_path, filename)
with open(downloaded_file, 'wb') as f:
f.write(file.content)
return downloaded_file
self.write_file(path, file.content, save_path)

def retrieve_bag(self, path, save_path=""):
file = self.get(path, status_code=200, allow_redirects=True)

if file.headers['Content-Type'] != "application/zip":
print(f"Retrieving {path}")
response = self.get(path, status_code=200, allow_redirects=True)

file_is_ready = False
content_type = response.headers['Content-Type']

if content_type == "application/zip":
# if the path doesn't end with .zip, add it
if not path.endswith(".zip"):
path += ".zip"
file_is_ready = True
if content_type == "binary/octet-stream":
# here we assume that the stream is a zip file
if not path.endswith(".zip"):
path += ".zip"
file_is_ready = True

if not file_is_ready:
time.sleep(CHECK_TASK_PING_INTERVAL)
return self.retrieve_bag(path, save_path)
return self.retrieve_file(path, save_path)

self.write_file(path, response.content, save_path)

def write_file(self, path, content, save_path=""):
filename = path.split("/")[-1]
downloaded_file = os.path.join(save_path, filename)
with open(downloaded_file, 'wb') as f:
f.write(content)
return downloaded_file

def check_task(self, task_id):
response = self.get(f"/hsapi/taskstatus/{task_id}/", status_code=200)
Expand Down
Loading