Skip to content

Commit

Permalink
fix(uploads): make sure files are always opened in binary format
Browse files Browse the repository at this point in the history
  • Loading branch information
imathews committed May 21, 2024
1 parent 85a1126 commit 0491ea0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/redivis/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.14.8"
__version__ = "0.14.9"
1 change: 0 additions & 1 deletion src/redivis/classes/Notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@ def create_output_table(self, data=None, *, name=None, append=False, geography_v
payload={"name": name, "append": append, "geographyVariables": geography_variables, "tempUploadId": temp_upload["id"]},
)


return Table(name=res["name"], properties=res)
8 changes: 8 additions & 0 deletions src/redivis/classes/Upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ def create(
(hasattr(data, "read") and os.stat(data.name).st_size > 1e7)
or (hasattr(data, "__len__") and len(data) > 1e7)
):
did_reopen_file = False
pbar_bytes = None
# If file isn't in binary mode, we need to reopen, otherwise uploading of non-text files will fail
if hasattr(data, 'mode') and 'b' not in data.mode:
data = open(data.name, 'rb')
did_reopen_file = True

size = os.stat(data.name).st_size if hasattr(data, "read") else len(data)
if progress:
pbar_bytes = tqdm(total=size, unit='B', leave=False, unit_scale=True)
Expand All @@ -77,6 +83,8 @@ def create(
perform_standard_upload(data=data, temp_upload_url=temp_upload["url"],
progressbar=pbar_bytes)

if did_reopen_file:
data.close()
if progress:
pbar_bytes.close()

Expand Down
8 changes: 0 additions & 8 deletions src/redivis/common/retryable_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
def perform_resumable_upload(data, temp_upload_url=None, progressbar=None):
retry_count = 0
start_byte = 0
did_reopen_file = False
is_file = True if hasattr(data, "read") else False
file_size = os.stat(data.name).st_size if is_file else len(data)
chunk_size = file_size

if is_file and hasattr(data, 'mode') and 'b' not in data.mode:
data = open(data.name, 'rb')
did_reopen_file = True

resumable_url = initiate_resumable_upload(file_size, temp_upload_url)

while start_byte < file_size or start_byte == 0: # handle empty upload for start_byte == 0
Expand Down Expand Up @@ -60,9 +55,6 @@ def perform_resumable_upload(data, temp_upload_url=None, progressbar=None):
file_size=file_size, resumable_url=resumable_url
)

if did_reopen_file:
data.close()


def initiate_resumable_upload(size, temp_upload_url, retry_count=0):
did_request_complete = False
Expand Down

0 comments on commit 0491ea0

Please sign in to comment.