]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
webp thumbnail support with png fallback
authorMichael Shamoon <4887959+shamoon@users.noreply.github.com>
Fri, 10 Jun 2022 08:39:20 +0000 (01:39 -0700)
committerMichael Shamoon <4887959+shamoon@users.noreply.github.com>
Fri, 10 Jun 2022 09:28:13 +0000 (02:28 -0700)
src/documents/models.py
src/documents/parsers.py
src/documents/views.py

index 0061e5d0fc44670bf36605abccdcd1b4507a17db..fe6d9ca20d49f63e97c635a03c4b3cf4429b4f6c 100644 (file)
@@ -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):
index 469ec2f1e8701a275948b2bf12eb568387158bf0..bc8af0ec8238dc7422b122d594608d4a96d8e91c 100644 (file)
@@ -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 = (
index cdd38180bb1d10b0e4bde5af64ebef6edc079d85..1e9583bc52576462bdcbaff74b4a81ad8a33500f 100644 (file)
@@ -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()