99from django .utils .safestring import mark_safe
1010from django .utils .translation import gettext_lazy as _
1111
12+ from easy_thumbnails .exceptions import InvalidImageFormatError
1213from easy_thumbnails .files import get_thumbnailer
1314from easy_thumbnails .options import ThumbnailOptions
1415
@@ -94,21 +95,28 @@ def filer_has_permission(context, item, action):
9495
9596
9697def file_icon_context (file , detail , width , height ):
97- # Only check on FileSystemStorage if file exists for performance reasons
98- if isinstance (default_storage , FileSystemStorage ) and not file .file .exists ():
99- return {
100- 'icon_url' : staticfiles_storage .url ('filer/icons/file-missing.svg' ),
101- 'alt_text' : _ ("File is missing" ),
102- 'width' : width ,
103- 'height' : height ,
104- }
10598 mime_maintype , mime_subtype = file .mime_maintype , file .mime_subtype
10699 context = {
107100 'mime_maintype' : mime_maintype ,
108101 'mime_type' : file .mime_type ,
109102 }
103+ # Get download_url and aspect ratio right for detail view
110104 if detail :
111105 context ['download_url' ] = file .url
106+ if file .width :
107+ width , height = 210 , ceil (210 / file .width * file .height )
108+ context ['sidebar_image_ratio' ] = file .width / 210
109+ # returned context if icon is not available
110+ not_available_context = {
111+ 'icon_url' : staticfiles_storage .url ('filer/icons/file-missing.svg' ),
112+ 'alt_text' : _ ("File is missing" ),
113+ 'width' : width ,
114+ 'height' : width , # The icon is a square
115+ }
116+ # Check if file exists for performance reasons (only on FileSystemStorage)
117+ if isinstance (default_storage , FileSystemStorage ) and file .file and not file .file .exists ():
118+ return not_available_context
119+
112120 if isinstance (file , BaseImage ):
113121 thumbnailer = get_thumbnailer (file )
114122
@@ -118,14 +126,12 @@ def file_icon_context(file, detail, width, height):
118126 icon_url = staticfiles_storage .url ('filer/icons/file-unknown.svg' )
119127 else :
120128 if detail :
121- width , height = 210 , ceil (210 / file .width * file .height )
122- context ['sidebar_image_ratio' ] = file .width / 210
123129 opts = {'size' : (width , height ), 'upscale' : True }
124130 else :
125131 opts = {'size' : (width , height ), 'crop' : True }
126132 thumbnail_options = ThumbnailOptions (opts )
127133 # Optimize directory listing:
128- if not detail and width == height and width in DEFERRED_THUMBNAIL_SIZES and hasattr (file , "thumbnail_name" ):
134+ if width == height and width in DEFERRED_THUMBNAIL_SIZES and hasattr (file , "thumbnail_name" ):
129135 # Get name of thumbnail from easy-thumbnail
130136 configured_name = thumbnailer .get_thumbnail_name (thumbnail_options , transparent = file ._transparent )
131137 # If the name was annotated: Thumbnail exists and we can use it
@@ -137,17 +143,27 @@ def file_icon_context(file, detail, width, height):
137143 icon_url = reverse ("admin:filer_file_fileicon" , args = (file .pk , width ))
138144 context ['alt_text' ] = file .default_alt_text
139145 else :
140- icon_url = thumbnailer .get_thumbnail (thumbnail_options ).url
141- context ['alt_text' ] = file .default_alt_text
142- if mime_subtype != 'svg+xml' :
143- thumbnail_options ['size' ] = 2 * width , 2 * height
144- context ['highres_url' ] = thumbnailer .get_thumbnail (thumbnail_options ).url
146+ # Try creating thumbnails / take existing ones
147+ try :
148+ icon_url = thumbnailer .get_thumbnail (thumbnail_options ).url
149+ context ['alt_text' ] = file .default_alt_text
150+ if mime_subtype != 'svg+xml' :
151+ thumbnail_options ['size' ] = 2 * width , 2 * height
152+ context ['highres_url' ] = thumbnailer .get_thumbnail (thumbnail_options ).url
153+ except (InvalidImageFormatError , ):
154+ # This is caught by file.exists() for file storage systems
155+ # For remote storage systems we catch the error to avoid second trip
156+ # to the storage system
157+ return not_available_context
145158 elif mime_maintype in ['audio' , 'font' , 'video' ]:
146159 icon_url = staticfiles_storage .url (f'filer/icons/file-{ mime_maintype } .svg' )
160+ height = width # icon is a square
147161 elif mime_maintype == 'application' and mime_subtype in ['zip' , 'pdf' ]:
148162 icon_url = staticfiles_storage .url (f'filer/icons/file-{ mime_subtype } .svg' )
163+ height = width # icon is a square
149164 else :
150165 icon_url = staticfiles_storage .url ('filer/icons/file-unknown.svg' )
166+ height = width # icon is a square
151167 context .update (width = width , height = height , icon_url = icon_url )
152168 return context
153169
0 commit comments