Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2718 from zafrs/feature/dropbox_bigfile
Browse files Browse the repository at this point in the history
dropbox handle big file
  • Loading branch information
kalisp authored Feb 14, 2022
2 parents 666d161 + 97c8e17 commit b4b5ab7
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,34 @@ def upload_file(self, source_path, path,
mode = dropbox.files.WriteMode.overwrite

with open(source_path, "rb") as f:
self.dbx.files_upload(f.read(), path, mode=mode)
file_size = os.path.getsize(source_path)

CHUNK_SIZE = 50 * 1024 * 1024

if file_size <= CHUNK_SIZE:
self.dbx.files_upload(f.read(), path, mode=mode)
else:
upload_session_start_result = \
self.dbx.files_upload_session_start(f.read(CHUNK_SIZE))

cursor = dropbox.files.UploadSessionCursor(
session_id=upload_session_start_result.session_id,
offset=f.tell())

commit = dropbox.files.CommitInfo(path=path, mode=mode)

while f.tell() < file_size:
if (file_size - f.tell()) <= CHUNK_SIZE:
self.dbx.files_upload_session_finish(
f.read(CHUNK_SIZE),
cursor,
commit)
else:
self.dbx.files_upload_session_append(
f.read(CHUNK_SIZE),
cursor.session_id,
cursor.offset)
cursor.offset = f.tell()

server.update_db(
collection=collection,
Expand Down

0 comments on commit b4b5ab7

Please sign in to comment.