Skip to content

Fix #93 -- Fix upload_folder on Windows #94

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

Merged
merged 3 commits into from
Feb 27, 2019
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
17 changes: 10 additions & 7 deletions s3file/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import logging
import os
import pathlib
import uuid

from django.conf import settings
Expand All @@ -13,7 +14,7 @@ class S3FileInputMixin:
"""FileInput that uses JavaScript to directly upload to Amazon S3."""

needs_multipart_form = False
upload_path = getattr(settings, 'S3FILE_UPLOAD_PATH', os.path.join('tmp', 's3file'))
upload_path = getattr(settings, 'S3FILE_UPLOAD_PATH', pathlib.PurePosixPath('tmp', 's3file'))
expires = settings.SESSION_COOKIE_AGE

@property
Expand All @@ -29,7 +30,7 @@ def build_attrs(self, *args, **kwargs):

accept = attrs.get('accept')
response = self.client.generate_presigned_post(
self.bucket_name, os.path.join(self.upload_folder, '${filename}'),
self.bucket_name, str(pathlib.PurePosixPath(self.upload_folder, '${filename}')),
Conditions=self.get_conditions(accept),
ExpiresIn=self.expires,
)
Expand All @@ -50,7 +51,7 @@ def build_attrs(self, *args, **kwargs):
def get_conditions(self, accept):
conditions = [
{"bucket": self.bucket_name},
["starts-with", "$key", self.upload_folder],
["starts-with", "$key", str(self.upload_folder)],
{"success_action_status": "201"},
]
if accept and ',' not in accept:
Expand All @@ -66,10 +67,12 @@ def get_conditions(self, accept):

@cached_property
def upload_folder(self):
return os.path.join(
return str(pathlib.PurePosixPath(
self.upload_path,
uuid.uuid4().hex,
)
base64.urlsafe_b64encode(
uuid.uuid4().bytes
).decode('utf-8').rstrip('=\n'),
)) # S3 uses POSIX paths

class Media:
js = (
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def driver():
chrome_options.add_argument('headless')
chrome_options.add_argument('window-size=1200x800')
try:
b = webdriver.Chrome(chrome_options=chrome_options)
b = webdriver.Chrome(options=chrome_options)
except WebDriverException as e:
pytest.skip(force_text(e))
else:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ def test_file_insert_submit_value(self, driver, live_server, upload_file, freeze

def test_media(self):
assert ClearableFileInput().media._js == ['s3file/js/s3file.js']

def test_upload_folder(self):
assert ClearableFileInput().upload_folder.startswith('tmp/s3file/')
assert len(ClearableFileInput().upload_folder) == 33
1 change: 1 addition & 0 deletions tests/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@
AWS_STORAGE_BUCKET_NAME = 'test-bucket'
AWS_S3_REGION_NAME = 'eu-central-1'
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_DEFAULT_ACL = None