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

Update for dropbox api v2 #346

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
36 changes: 27 additions & 9 deletions storages/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.utils.deconstruct import deconstructible
from dropbox import Dropbox
from dropbox.exceptions import ApiError
from dropbox.files import FileMetadata, FolderMetadata, UploadSessionCursor, CommitInfo

from storages.utils import setting

Expand Down Expand Up @@ -75,14 +76,15 @@ def exists(self, name):
def listdir(self, path):
directories, files = [], []
full_path = self._full_path(path)
metadata = self.client.files_get_metadata(full_path)
for entry in metadata['contents']:
entry['path'] = entry['path'].replace(full_path, '', 1)
entry['path'] = entry['path'].replace('/', '', 1)
if entry['is_dir']:
directories.append(entry['path'])
else:
files.append(entry['path'])
# Specify the root folder as an empty string rather than as "/"
if full_path == '/':
full_path = ''
entries = self.client.files_list_folder(full_path).entries
for entry in entries:
if isinstance(entry, FileMetadata):
files.append(entry.name)
if isinstance(entry, FolderMetadata):
directories.append(entry.name)
return directories, files

def size(self, name):
Expand All @@ -108,5 +110,21 @@ def _open(self, name, mode='rb'):
return remote_file

def _save(self, name, content):
self.client.files_upload(content, self._full_path(name))
# files_upload
CHUNK_SIZE = 20 * 1024 * 1024
content.open()
f = content.file
if content.size < CHUNK_SIZE:
self.client.files_upload(f.read(), self._full_path(name))
else:
upload_session_start_result = self.client.files_upload_session_start(f.read(CHUNK_SIZE))
cursor = UploadSessionCursor(session_id=upload_session_start_result.session_id, offset=f.tell())
commit = CommitInfo(path=self._full_path(name))
while f.tell() < content.size:
if ((content.size - f.tell()) <= CHUNK_SIZE):
self.client.files_upload_session_finish(f.read(CHUNK_SIZE), cursor, commit)
else:
self.client.files_upload_session_append(f.read(CHUNK_SIZE), cursor.session_id, cursor.offset)
cursor.offset = f.tell()
content.close()
return name
2 changes: 1 addition & 1 deletion tests/test_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_not_exists(self, *args):
exists = self.storage.exists('bar')
self.assertFalse(exists)

@mock.patch('dropbox.Dropbox.files_get_metadata',
@mock.patch('dropbox.Dropbox.files_list_folder',
return_value=FILES_FIXTURE)
def test_listdir(self, *args):
dirs, files = self.storage.listdir('/')
Expand Down