Skip to content

Commit

Permalink
chore: support for file like objects in upload
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Sep 24, 2024
1 parent 9b30d1e commit 75c9363
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

*
* Support for file-like objects in the `upload_large_file()` method

### Changed

Expand Down
17 changes: 14 additions & 3 deletions src/dropbox/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import os
import json

import appier

CHUNK_SIZE = 64 * 1024 * 1024
""" The default size of the chunk of the chunk
that is going to be used for file upload """
Expand Down Expand Up @@ -156,8 +158,15 @@ def upload_large_file(self, path, target, chunk_size=CHUNK_SIZE):
offset = 0
contents = self.session_start_file()
session_id = contents["session_id"]
file_size = os.path.getsize(path)
file = open(path, "rb")
is_path = appier.legacy.is_string(path)
if is_path:
file = open(path, "rb")
else:
file = path
position = file.tell()
file.seek(0, os.SEEK_END)
file_size = file.tell()
file.seek(0)
try:
while True:
if offset == file_size:
Expand All @@ -168,6 +177,8 @@ def upload_large_file(self, path, target, chunk_size=CHUNK_SIZE):
)
offset += amount
finally:
file.close()
file.seek(position)
if is_path:
file.close()
contents = self.session_finish_file(session_id, offset=offset, path=target)
return contents

0 comments on commit 75c9363

Please sign in to comment.