mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-24 17:06:18 -05:00
Hiding Calibre cover and metadata files when discovered.
This commit is contained in:
parent
7a855b57cc
commit
a5da49a135
3 changed files with 26 additions and 17 deletions
|
|
@ -175,6 +175,7 @@ class EBook(ModelHelper, Base):
|
|||
raise ValueError('Cannot generate cover for ebook without ebook file!')
|
||||
|
||||
if calibre_cover := discover_calibre_cover(path):
|
||||
self.hide_calibre_files()
|
||||
return calibre_cover
|
||||
|
||||
cover_bytes = extract_ebook_cover(path)
|
||||
|
|
@ -194,6 +195,24 @@ class EBook(ModelHelper, Base):
|
|||
def cover_path(self) -> Optional[pathlib.Path]:
|
||||
return self.cover_file['path']
|
||||
|
||||
def hide_calibre_files(self):
|
||||
"""Delete FileGroups of the cover/metadata when this is a Calibre ebook directory."""
|
||||
path = self.file_group.primary_path
|
||||
if not discover_calibre_cover(path):
|
||||
raise ValueError(f'Refusing to hide Calibre files when we do not have a Calibre ebook directory!')
|
||||
|
||||
metadata = path.parent / 'metadata.opf'
|
||||
cover = path.parent / 'cover.jpg'
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
file_groups = session.query(FileGroup) \
|
||||
.filter(or_(
|
||||
FileGroup.primary_path == str(metadata),
|
||||
FileGroup.primary_path == str(cover),
|
||||
))
|
||||
for file_group in file_groups:
|
||||
session.delete(file_group)
|
||||
|
||||
|
||||
def _model_ebook(ebook: EBook) -> EBook:
|
||||
"""Creates an EBook model based off a File. Searches for it's cover in the provided `files`."""
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ class FileGroup(ModelHelper, Base):
|
|||
model = Column(String) # "video", "archive", "ebook", etc.
|
||||
modification_datetime = Column(TZDateTime)
|
||||
# the Path of the file that can be modeled or indexed.
|
||||
primary_path = Column(MediaPathType, nullable=False, unique=True)
|
||||
primary_path: pathlib.Path = Column(MediaPathType, nullable=False, unique=True)
|
||||
size = Column(BigInteger, default=lambda: 0)
|
||||
title = Column(String)
|
||||
|
||||
|
|
|
|||
|
|
@ -117,27 +117,17 @@ async def test_discover_calibre_cover(test_session, test_directory, example_epub
|
|||
metadata = test_directory / 'metadata.opf'
|
||||
metadata.touch()
|
||||
|
||||
await refresh_files()
|
||||
|
||||
assert test_session.query(EBook).count() == 1
|
||||
ebook: EBook = test_session.query(EBook).one()
|
||||
assert ebook.file_group.title == 'WROLPi Test Book'
|
||||
# The cover in the ebook was extracted.
|
||||
assert ebook.cover_file['path'] == test_directory / 'ebook example.jpeg'
|
||||
assert (test_directory / 'ebook example.jpeg').is_file()
|
||||
|
||||
# Delete the extracted cover, use the cover image from Calibre.
|
||||
(test_directory / 'ebook example.jpeg').unlink()
|
||||
# Create a "cover.jpg" file near the ebook, this is the Calibre cover.
|
||||
cover_image = test_directory / 'cover.jpg'
|
||||
shutil.move(image_file, cover_image)
|
||||
|
||||
# Reset files.
|
||||
for file_group in test_session.query(FileGroup):
|
||||
test_session.delete(file_group)
|
||||
test_session.commit()
|
||||
|
||||
# Calibre cover image was discovered, no cover was generated.
|
||||
await refresh_files()
|
||||
ebook: EBook = test_session.query(EBook).one()
|
||||
assert ebook.file_group.title == 'WROLPi Test Book'
|
||||
assert ebook.cover_file['path'] == test_directory / 'cover.jpg'
|
||||
assert {i['path'].name for i in ebook.file_group.files} == {'ebook example.epub', 'ebook example.mobi', 'cover.jpg'}
|
||||
# Cover is not the first image from the EPUB.
|
||||
assert ebook.cover_path.stat().st_size != 292579
|
||||
# Metadata and cover were deleted from the FileGroups.
|
||||
assert test_session.query(FileGroup).count() == 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue