Fix: restore image without archive version thumbnail generation (#14741)

This commit is contained in:
shamoon authored and GitHub committed 2026-11-11 10:12:47 -08:00
1 parent d05e96cda2
commit 759f9cd22b
2 files changed
+57

No files matched your search

+5
View File
@@ -17,6 +17,7 @@ from django.conf import settings
from PIL import Image
from documents.parsers import ParseError
from documents.parsers import encode_thumbnail_webp
from documents.parsers import make_thumbnail_from_pdf
from documents.utils import copy_file_with_basic_stats
from documents.utils import maybe_override_pixel_limit
@@ -162,6 +163,10 @@ class RasterisedDocumentParser:
# ------------------------------------------------------------------
def get_thumbnail(self, document_path: Path, mime_type: str) -> Path:
if self.archive_path is None and self.is_image(mime_type):
thumbnail_path = self.tempdir / "convert.webp"
encode_thumbnail_webp(Path(document_path), thumbnail_path)
return thumbnail_path
return make_thumbnail_from_pdf(
self.archive_path or Path(document_path),
self.tempdir,
@@ -18,6 +18,7 @@ from ocrmypdf import SubprocessOutputError
from PIL import Image
from documents.parsers import ParseError
from documents.parsers import get_default_thumbnail
from documents.parsers import rasterize_pdf_page_to_png
from paperless.models import ModeChoices
from paperless.parsers import ParserProtocol
@@ -270,6 +271,57 @@ class TestDpiHelpers:
class TestGetThumbnail:
@pytest.mark.parametrize(
("filename", "mime_type"),
[
pytest.param("simple.jpg", "image/jpeg", id="jpeg"),
pytest.param("simple.png", "image/png", id="png"),
pytest.param("simple.tif", "image/tiff", id="tiff"),
pytest.param("simple.gif", "image/gif", id="gif"),
pytest.param("simple.bmp", "image/bmp", id="bmp"),
pytest.param("document.webp", "image/webp", id="webp"),
pytest.param("simple.heic", "image/heic", id="heic"),
],
)
def test_image_without_archive_has_image_thumbnail(
self,
mocker: MockerFixture,
tesseract_parser: RasterisedDocumentParser,
tesseract_samples_dir: Path,
filename: str,
mime_type: str,
) -> None:
original = tesseract_samples_dir / filename
render_pdf = mocker.patch("documents.parsers.rasterize_pdf_page_to_png")
thumb = tesseract_parser.get_thumbnail(original, mime_type)
render_pdf.assert_not_called()
assert thumb.read_bytes() != get_default_thumbnail().read_bytes()
with Image.open(thumb) as im:
assert im.format == "WEBP"
assert 0 < im.width <= 500
assert 0 < im.height <= 5000
def test_image_with_archive_uses_pdf_thumbnail(
self,
mocker: MockerFixture,
tesseract_parser: RasterisedDocumentParser,
tesseract_samples_dir: Path,
simple_digital_pdf_file: Path,
) -> None:
tesseract_parser.archive_path = simple_digital_pdf_file
target = "paperless.parsers.tesseract.make_thumbnail_from_pdf"
make_thumbnail = mocker.patch(target)
original = tesseract_samples_dir / "simple.jpg"
tesseract_parser.get_thumbnail(original, "image/jpeg")
make_thumbnail.assert_called_once_with(
simple_digital_pdf_file,
tesseract_parser.tempdir,
)
def test_thumbnail_is_file(
self,
tesseract_parser: RasterisedDocumentParser,