{isoDatetimeToAgoPopup(dt, false)}
diff --git a/wrolpi/files/pdfs.py b/wrolpi/files/pdfs.py index f300de71..b39334d1 100644 --- a/wrolpi/files/pdfs.py +++ b/wrolpi/files/pdfs.py @@ -150,6 +150,12 @@ async def pdf_modeler(): file_group.published_datetime = metadata.published_datetime file_group.published_modified_datetime = metadata.modification_datetime file_group.model = 'pdf' + + # Find an image with the same stem to use as the poster. + poster_files = file_group.my_poster_files() + if poster_files: + file_group.data = file_group.data or {} + file_group.data['poster_path'] = str(poster_files[0]['path']) except Exception as e: logger.error(f'Failed to index PDF {pdf_file}', exc_info=e) if PYTEST: diff --git a/wrolpi/files/test/test_pdfs.py b/wrolpi/files/test/test_pdfs.py index 80de6e1b..e5c069b3 100644 --- a/wrolpi/files/test/test_pdfs.py +++ b/wrolpi/files/test/test_pdfs.py @@ -1,6 +1,7 @@ from unittest import mock import pytest +from PIL import Image from wrolpi.dates import now from wrolpi.files import lib as files_lib @@ -74,3 +75,20 @@ async def test_pdf_indexer_max_size(test_session, example_pdf): await pdfs.pdf_modeler() file_group = test_session.query(FileGroup).one() assert file_group.d_text is None + + +@pytest.mark.asyncio +async def test_pdf_poster(test_session, example_pdf): + """PDF modeler finds an image with the same stem and uses it as the poster.""" + # Create an image with the same stem as the PDF. + poster_path = example_pdf.with_suffix('.jpg') + Image.new('RGB', (25, 25), color='grey').save(poster_path) + + files_lib._upsert_files([example_pdf, poster_path], now()) + + await pdfs.pdf_modeler() + file_group = test_session.query(FileGroup).one() + assert file_group.indexed is True + assert file_group.data is not None + # FancyJSON converts paths back to pathlib.Path when reading from DB. + assert file_group.data.get('poster_path') == poster_path