Skip to content

Commit

Permalink
Change cover file name in EPUB to cover.jpg FIX #20
Browse files Browse the repository at this point in the history
  • Loading branch information
gvellut committed May 12, 2022
1 parent 6dd3bf1 commit 1deef1d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
38 changes: 31 additions & 7 deletions jncep/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,10 @@ def _local_image_filename(image):
# ext is almost always .jpg but sometimes it is .jpeg
# splitext works fine with a url
root, ext = os.path.splitext(image.url)
new_local_filename = to_safe_filename(root) + ext
root = root.replace("https://", "")
# i is alphabetically greater than c (for cover.jpg)
# so cover will always be first in zip ; useful for File Explorer cf GH #20
new_local_filename = "i_" + to_safe_filename(root) + ext
return new_local_filename


Expand Down Expand Up @@ -733,14 +736,35 @@ async def fill_covers_and_content(session, cover_volumes, content_parts):
contents, covers = await bag(tasks)

for part in content_parts:
if part.part_id in contents:
content, images = contents[part.part_id]
part.content = content
part.images = images
content, images = contents[part.part_id]
part.content = content
part.images = images

for volume in cover_volumes:
if volume.volume_id in covers:
volume.cover = covers[volume.volume_id]
volume.cover = covers[volume.volume_id]

_rename_cover_images(cover_volumes)


def _rename_cover_images(volumes):
# replace cover local filename from the default to cover.jpg
# both in part images + volume.cover
cover_filename = "cover.jpg"
for volume in volumes:
if not volume.cover:
continue

cover_image = volume.cover
cover_image.local_filename = cover_filename

for part in volume.parts:
if not part.images:
continue

for image in part.images:
if image.url == cover_image.url:
image.local_filename = cover_filename
break


async def _write_bytes(filepath, content):
Expand Down
2 changes: 1 addition & 1 deletion jncep/epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def output_epub(output_filepath, book_details: BookDetails, style_css_path=None)
if book_details.cover_image:
content = book_details.cover_image.content
# in case cover image also present in content, use the file name
# (same URL => same local filename)
# (same URL => same local filename or cover.jpg (renamed in core.py))
cover_image_filename = book_details.cover_image.local_filename
else:
# the lib handles that semi-gracefully (doesn't crash)
Expand Down
1 change: 0 additions & 1 deletion jncep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Part:
class Image:
url = attr.ib()
content = attr.ib(None)
part = attr.ib(None)
local_filename = attr.ib(None)

order_in_part = attr.ib(None)

0 comments on commit 1deef1d

Please sign in to comment.