Update image upload handler to be content-type aware

Update handle_image: made filetype required and not optional
Updated handle_image usage to reflect changes
This commit is contained in:
Nathan Beals
2022-04-23 00:11:04 -04:00
parent 9adc1f7266
commit 8df3009cb2
3 changed files with 12 additions and 7 deletions

View File

@ -38,10 +38,12 @@ def get_filetype(name):
# TODO this whole file needs proper documentation, refactoring, and testing # TODO this whole file needs proper documentation, refactoring, and testing
# TODO also add env variable to define which images sizes should be compressed # TODO also add env variable to define which images sizes should be compressed
def handle_image(request, image_object, filetype='.jpeg'): # filetype argument can not be optional, otherwise this function will treat all images as if they were a jpeg
# Because it's no longer optional, no reason to return it
def handle_image(request, image_object, filetype):
if (image_object.size / 1000) > 500: # if larger than 500 kb compress if (image_object.size / 1000) > 500: # if larger than 500 kb compress
if filetype == '.jpeg' or filetype == '.jpg': if filetype == '.jpeg' or filetype == '.jpg':
return rescale_image_jpeg(image_object), filetype return rescale_image_jpeg(image_object)
if filetype == '.png': if filetype == '.png':
return rescale_image_png(image_object), filetype return rescale_image_png(image_object)
return image_object, filetype return image_object

View File

@ -253,7 +253,7 @@ class Integration:
:param image_file: ByteIO stream containing the image :param image_file: ByteIO stream containing the image
:param filetype: type of file to write bytes to, default to .jpeg if unknown :param filetype: type of file to write bytes to, default to .jpeg if unknown
""" """
recipe.image = File(handle_image(self.request, File(image_file, name='image'), filetype=filetype)[0], name=f'{uuid.uuid4()}_{recipe.pk}{filetype}') recipe.image = File(handle_image(self.request, File(image_file, name='image'), filetype=filetype), name=f'{uuid.uuid4()}_{recipe.pk}{filetype}')
recipe.save() recipe.save()
def get_recipe_from_file(self, file): def get_recipe_from_file(self, file):

View File

@ -1,5 +1,6 @@
import io import io
import json import json
import mimetypes
import re import re
import uuid import uuid
from collections import OrderedDict from collections import OrderedDict
@ -772,14 +773,16 @@ class RecipeViewSet(viewsets.ModelViewSet):
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
image = None image = None
filetype = ".jpeg" # fall-back to .jpeg, even if wrong, at least users will know it's an image and most image viewers can open it correctly anyways
if 'image' in serializer.validated_data: if 'image' in serializer.validated_data:
image = obj.image image = obj.image
filetype = mimetypes.guess_extension(serializer.validated_data['image'].content_type) or filetype
elif 'image_url' in serializer.validated_data: elif 'image_url' in serializer.validated_data:
try: try:
response = requests.get(serializer.validated_data['image_url']) response = requests.get(serializer.validated_data['image_url'])
image = File(io.BytesIO(response.content)) image = File(io.BytesIO(response.content))
print('test') filetype = mimetypes.guess_extension(response.headers['content-type']) or filetype
except UnidentifiedImageError as e: except UnidentifiedImageError as e:
print(e) print(e)
pass pass
@ -791,7 +794,7 @@ class RecipeViewSet(viewsets.ModelViewSet):
pass pass
if image is not None: if image is not None:
img, filetype = handle_image(request, image) img = handle_image(request, image, filetype)
obj.image = File(img, name=f'{uuid.uuid4()}_{obj.pk}{filetype}') obj.image = File(img, name=f'{uuid.uuid4()}_{obj.pk}{filetype}')
obj.save() obj.save()
return Response(serializer.data) return Response(serializer.data)