]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Logs the errors during thumbnail generation, updates default to be WebP
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Tue, 12 Sep 2023 03:07:06 +0000 (20:07 -0700)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Tue, 12 Sep 2023 19:16:22 +0000 (12:16 -0700)
src/documents/parsers.py
src/documents/resources/document.png [deleted file]
src/documents/resources/document.webp [new file with mode: 0644]
src/documents/tests/test_file_handling.py

index 2e6b17cb5f5a6f17f3489315a4b8a5557a84566b..dc67af1a2439b8b32ee2925f3c47fdd7c9f33699 100644 (file)
@@ -166,11 +166,11 @@ def run_convert(
         raise ParseError(f"Convert failed at {args}")
 
 
-def get_default_thumbnail() -> str:
+def get_default_thumbnail() -> Path:
     """
     Returns the path to a generic thumbnail
     """
-    return os.path.join(os.path.dirname(__file__), "resources", "document.png")
+    return (Path(__file__).parent / "resources" / "document.webp").resolve()
 
 
 def make_thumbnail_from_pdf_gs_fallback(in_path, temp_dir, logging_group=None) -> str:
@@ -183,12 +183,13 @@ def make_thumbnail_from_pdf_gs_fallback(in_path, temp_dir, logging_group=None) -
         "to ghostscript. Check your /etc/ImageMagick-x/policy.xml!",
         extra={"group": logging_group},
     )
+    # Ghostscript doesn't handle WebP outputs
     gs_out_path = os.path.join(temp_dir, "gs_out.png")
     cmd = [settings.GS_BINARY, "-q", "-sDEVICE=pngalpha", "-o", gs_out_path, in_path]
     try:
         if not subprocess.Popen(cmd).wait() == 0:
             raise ParseError(f"Thumbnail (gs) failed at {cmd}")
-        # then run convert on the output from gs
+        # then run convert on the output from gs to make WebP
         run_convert(
             density=300,
             scale="500x5000>",
@@ -203,11 +204,12 @@ def make_thumbnail_from_pdf_gs_fallback(in_path, temp_dir, logging_group=None) -
 
         return out_path
 
-    except ParseError:
+    except ParseError as e:
+        logger.error(f"Unable to make thumbnail with Ghostscript: {e}")
         # The caller might expect a generated thumbnail that can be moved,
         # so we need to copy it before it gets moved.
         # https://github.com/paperless-ngx/paperless-ngx/issues/3631
-        default_thumbnail_path = os.path.join(temp_dir, "document.png")
+        default_thumbnail_path = os.path.join(temp_dir, "document.webp")
         copy_file_with_basic_stats(get_default_thumbnail(), default_thumbnail_path)
         return default_thumbnail_path
 
@@ -231,7 +233,8 @@ def make_thumbnail_from_pdf(in_path, temp_dir, logging_group=None) -> str:
             output_file=out_path,
             logging_group=logging_group,
         )
-    except ParseError:
+    except ParseError as e:
+        logger.error(f"Unable to make thumbnail with convert: {e}")
         out_path = make_thumbnail_from_pdf_gs_fallback(in_path, temp_dir, logging_group)
 
     return out_path
diff --git a/src/documents/resources/document.png b/src/documents/resources/document.png
deleted file mode 100644 (file)
index 164afd1..0000000
Binary files a/src/documents/resources/document.png and /dev/null differ
diff --git a/src/documents/resources/document.webp b/src/documents/resources/document.webp
new file mode 100644 (file)
index 0000000..700dd79
Binary files /dev/null and b/src/documents/resources/document.webp differ
index 9e27b9aad3c0cf2d849e3bf6eb02fa6fa38c83bd..89c2e6e463f01f8f1ba887a31adacb338da62b29 100644 (file)
@@ -820,7 +820,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
 
     @override_settings(FILENAME_FORMAT="{title}")
     def test_archive_deleted2(self):
-        original = os.path.join(settings.ORIGINALS_DIR, "document.png")
+        original = os.path.join(settings.ORIGINALS_DIR, "document.webp")
         original2 = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf")
         archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")
         Path(original).touch()
@@ -828,9 +828,9 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
         Path(archive).touch()
 
         doc1 = Document.objects.create(
-            mime_type="image/png",
+            mime_type="image/webp",
             title="document",
-            filename="document.png",
+            filename="document.webp",
             checksum="A",
             archive_checksum="B",
             archive_filename="0000001.pdf",