Skip to content

Commit b88904a

Browse files
committed
Improve the display order algorithm
1 parent eb8eb64 commit b88904a

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

comiclib/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from .scan import watch, scannow
55
from .config import settings
6-
from .utils import is_image, extract_thumbnail, convert_image, ArchiveFile
6+
from .utils import is_image, extract_thumbnail, convert_image, ArchiveFile, ordered
77
from typing import Union, Annotated
88
from enum import Enum
99
from pathlib import Path
@@ -285,10 +285,10 @@ def extract_archive(id: str, force: bool = True, db: Session = Depends(get_db)):
285285
return JSONResponse({"operation": "", "error": "This ID doesn't exist on the server.", "success": 0}, status.HTTP_400_BAD_REQUEST)
286286
path = Path(settings.content) / a.path
287287
if path.is_dir():
288-
pages = [f"./api/archives/{id}/page?path="+quote(p.name, safe='') for p in sorted(path.iterdir()) if is_image(p)]
288+
pages = [f"./api/archives/{id}/page?path="+quote(p.name, safe='') for p in ordered(path.iterdir()) if is_image(p)]
289289
elif ArchiveFile.support_formats.fullmatch(path.name):
290290
with ArchiveFile(path) as z:
291-
pages = [f"./api/archives/{id}/page?path="+quote(filename, safe='') for filename in sorted(map(lambda z_info: z_info.filename, filter(lambda z_info: not z_info.is_dir() and is_image(z_info.filename), z.infolist())))]
291+
pages = [f"./api/archives/{id}/page?path="+quote(filename, safe='') for filename in ordered(map(lambda z_info: z_info.filename, filter(lambda z_info: not z_info.is_dir() and is_image(z_info.filename), z.infolist())))]
292292
else:
293293
raise NotImplementedError
294294
return {"job": -1, "pages": pages}

comiclib/utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Union, Iterable
22
from pathlib import Path
33
from zipfile import ZipFile
44
import io
@@ -21,6 +21,16 @@
2121
from .config import settings
2222

2323

24+
def ordered(iterable: Iterable[str]) -> list[str]:
25+
unsorted = tuple(iterable)
26+
try:
27+
unsorted_path = tuple(Path(s) for s in unsorted)
28+
if any(unsorted_path[0].parent != p.parent for p in unsorted_path[1:]):
29+
raise ValueError
30+
return sorted(unsorted, key=lambda s: int(Path(s).stem))
31+
except ValueError:
32+
return sorted(unsorted)
33+
2434
mimetypes.add_type('image/jxl', '.jxl')
2535
def is_image(p: Union[str, Path]):
2636
if Path(p).parts[0] == '__MACOSX': return False
@@ -53,10 +63,10 @@ def extract_thumbnail(path: Union[str, Path], id: str, page: int, cache=False, c
5363
return saveto.relative_to(settings.thumb)
5464
saveto.parent.mkdir(parents=True, exist_ok=True)
5565
if path.is_dir():
56-
convert_image(sorted(filter(is_image, path.iterdir()))[page-1], saveto, thumbnail=True)
66+
convert_image(ordered(filter(is_image, path.iterdir()))[page-1], saveto, thumbnail=True)
5767
elif ArchiveFile.support_formats.fullmatch(path.name):
5868
with ArchiveFile(path) as z:
59-
with z.open(sorted(map(lambda z_info: z_info.filename, filter(lambda z_info: not z_info.is_dir() and is_image(z_info.filename), z.infolist())))[page-1]) as f:
69+
with z.open(ordered(map(lambda z_info: z_info.filename, filter(lambda z_info: not z_info.is_dir() and is_image(z_info.filename), z.infolist())))[page-1]) as f:
6070
convert_image(f, saveto, thumbnail=True)
6171
else:
6272
raise NotImplementedError

0 commit comments

Comments
 (0)