@property
def thumbnail_path(self):
- file_name = f"{self.pk:07}.png"
+ file_name = f"{self.pk:07}.webp"
if self.storage_type == self.STORAGE_TYPE_GPG:
file_name += ".gpg"
- return os.path.join(settings.THUMBNAIL_DIR, file_name)
+ thumb = os.path.join(settings.THUMBNAIL_DIR, file_name)
+
+ if os.path.exists(thumb):
+ return thumb
+ else:
+ return os.path.splitext(thumb)[0] + ".png"
@property
def thumbnail_file(self):
"""
The thumbnail of a PDF is just a 500px wide image of the first page.
"""
- out_path = os.path.join(temp_dir, "convert.png")
+ out_path = os.path.join(temp_dir, "convert.webp")
# Run convert to get a decent thumbnail
try:
def get_optimised_thumbnail(self, document_path, mime_type, file_name=None):
thumbnail = self.get_thumbnail(document_path, mime_type, file_name)
- if settings.OPTIMIZE_THUMBNAILS:
+ if settings.OPTIMIZE_THUMBNAILS and os.path.splitext(thumbnail)[1] == ".png":
out_path = os.path.join(self.tempdir, "thumb_optipng.png")
args = (
handle = doc.thumbnail_file
# TODO: Send ETag information and use that to send new thumbnails
# if available
- return HttpResponse(handle, content_type="image/png")
+ content_type = (
+ "image/webp"
+ if os.path.splitext(doc.thumbnail_path)[1] == ".webp"
+ else "image/png"
+ )
+ return HttpResponse(handle, content_type=content_type)
except (FileNotFoundError, Document.DoesNotExist):
raise Http404()