You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sorl-thumbnail does not provide any way to catch and address errors when attempting to read invalid images using get_thumbnail(). I noticed that the project has a THUMBNAIL_DEBUG setting to address this problem, but it appears to only apply to template tags and not when using the low-level API directly.
Here is my specific use case: I have a Django REST Framework serializer for users that contains a photo field which produces a URL to a thumbnail of the user's photo. Here is the current code:
fromdjango.contrib.staticfiles.storageimportstaticfiles_storagefromrest_frameworkimportserializersfromsorl.thumbnailimportget_thumbnailclassUserSerializer(serializers.ModelSerializer):
photo=serializers.SerializerMethodField("get_photo_url")
classMeta:
model=Userfields= ("first_name", "last_name", "photo")
defget_photo_url(self, obj):
try:
photo=obj.photoexceptPhoto.DoesNotExist:
returnstaticfiles_storage.url("img/person-outline.png")
else:
# broken images cause errors to be logged, but those errors are not propagated herethumbnail=get_thumbnail(photo.img, "135x135", crop="center")
returnself.context["request"].build_absolute_uri(thumbnail.url)
If there is something wrong with the image provided to get_thumbnail(), perhaps it doesn't exist or cannot be read as an image, that exception is not propagated here. Instead, errors are logged and a dummy thumbnail is used instead.
I'd like to detect when this happens so that I can I can provide a static image instead, as you can see I am doing when the Photo instance for the user does not exist (ie. the user has never uploaded a photo). I could use the THUMBNAIL_DUMMY_SOURCE setting to achieve this, but that is a global setting and I have a similar paradigm when serializing other types of objects other than users. I don't want to share the same thumbnail for each of these cases, I want a different thumbnail for each type.
The only solution I can think of that I could apply right now would be to always inspect the image file before providing it to get_thumbnail(), but obviously this is very inefficient.
If THUMBNAIL_DEBUG was changed to apply when using the low-level API, this would solve my problem, but honestly it wouldn't be an ideal solution. I'd rather not raise errors across the entire application, but instead I think it would be better to be able to opt into raising errors in specific contexts. For instance, a kwarg raise_errors could be added to get_thumbnail():
Does this request make sense? Have I missed anything? Perhaps there is a feature in sorl-thumbnail I am not aware of that satisfies my use case? Regardless, thank you very much for reading. I appreciate your time.
The text was updated successfully, but these errors were encountered:
In my case, this was the error message if I called the get_thumbnail function:
'NoneType' object has no attribute 'build_absolute_uri'
The error's reason:
There was no request object in the serializer's context dictionary.
The solution:
In the View, I call the serializer with serializer = TheSerializer(instance, data=data, context=self.get_serializer_context())
or serializer = TheSerializer(instance, data=data, context={'request': request})
sorl-thumbnail
does not provide any way to catch and address errors when attempting to read invalid images usingget_thumbnail()
. I noticed that the project has aTHUMBNAIL_DEBUG
setting to address this problem, but it appears to only apply to template tags and not when using the low-level API directly.Here is my specific use case: I have a Django REST Framework serializer for users that contains a
photo
field which produces a URL to a thumbnail of the user's photo. Here is the current code:If there is something wrong with the image provided to
get_thumbnail()
, perhaps it doesn't exist or cannot be read as an image, that exception is not propagated here. Instead, errors are logged and a dummy thumbnail is used instead.I'd like to detect when this happens so that I can I can provide a static image instead, as you can see I am doing when the
Photo
instance for the user does not exist (ie. the user has never uploaded a photo). I could use theTHUMBNAIL_DUMMY_SOURCE
setting to achieve this, but that is a global setting and I have a similar paradigm when serializing other types of objects other than users. I don't want to share the same thumbnail for each of these cases, I want a different thumbnail for each type.The only solution I can think of that I could apply right now would be to always inspect the image file before providing it to
get_thumbnail()
, but obviously this is very inefficient.If
THUMBNAIL_DEBUG
was changed to apply when using the low-level API, this would solve my problem, but honestly it wouldn't be an ideal solution. I'd rather not raise errors across the entire application, but instead I think it would be better to be able to opt into raising errors in specific contexts. For instance, a kwargraise_errors
could be added toget_thumbnail()
:Does this request make sense? Have I missed anything? Perhaps there is a feature in
sorl-thumbnail
I am not aware of that satisfies my use case? Regardless, thank you very much for reading. I appreciate your time.The text was updated successfully, but these errors were encountered: