Skip to content

Commit

Permalink
attempt to fix #99: Upload compatible with storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Dec 6, 2023
1 parent dc28c13 commit 9e28109
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
21 changes: 6 additions & 15 deletions formset/upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import mimetypes
import os
import tempfile
from pathlib import Path

from django.contrib.staticfiles.storage import staticfiles_storage
Expand All @@ -10,7 +8,7 @@

THUMBNAIL_MAX_HEIGHT = 200
THUMBNAIL_MAX_WIDTH = 350
UPLOAD_TEMP_DIR = Path(default_storage.base_location) / 'upload_temp'
UPLOAD_TEMP_DIR = Path('upload_temp')


def get_thumbnail_path(image_path, image_height=THUMBNAIL_MAX_HEIGHT):
Expand All @@ -23,15 +21,15 @@ def thumbnail_image(storage, image_path, image_height=THUMBNAIL_MAX_HEIGHT):
try:
from PIL import Image, ImageOps

image = Image.open(image_path)
image = Image.open(storage.path(image_path))
except Exception:
return staticfiles_storage.url('formset/icons/file-picture.svg')
else:
height = int(image_height)
width = int(round(image.width * height / image.height))
width, height = min(width, THUMBNAIL_MAX_WIDTH), min(height, THUMBNAIL_MAX_HEIGHT)
thumb = ImageOps.fit(ImageOps.exif_transpose(image), (width, height))
thumbnail_path = get_thumbnail_path(image_path, image_height)
thumbnail_path = get_thumbnail_path(storage.path(image_path), image_height)
thumb.save(thumbnail_path)
return storage.url(thumbnail_path.relative_to(storage.location))

Expand Down Expand Up @@ -106,16 +104,9 @@ def _receive_uploaded_file(self, file_obj, image_height=None):
return HttpResponseBadRequest(f"File upload failed for '{file_obj.name}'.")
signer = get_cookie_signer(salt='formset')

# copy uploaded file into temporary clipboard inside the default storage location
UPLOAD_TEMP_DIR.mkdir(parents=True, exist_ok=True)
file_path = Path(file_obj.name)
fh, temp_path = tempfile.mkstemp(suffix=file_path.suffix, prefix=f'{file_path.stem}.', dir=UPLOAD_TEMP_DIR)
for chunk in file_obj.chunks():
os.write(fh, chunk)
os.close(fh)
temp_path = default_storage.save(UPLOAD_TEMP_DIR / file_obj.name, file_obj)
assert default_storage.size(temp_path) == file_obj.size
relative_path = Path(temp_path).relative_to(default_storage.location)
download_url = default_storage.url(relative_path)
download_url = default_storage.url(temp_path)

# dict returned by the form on submission
mime_type, sub_type = split_mime_type(file_obj.content_type)
Expand All @@ -127,7 +118,7 @@ def _receive_uploaded_file(self, file_obj, image_height=None):
else:
thumbnail_url = file_icon_url(mime_type, sub_type)
file_handle = {
'upload_temp_name': signer.sign(relative_path),
'upload_temp_name': signer.sign(temp_path),
'content_type': f'{mime_type}/{sub_type}',
'content_type_extra': file_obj.content_type_extra,
'name': file_obj.name[:self.filename_max_length],
Expand Down
2 changes: 1 addition & 1 deletion formset/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def value_from_datadict(self, data, files, name):
# file has just been uploaded
signer = get_cookie_signer(salt='formset')
upload_temp_name = signer.unsign(handle['upload_temp_name'])
file = open(default_storage.path(upload_temp_name), 'rb')
file = default_storage.open(upload_temp_name, 'rb')
file.seek(0, os.SEEK_END)
size = file.tell()
file.seek(0)
Expand Down
4 changes: 3 additions & 1 deletion testapp/tests/test_e2e_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_upload_image(page, mocker, viewname):
img_element = dropbox.locator('img')
expect(img_element).to_be_visible()
img_src = img_element.get_attribute('src')
match = re.match(r'^/media/((upload_temp/python-django\.[a-z0-9_]+?)_h128(.png))$', img_src)
match = re.match(r'^/media/((upload_temp/python-django_[A-Za-z0-9]+?)_h128(.png))$', img_src)
if match is None:
print('img_src ', img_src)
assert match is not None
thumbnail_url = match.group(1)
assert (settings.MEDIA_ROOT / thumbnail_url).exists() # the thumbnail
Expand Down

0 comments on commit 9e28109

Please sign in to comment.