Implement storage configuration as suggested by @jeremystretch

This commit is contained in:
Sander Steffann
2019-12-11 16:09:32 +01:00
parent 837db7708a
commit e85889a405
3 changed files with 29 additions and 68 deletions

View File

@@ -611,21 +611,20 @@ class ImageAttachment(models.Model):
@property
def size(self):
"""
Wrapper around `image.size` to suppress an OSError in case the file is inaccessible. When S3 storage is used
ClientError is suppressed instead.
Wrapper around `image.size` to suppress an OSError in case the file is inaccessible. Also opportunistically
catch other exceptions that we know other storage back-ends to throw.
"""
from django.conf import settings
if settings.MEDIA_STORAGE and settings.MEDIA_STORAGE['BACKEND'] == 'S3':
# For S3 we need to handle a different exception
expected_exceptions = [OSError]
try:
from botocore.exceptions import ClientError
try:
return self.image.size
except ClientError:
return None
expected_exceptions.append(ClientError)
except ImportError:
pass
try:
return self.image.size
except OSError:
except tuple(expected_exceptions):
return None