Skip to content

Commit b82f84f

Browse files
committed
add option cover
1 parent b92891a commit b82f84f

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

comiclib/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class Settings(BaseSettings):
99
debug: bool = False
1010
loglevel: str = 'INFO'
1111
content: str = '.'
12-
thumb: str = './thumb'
12+
cover: str = './thumb'
13+
thumb: Union[str, None] = None
1314
metadata: str = 'sqlite:///./comiclib_metadata.db'
1415
password: Union[str, None] = None
1516
skip_exits: bool = True
@@ -31,3 +32,5 @@ class Settings(BaseSettings):
3132

3233
settings.UA_convert_jxl = re.compile(settings.UA_convert_jxl)
3334
settings.UA_convert_all = re.compile(settings.UA_convert_all)
35+
if settings.thumb is None:
36+
settings.thumb = settings.cover

comiclib/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def get_archive_thumbnail(id: str, background_tasks: BackgroundTasks, response:
231231
if a.thumb.startswith('http'):
232232
return RedirectResponse(a.thumb)
233233
else:
234-
thumb_path = Path(settings.thumb) / a.thumb
234+
thumb_path = Path(settings.cover) / a.thumb
235235
else:
236236
thumb_path = Path(settings.thumb) / extract_thumbnail(Path(settings.content) / a.path, id, page, cache=True)
237237
return FileResponse(thumb_path)
@@ -242,7 +242,7 @@ def update_thumbnail(id: str, page: int = 1, db: Session = Depends(get_db)):
242242
a = db.get(Archive, id)
243243
if a is None:
244244
return JSONResponse({"operation": "", "error": "This ID doesn't exist on the server.", "success": 0}, status.HTTP_400_BAD_REQUEST)
245-
thumb_path = extract_thumbnail(Path(settings.content) / a.path, a.id, page)
245+
thumb_path = extract_thumbnail(Path(settings.content) / a.path, a.id, page, cover=True)
246246
a.thumb = str(thumb_path)
247247
db.commit()
248248
return {

comiclib/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@ def convert_image(f_or_path, saveto: str, thumbnail=False):
3333
subprocess.run(cmd, check=True, stderr=None if settings.debug else subprocess.DEVNULL)
3434

3535

36-
def extract_thumbnail(path: Union[str, Path], id: str, page: int, cache=False) -> Path:
36+
def extract_thumbnail(path: Union[str, Path], id: str, page: int, cache=False, cover=False) -> Path:
3737
path = Path(path)
38-
saveto = Path(settings.thumb) / (id+'.webp') if page == 1 else Path(settings.thumb) / id / f'{page}.webp'
38+
saveto = Path(settings.thumb) / id / f'{page}.webp'
3939
if cache and saveto.exists():
4040
return saveto.relative_to(settings.thumb)
4141
saveto.parent.mkdir(parents=True, exist_ok=True)
4242
if path.is_dir():
4343
convert_image(sorted(filter(lambda p:p.suffix != '.txt' and not p.name.startswith('.'), path.iterdir()))[page-1], saveto, thumbnail=True)
44-
return saveto.relative_to(settings.thumb)
4544
elif path.suffix == '.zip':
4645
with ZipFile(path) as z:
4746
with z.open(list(filter(lambda z_info: not z_info.is_dir(), z.infolist()))[page-1].filename) as f:
4847
convert_image(f, saveto, thumbnail=True)
49-
return saveto.relative_to(settings.thumb)
5048
else:
51-
raise NotImplementedError
49+
raise NotImplementedError
50+
if cover:
51+
cover_path = Path(settings.cover) / f'{id}.webp'
52+
cover_path.parent.mkdir(parents=True, exist_ok=True)
53+
shutil.copy2(saveto, cover_path)
54+
return cover_path.name
55+
return saveto.relative_to(settings.thumb)

0 commit comments

Comments
 (0)