diff --git a/CHANGELOG.md b/CHANGELOG.md index 987a851..8aa7c3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/dropbox/file.py b/src/dropbox/file.py index 15554f8..87863e8 100644 --- a/src/dropbox/file.py +++ b/src/dropbox/file.py @@ -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 """ @@ -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: @@ -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