Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

webp thumbnail support with png fallback

Michael Shamoon58f2c6a5fc27
diff --git a/src/documents/models.py b/src/documents/models.py
index 0061e5d0f..fe6d9ca20 100644
--- a/src/documents/models.py
+++ b/src/documents/models.py
@@ -293,11 +293,16 @@ class Document(models.Model):
@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):
diff --git a/src/documents/parsers.py b/src/documents/parsers.py
index 469ec2f1e..bc8af0ec8 100644
--- a/src/documents/parsers.py
+++ b/src/documents/parsers.py
@@ -191,7 +191,7 @@ def make_thumbnail_from_pdf(in_path, temp_dir, logging_group=None) -> str:
"""
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:
@@ -321,7 +321,7 @@ class DocumentParser(LoggingMixin):
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 = (
diff --git a/src/documents/views.py b/src/documents/views.py
index cdd38180b..1e9583bc5 100644
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -362,7 +362,12 @@ class DocumentViewSet(
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()