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

[dropbox] fix name mangling by replacing lstrip with removeprefix #1279

Merged
merged 1 commit into from
Aug 26, 2023
Merged
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
8 changes: 7 additions & 1 deletion storages/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class DropboxStorageException(Exception):
DropBoxStorageException = DropboxStorageException


def removeprefix(prefix, name):
if name.startswith(prefix):
name = name[len(prefix):]
return name


class DropboxFile(File):
def __init__(self, name, storage):
self.name = name
Expand Down Expand Up @@ -172,7 +178,7 @@ def _save(self, name, content):
content.close()
# .save() validates the filename isn't absolute but Dropbox requires an absolute filename.
# Work with the absolute name internally but strip it off before passing up-the-chain.
return name.lstrip(self.root_path)
return removeprefix(self.root_path, name).lstrip('/')

def _chunked_upload(self, content, dest_path):
upload_session = self.client.files_upload_session_start(
Expand Down
8 changes: 5 additions & 3 deletions tests/test_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ def test_jailed(self, *args):
@mock.patch('dropbox.Dropbox.files_upload', return_value='foo')
@mock.patch('dropbox.Dropbox.files_get_metadata', return_value=None)
def test_saves(self, *args):
self.storage = dropbox.DropboxStorage('foo', root_path='/bar')
name = self.storage.save('xyz', File(io.BytesIO(b'abc'), 'def'))
self.assertEqual(name, 'xyz')
self.storage = dropbox.DropboxStorage('foo', root_path='/app.qoo.foo/')
for filename in ['xyz', 'quark']:
with self.subTest(filename=filename):
name = self.storage.save(filename, File(io.BytesIO(b'abc'), 'def'))
self.assertEqual(name, filename)

def test_suspicious(self, *args):
self.storage = dropbox.DropboxStorage('foo', root_path='/bar')
Expand Down
Loading