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

Write mode option for Dropbox #873

Merged
merged 12 commits into from
Apr 18, 2020
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ By order of apparition, thanks:
* Shaung Cheng (S3 docs)
* Andrew Perry (Bug fixes in SFTPStorage)
* Manuel Kaufmann (humitos)
* Taras Petriichuk (Dropbox autorename option)


Extra thanks to Marty for adding this in Django,
Expand Down
15 changes: 11 additions & 4 deletions storages/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
from django.utils.deconstruct import deconstructible
from dropbox import Dropbox
from dropbox.exceptions import ApiError
from dropbox.files import CommitInfo, FolderMetadata, UploadSessionCursor
from dropbox.files import CommitInfo, FolderMetadata, UploadSessionCursor, WriteMode

from storages.utils import setting

_DEFAULT_TIMEOUT = 100
_DEFAULT_OVERWRITE = False


class DropBoxStorageException(Exception):
Expand Down Expand Up @@ -69,15 +70,18 @@ class DropBoxStorage(Storage):
location = setting('DROPBOX_ROOT_PATH', '/')
oauth2_access_token = setting('DROPBOX_OAUTH2_TOKEN')
timeout = setting('DROPBOX_TIMEOUT', _DEFAULT_TIMEOUT)
overwrite = setting('DROPBOX_OVERWRITE', _DEFAULT_OVERWRITE)

CHUNK_SIZE = 4 * 1024 * 1024

def __init__(self, oauth2_access_token=oauth2_access_token, root_path=location, timeout=timeout):
def __init__(self, oauth2_access_token=oauth2_access_token, root_path=location, timeout=timeout,
overwrite=overwrite):
if oauth2_access_token is None:
raise ImproperlyConfigured("You must configure an auth token at"
"'settings.DROPBOX_OAUTH2_TOKEN'.")

self.root_path = root_path
self.overwrite = overwrite
self.client = Dropbox(oauth2_access_token, timeout=timeout)

def _full_path(self, name):
Expand All @@ -89,6 +93,9 @@ def delete(self, name):
self.client.files_delete(self._full_path(name))

def exists(self, name):
if self.overwrite:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so how then to make Django to not rename? any suggestions?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the other backends. They override get_available_name.

Copy link
Contributor Author

@petriichuk petriichuk Apr 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jschneier updated

return False

try:
return bool(self.client.files_get_metadata(self._full_path(name)))
except ApiError:
Expand Down Expand Up @@ -132,7 +139,7 @@ def _open(self, name, mode='rb'):
def _save(self, name, content):
content.open()
if content.size <= self.CHUNK_SIZE:
self.client.files_upload(content.read(), self._full_path(name))
self.client.files_upload(content.read(), self._full_path(name), mode=WriteMode('overwrite'))
Copy link
Owner