Skip to content

Commit 4587453

Browse files
author
aaron
committed
fix #104, prepend default_storage.location to upload_path then strip it back out before passing to S3Boto3Storage
1 parent c6addde commit 4587453

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

s3file/forms.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ class S3FileInputMixin:
1414
"""FileInput that uses JavaScript to directly upload to Amazon S3."""
1515

1616
needs_multipart_form = False
17-
upload_path = getattr(
18-
settings, 'S3FILE_UPLOAD_PATH', pathlib.PurePosixPath('tmp', 's3file')
19-
)
17+
try:
18+
upload_path = pathlib.PurePosixPath(
19+
default_storage.location, getattr(settings, 'S3FILE_UPLOAD_PATH')
20+
)
21+
except AttributeError:
22+
upload_path = pathlib.PurePosixPath(default_storage.location, 'tmp', 's3file')
23+
2024
expires = settings.SESSION_COOKIE_AGE
2125

2226
@property

s3file/middleware.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __call__(self, request):
2323
def get_files_from_storage(paths):
2424
"""Return S3 file where the name does not include the path."""
2525
for path in paths:
26+
path = path.replace(default_storage.location + '/', '', 1)
2627
try:
2728
f = default_storage.open(path)
2829
f.name = os.path.basename(path)

tests/test_forms.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def url(self):
3434
@pytest.fixture
3535
def freeze(self, monkeypatch):
3636
"""Freeze datetime and UUID."""
37-
monkeypatch.setattr('s3file.forms.S3FileInputMixin.upload_folder', 'tmp')
37+
monkeypatch.setattr(
38+
's3file.forms.S3FileInputMixin.upload_folder',
39+
default_storage.location + '/' + 'tmp'
40+
)
3841

3942
def test_value_from_datadict(self, client, upload_file):
4043
with open(upload_file) as f:
@@ -95,7 +98,7 @@ def test_get_conditions(self, freeze):
9598
assert all(condition in conditions for condition in [
9699
{"bucket": 'test-bucket'},
97100
{"success_action_status": "201"},
98-
['starts-with', '$key', 'tmp'],
101+
['starts-with', '$key', default_storage.location + '/' + 'tmp'],
99102
["starts-with", "$Content-Type", ""]
100103
]), conditions
101104

@@ -188,5 +191,7 @@ def test_media(self):
188191
assert ClearableFileInput().media._js == ['s3file/js/s3file.js']
189192

190193
def test_upload_folder(self):
191-
assert ClearableFileInput().upload_folder.startswith('tmp/s3file/')
192-
assert len(ClearableFileInput().upload_folder) == 33
194+
assert ClearableFileInput().upload_folder.startswith(
195+
default_storage.location + '/' + 'tmp/s3file/'
196+
)
197+
assert len(ClearableFileInput().upload_folder) == 50

0 commit comments

Comments
 (0)