diff --git a/wrolpi/files/ebooks.py b/wrolpi/files/ebooks.py index 2bcc366a..1e3902c8 100644 --- a/wrolpi/files/ebooks.py +++ b/wrolpi/files/ebooks.py @@ -183,7 +183,7 @@ class EBook(ModelHelper, Base): cover_path.write_bytes(cover_bytes) return cover_path - logger.warning(f'Unable to generate cover for {self.ebook_path}') + logger.warning(f'Unable to generate ebook cover for {self.file_group.primary_path}') @property def cover_file(self) -> Optional[dict]: @@ -236,8 +236,9 @@ def _model_ebook(ebook: EBook) -> EBook: else: # Extract cover because there is no cover file. cover_path = ebook.generate_cover() - ebook.file_group.append_files(cover_path) - ebook.file_group.data['cover_path'] = str(cover_path) + if cover_path: + ebook.file_group.append_files(cover_path) + ebook.file_group.data['cover_path'] = str(cover_path) if not ebook.file_group.title: # Book was not indexed above (probably a MOBI), use the file data. diff --git a/wrolpi/files/test/test_ebooks.py b/wrolpi/files/test/test_ebooks.py index 02890f0e..d1860da4 100644 --- a/wrolpi/files/test/test_ebooks.py +++ b/wrolpi/files/test/test_ebooks.py @@ -66,6 +66,16 @@ async def test_discover_local_cover(test_session, test_directory, example_epub, assert ebook.cover_path.read_bytes() == cover_path.read_bytes() +@pytest.mark.asyncio +async def test_extract_cover(test_session, test_directory, example_epub): + """First image is extracted from the Ebook and used as the cover.""" + await refresh_files() + + ebook: EBook = test_session.query(EBook).one() + # The WROLPi logo is the first image in the example epub. + assert ebook.cover_path and ebook.cover_path.stat().st_size == 297099 + + def test_search(test_session, test_client, example_epub): """Ebooks are handled in File search results.""" request, response = test_client.post('/api/files/refresh')