2021-03-20 10:32:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
2021-09-29 07:40:12 +00:00
|
|
|
# Copyright (C) 2020 monkey
|
2021-03-20 10:32:50 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
2023-03-20 18:00:35 +00:00
|
|
|
from shutil import copyfile, copyfileobj
|
2022-02-08 19:15:01 +00:00
|
|
|
from urllib.request import urlopen
|
2024-07-02 18:53:08 +00:00
|
|
|
from io import BytesIO
|
2024-08-10 19:05:29 +00:00
|
|
|
from datetime import datetime, timezone
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-25 08:04:38 +00:00
|
|
|
from .. import constants
|
2024-11-26 05:13:10 +00:00
|
|
|
from cps import config, db, fs, gdriveutils, logger, ub, app
|
2021-09-29 07:40:12 +00:00
|
|
|
from cps.services.worker import CalibreTask, STAT_CANCELLED, STAT_ENDED
|
2021-09-25 08:04:38 +00:00
|
|
|
from sqlalchemy import func, text, or_
|
2022-04-24 16:40:50 +00:00
|
|
|
from flask_babel import lazy_gettext as N_
|
2024-11-26 05:13:10 +00:00
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
try:
|
|
|
|
from wand.image import Image
|
|
|
|
use_IM = True
|
|
|
|
except (ImportError, RuntimeError) as e:
|
|
|
|
use_IM = False
|
|
|
|
|
2021-09-25 08:04:38 +00:00
|
|
|
|
|
|
|
def get_resize_height(resolution):
|
2024-08-15 16:46:41 +00:00
|
|
|
return int(255 * resolution)
|
2021-09-25 08:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_resize_width(resolution, original_width, original_height):
|
|
|
|
height = get_resize_height(resolution)
|
|
|
|
percent = (height / float(original_height))
|
|
|
|
width = int((float(original_width) * float(percent)))
|
|
|
|
return width if width % 2 == 0 else width + 1
|
|
|
|
|
|
|
|
|
|
|
|
def get_best_fit(width, height, image_width, image_height):
|
|
|
|
resize_width = int(width / 2.0)
|
|
|
|
resize_height = int(height / 2.0)
|
|
|
|
aspect_ratio = image_width / image_height
|
|
|
|
|
2022-04-23 17:56:02 +00:00
|
|
|
# If this image's aspect ratio is different from the first image, then resize this image
|
2021-09-25 08:04:38 +00:00
|
|
|
# to fill the width and height of the first image
|
|
|
|
if aspect_ratio < width / height:
|
|
|
|
resize_width = int(width / 2.0)
|
|
|
|
resize_height = image_height * int(width / 2.0) / image_width
|
|
|
|
|
|
|
|
elif aspect_ratio > width / height:
|
|
|
|
resize_width = image_width * int(height / 2.0) / image_height
|
|
|
|
resize_height = int(height / 2.0)
|
|
|
|
|
|
|
|
return {'width': resize_width, 'height': resize_height}
|
2021-03-20 10:32:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TaskGenerateCoverThumbnails(CalibreTask):
|
2022-04-22 14:13:51 +00:00
|
|
|
def __init__(self, book_id=-1, task_message=''):
|
2021-03-20 10:32:50 +00:00
|
|
|
super(TaskGenerateCoverThumbnails, self).__init__(task_message)
|
|
|
|
self.log = logger.create()
|
2022-04-22 14:13:51 +00:00
|
|
|
self.book_id = book_id
|
2021-03-20 10:32:50 +00:00
|
|
|
self.app_db_session = ub.get_new_session_instance()
|
|
|
|
self.cache = fs.FileSystem()
|
|
|
|
self.resolutions = [
|
2021-09-25 08:04:38 +00:00
|
|
|
constants.COVER_THUMBNAIL_SMALL,
|
2024-08-15 16:46:41 +00:00
|
|
|
constants.COVER_THUMBNAIL_MEDIUM,
|
|
|
|
constants.COVER_THUMBNAIL_LARGE
|
2021-03-20 10:32:50 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def run(self, worker_thread):
|
2022-04-30 06:26:00 +00:00
|
|
|
if use_IM and self.stat != STAT_CANCELLED and self.stat != STAT_ENDED:
|
2021-09-29 07:40:12 +00:00
|
|
|
self.message = 'Scanning Books'
|
2022-04-22 18:31:03 +00:00
|
|
|
books_with_covers = self.get_books_with_covers(self.book_id)
|
2021-09-24 08:11:14 +00:00
|
|
|
count = len(books_with_covers)
|
|
|
|
|
2021-09-29 07:40:12 +00:00
|
|
|
total_generated = 0
|
2021-09-24 08:11:14 +00:00
|
|
|
for i, book in enumerate(books_with_covers):
|
|
|
|
|
|
|
|
# Generate new thumbnails for missing covers
|
2022-04-22 14:13:51 +00:00
|
|
|
generated = self.create_book_cover_thumbnails(book)
|
2021-09-24 08:11:14 +00:00
|
|
|
|
2021-09-29 07:40:12 +00:00
|
|
|
# Increment the progress
|
2021-03-20 10:32:50 +00:00
|
|
|
self.progress = (1.0 / count) * i
|
|
|
|
|
2021-09-29 07:40:12 +00:00
|
|
|
if generated > 0:
|
|
|
|
total_generated += generated
|
2023-01-21 14:23:18 +00:00
|
|
|
self.message = N_('Generated %(count)s cover thumbnails', count=total_generated)
|
2021-09-29 07:40:12 +00:00
|
|
|
|
|
|
|
# Check if job has been cancelled or ended
|
|
|
|
if self.stat == STAT_CANCELLED:
|
|
|
|
self.log.info(f'GenerateCoverThumbnails task has been cancelled.')
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.stat == STAT_ENDED:
|
|
|
|
self.log.info(f'GenerateCoverThumbnails task has been ended.')
|
|
|
|
return
|
|
|
|
|
|
|
|
if total_generated == 0:
|
|
|
|
self.self_cleanup = True
|
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
self._handleSuccess()
|
|
|
|
self.app_db_session.remove()
|
|
|
|
|
2024-06-18 18:13:26 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_books_with_covers(book_id=-1):
|
2022-04-22 18:31:03 +00:00
|
|
|
filter_exp = (db.Books.id == book_id) if book_id != -1 else True
|
2024-11-26 05:13:10 +00:00
|
|
|
with app.app_context():
|
|
|
|
calibre_db = db.CalibreDB(app) #, expire_on_commit=False, init=True)
|
|
|
|
books_cover = calibre_db.session.query(db.Books).filter(db.Books.has_cover == 1).filter(filter_exp).all()
|
|
|
|
# calibre_db.session.close()
|
2022-04-30 06:26:00 +00:00
|
|
|
return books_cover
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-24 08:11:14 +00:00
|
|
|
def get_book_cover_thumbnails(self, book_id):
|
2021-09-25 08:04:38 +00:00
|
|
|
return self.app_db_session \
|
|
|
|
.query(ub.Thumbnail) \
|
|
|
|
.filter(ub.Thumbnail.type == constants.THUMBNAIL_TYPE_COVER) \
|
|
|
|
.filter(ub.Thumbnail.entity_id == book_id) \
|
2024-08-10 19:05:29 +00:00
|
|
|
.filter(or_(ub.Thumbnail.expiration.is_(None), ub.Thumbnail.expiration > datetime.now(timezone.utc))) \
|
2021-09-24 08:11:14 +00:00
|
|
|
.all()
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2022-04-22 14:13:51 +00:00
|
|
|
def create_book_cover_thumbnails(self, book):
|
|
|
|
generated = 0
|
|
|
|
book_cover_thumbnails = self.get_book_cover_thumbnails(book.id)
|
|
|
|
|
|
|
|
# Generate new thumbnails for missing covers
|
|
|
|
resolutions = list(map(lambda t: t.resolution, book_cover_thumbnails))
|
|
|
|
missing_resolutions = list(set(self.resolutions).difference(resolutions))
|
|
|
|
for resolution in missing_resolutions:
|
|
|
|
generated += 1
|
|
|
|
self.create_book_cover_single_thumbnail(book, resolution)
|
|
|
|
|
|
|
|
# Replace outdated or missing thumbnails
|
|
|
|
for thumbnail in book_cover_thumbnails:
|
2023-04-15 11:22:17 +00:00
|
|
|
if book.last_modified.replace(tzinfo=None) > thumbnail.generated_at:
|
2022-04-22 14:13:51 +00:00
|
|
|
generated += 1
|
|
|
|
self.update_book_cover_thumbnail(book, thumbnail)
|
|
|
|
|
|
|
|
elif not self.cache.get_cache_file_exists(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS):
|
|
|
|
generated += 1
|
|
|
|
self.update_book_cover_thumbnail(book, thumbnail)
|
|
|
|
return generated
|
|
|
|
|
|
|
|
def create_book_cover_single_thumbnail(self, book, resolution):
|
2021-03-20 10:32:50 +00:00
|
|
|
thumbnail = ub.Thumbnail()
|
2021-09-25 08:04:38 +00:00
|
|
|
thumbnail.type = constants.THUMBNAIL_TYPE_COVER
|
|
|
|
thumbnail.entity_id = book.id
|
2021-03-20 10:32:50 +00:00
|
|
|
thumbnail.format = 'jpeg'
|
|
|
|
thumbnail.resolution = resolution
|
|
|
|
|
|
|
|
self.app_db_session.add(thumbnail)
|
|
|
|
try:
|
|
|
|
self.app_db_session.commit()
|
|
|
|
self.generate_book_thumbnail(book, thumbnail)
|
|
|
|
except Exception as ex:
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error creating book thumbnail: ' + str(ex))
|
2022-04-24 16:40:50 +00:00
|
|
|
self._handleError('Error creating book thumbnail: ' + str(ex))
|
2021-03-20 10:32:50 +00:00
|
|
|
self.app_db_session.rollback()
|
|
|
|
|
2021-09-24 08:11:14 +00:00
|
|
|
def update_book_cover_thumbnail(self, book, thumbnail):
|
2024-08-10 19:05:29 +00:00
|
|
|
thumbnail.generated_at = datetime.now(timezone.utc)
|
2021-09-24 08:11:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.app_db_session.commit()
|
2021-09-25 08:04:38 +00:00
|
|
|
self.cache.delete_cache_file(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS)
|
2021-09-24 08:11:14 +00:00
|
|
|
self.generate_book_thumbnail(book, thumbnail)
|
|
|
|
except Exception as ex:
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error updating book thumbnail: ' + str(ex))
|
2022-04-24 16:40:50 +00:00
|
|
|
self._handleError('Error updating book thumbnail: ' + str(ex))
|
2021-09-24 08:11:14 +00:00
|
|
|
self.app_db_session.rollback()
|
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
def generate_book_thumbnail(self, book, thumbnail):
|
|
|
|
if book and thumbnail:
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
if not gdriveutils.is_gdrive_ready():
|
|
|
|
raise Exception('Google Drive is configured but not ready')
|
|
|
|
|
2024-07-02 18:53:08 +00:00
|
|
|
content = gdriveutils.get_cover_via_gdrive(book.path)
|
|
|
|
if not content:
|
2021-03-20 10:32:50 +00:00
|
|
|
raise Exception('Google Drive cover url not found')
|
|
|
|
try:
|
2024-07-02 18:53:08 +00:00
|
|
|
stream = BytesIO(content)
|
2021-03-20 10:32:50 +00:00
|
|
|
with Image(file=stream) as img:
|
2023-03-20 18:00:35 +00:00
|
|
|
filename = self.cache.get_cache_file_path(thumbnail.filename,
|
|
|
|
constants.CACHE_TYPE_THUMBNAILS)
|
2021-09-26 07:02:48 +00:00
|
|
|
height = get_resize_height(thumbnail.resolution)
|
2021-03-20 10:32:50 +00:00
|
|
|
if img.height > height:
|
2021-09-26 07:02:48 +00:00
|
|
|
width = get_resize_width(thumbnail.resolution, img.width, img.height)
|
2021-03-20 10:32:50 +00:00
|
|
|
img.resize(width=width, height=height, filter='lanczos')
|
|
|
|
img.format = thumbnail.format
|
|
|
|
img.save(filename=filename)
|
2023-03-20 18:00:35 +00:00
|
|
|
else:
|
2024-08-17 07:28:31 +00:00
|
|
|
stream.seek(0)
|
|
|
|
with open(filename, 'wb') as fd:
|
2023-03-20 18:00:35 +00:00
|
|
|
copyfileobj(stream, fd)
|
|
|
|
|
2024-08-17 07:28:31 +00:00
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
except Exception as ex:
|
|
|
|
# Bubble exception to calling function
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error generating thumbnail file: ' + str(ex))
|
2021-03-20 10:32:50 +00:00
|
|
|
raise ex
|
|
|
|
finally:
|
2021-09-29 07:40:12 +00:00
|
|
|
if stream is not None:
|
|
|
|
stream.close()
|
2021-03-20 10:32:50 +00:00
|
|
|
else:
|
2023-11-07 18:30:13 +00:00
|
|
|
book_cover_filepath = os.path.join(config.get_book_path(), book.path, 'cover.jpg')
|
2021-03-20 10:32:50 +00:00
|
|
|
if not os.path.isfile(book_cover_filepath):
|
|
|
|
raise Exception('Book cover file not found')
|
|
|
|
|
|
|
|
with Image(filename=book_cover_filepath) as img:
|
2021-09-25 08:04:38 +00:00
|
|
|
height = get_resize_height(thumbnail.resolution)
|
2023-03-20 18:00:35 +00:00
|
|
|
filename = self.cache.get_cache_file_path(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS)
|
2021-03-20 10:32:50 +00:00
|
|
|
if img.height > height:
|
2021-09-25 08:04:38 +00:00
|
|
|
width = get_resize_width(thumbnail.resolution, img.width, img.height)
|
2021-03-20 10:32:50 +00:00
|
|
|
img.resize(width=width, height=height, filter='lanczos')
|
|
|
|
img.format = thumbnail.format
|
|
|
|
img.save(filename=filename)
|
2023-03-20 18:00:35 +00:00
|
|
|
else:
|
|
|
|
# take cover as is
|
|
|
|
copyfile(book_cover_filepath, filename)
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-25 08:04:38 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2022-04-24 16:40:50 +00:00
|
|
|
return N_('Cover Thumbnails')
|
2021-09-29 07:40:12 +00:00
|
|
|
|
2022-03-16 19:31:25 +00:00
|
|
|
def __str__(self):
|
2022-04-22 18:31:03 +00:00
|
|
|
if self.book_id > 0:
|
2022-04-23 17:56:02 +00:00
|
|
|
return "Add Cover Thumbnails for Book {}".format(self.book_id)
|
2022-04-22 18:31:03 +00:00
|
|
|
else:
|
|
|
|
return "Generate Cover Thumbnails"
|
2022-03-16 19:31:25 +00:00
|
|
|
|
2021-09-29 07:40:12 +00:00
|
|
|
@property
|
|
|
|
def is_cancellable(self):
|
|
|
|
return True
|
2021-09-25 08:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TaskGenerateSeriesThumbnails(CalibreTask):
|
2021-09-29 07:40:12 +00:00
|
|
|
def __init__(self, task_message=''):
|
2021-09-25 08:04:38 +00:00
|
|
|
super(TaskGenerateSeriesThumbnails, self).__init__(task_message)
|
|
|
|
self.log = logger.create()
|
|
|
|
self.app_db_session = ub.get_new_session_instance()
|
2024-11-26 05:13:10 +00:00
|
|
|
# self.calibre_db = db.CalibreDB(expire_on_commit=False, init=True)
|
2021-09-25 08:04:38 +00:00
|
|
|
self.cache = fs.FileSystem()
|
|
|
|
self.resolutions = [
|
|
|
|
constants.COVER_THUMBNAIL_SMALL,
|
2021-09-29 07:40:12 +00:00
|
|
|
constants.COVER_THUMBNAIL_MEDIUM,
|
2021-09-25 08:04:38 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def run(self, worker_thread):
|
2024-11-26 05:13:10 +00:00
|
|
|
with app.app_context():
|
|
|
|
calibre_db = db.CalibreDB(app)
|
|
|
|
if calibre_db.session and use_IM and self.stat != STAT_CANCELLED and self.stat != STAT_ENDED:
|
|
|
|
self.message = 'Scanning Series'
|
|
|
|
all_series = self.get_series_with_four_plus_books(calibre_db)
|
|
|
|
count = len(all_series)
|
|
|
|
|
|
|
|
total_generated = 0
|
|
|
|
for i, series in enumerate(all_series):
|
|
|
|
generated = 0
|
|
|
|
series_thumbnails = self.get_series_thumbnails(series.id)
|
|
|
|
series_books = self.get_series_books(series.id, calibre_db)
|
|
|
|
|
|
|
|
# Generate new thumbnails for missing covers
|
|
|
|
resolutions = list(map(lambda t: t.resolution, series_thumbnails))
|
|
|
|
missing_resolutions = list(set(self.resolutions).difference(resolutions))
|
|
|
|
for resolution in missing_resolutions:
|
2021-09-29 07:40:12 +00:00
|
|
|
generated += 1
|
2024-11-26 05:13:10 +00:00
|
|
|
self.create_series_thumbnail(series, series_books, resolution)
|
2021-09-25 08:04:38 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
# Replace outdated or missing thumbnails
|
|
|
|
for thumbnail in series_thumbnails:
|
|
|
|
if any(book.last_modified > thumbnail.generated_at for book in series_books):
|
|
|
|
generated += 1
|
|
|
|
self.update_series_thumbnail(series_books, thumbnail)
|
2021-09-25 08:04:38 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
elif not self.cache.get_cache_file_exists(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS):
|
|
|
|
generated += 1
|
|
|
|
self.update_series_thumbnail(series_books, thumbnail)
|
2021-09-25 08:04:38 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
# Increment the progress
|
|
|
|
self.progress = (1.0 / count) * i
|
2021-09-29 07:40:12 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
if generated > 0:
|
|
|
|
total_generated += generated
|
|
|
|
self.message = N_('Generated {0} series thumbnails').format(total_generated)
|
2021-09-29 07:40:12 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
# Check if job has been cancelled or ended
|
|
|
|
if self.stat == STAT_CANCELLED:
|
|
|
|
self.log.info(f'GenerateSeriesThumbnails task has been cancelled.')
|
|
|
|
return
|
2021-09-29 07:40:12 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
if self.stat == STAT_ENDED:
|
|
|
|
self.log.info(f'GenerateSeriesThumbnails task has been ended.')
|
|
|
|
return
|
2021-09-29 07:40:12 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
if total_generated == 0:
|
|
|
|
self.self_cleanup = True
|
|
|
|
|
|
|
|
self._handleSuccess()
|
|
|
|
self.app_db_session.remove()
|
2021-09-25 08:04:38 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
def get_series_with_four_plus_books(self, calibre_db):
|
|
|
|
return calibre_db.session \
|
2021-09-25 08:04:38 +00:00
|
|
|
.query(db.Series) \
|
|
|
|
.join(db.books_series_link) \
|
|
|
|
.join(db.Books) \
|
|
|
|
.filter(db.Books.has_cover == 1) \
|
|
|
|
.group_by(text('books_series_link.series')) \
|
|
|
|
.having(func.count('book_series_link') > 3) \
|
|
|
|
.all()
|
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
def get_series_books(self, series_id, calibre_db):
|
|
|
|
return calibre_db.session \
|
2021-09-25 08:04:38 +00:00
|
|
|
.query(db.Books) \
|
|
|
|
.join(db.books_series_link) \
|
|
|
|
.join(db.Series) \
|
|
|
|
.filter(db.Books.has_cover == 1) \
|
|
|
|
.filter(db.Series.id == series_id) \
|
|
|
|
.all()
|
|
|
|
|
|
|
|
def get_series_thumbnails(self, series_id):
|
2024-08-10 16:09:52 +00:00
|
|
|
return (self.app_db_session
|
|
|
|
.query(ub.Thumbnail)
|
|
|
|
.filter(ub.Thumbnail.type == constants.THUMBNAIL_TYPE_SERIES)
|
|
|
|
.filter(ub.Thumbnail.entity_id == series_id)
|
2024-08-10 19:05:29 +00:00
|
|
|
.filter(or_(ub.Thumbnail.expiration.is_(None), ub.Thumbnail.expiration > datetime.now(timezone.utc)))
|
2024-08-10 16:09:52 +00:00
|
|
|
.all())
|
2021-09-25 08:04:38 +00:00
|
|
|
|
|
|
|
def create_series_thumbnail(self, series, series_books, resolution):
|
|
|
|
thumbnail = ub.Thumbnail()
|
|
|
|
thumbnail.type = constants.THUMBNAIL_TYPE_SERIES
|
|
|
|
thumbnail.entity_id = series.id
|
|
|
|
thumbnail.format = 'jpeg'
|
|
|
|
thumbnail.resolution = resolution
|
|
|
|
|
|
|
|
self.app_db_session.add(thumbnail)
|
|
|
|
try:
|
|
|
|
self.app_db_session.commit()
|
|
|
|
self.generate_series_thumbnail(series_books, thumbnail)
|
|
|
|
except Exception as ex:
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error creating book thumbnail: ' + str(ex))
|
2022-04-24 16:40:50 +00:00
|
|
|
self._handleError('Error creating book thumbnail: ' + str(ex))
|
2021-09-25 08:04:38 +00:00
|
|
|
self.app_db_session.rollback()
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-25 08:04:38 +00:00
|
|
|
def update_series_thumbnail(self, series_books, thumbnail):
|
2024-08-10 19:05:29 +00:00
|
|
|
thumbnail.generated_at = datetime.now(timezone.utc)
|
2021-09-25 08:04:38 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.app_db_session.commit()
|
|
|
|
self.cache.delete_cache_file(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS)
|
|
|
|
self.generate_series_thumbnail(series_books, thumbnail)
|
|
|
|
except Exception as ex:
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error updating book thumbnail: ' + str(ex))
|
2022-04-24 16:40:50 +00:00
|
|
|
self._handleError('Error updating book thumbnail: ' + str(ex))
|
2021-09-25 08:04:38 +00:00
|
|
|
self.app_db_session.rollback()
|
|
|
|
|
|
|
|
def generate_series_thumbnail(self, series_books, thumbnail):
|
2021-09-29 07:40:12 +00:00
|
|
|
# Get the last four books in the series based on series_index
|
|
|
|
books = sorted(series_books, key=lambda b: float(b.series_index), reverse=True)[:4]
|
2021-09-25 08:04:38 +00:00
|
|
|
|
|
|
|
top = 0
|
|
|
|
left = 0
|
|
|
|
width = 0
|
|
|
|
height = 0
|
|
|
|
with Image() as canvas:
|
|
|
|
for book in books:
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
if not gdriveutils.is_gdrive_ready():
|
|
|
|
raise Exception('Google Drive is configured but not ready')
|
|
|
|
|
|
|
|
web_content_link = gdriveutils.get_cover_via_gdrive(book.path)
|
|
|
|
if not web_content_link:
|
|
|
|
raise Exception('Google Drive cover url not found')
|
|
|
|
|
|
|
|
stream = None
|
|
|
|
try:
|
|
|
|
stream = urlopen(web_content_link)
|
|
|
|
with Image(file=stream) as img:
|
|
|
|
# Use the first image in this set to determine the width and height to scale the
|
|
|
|
# other images in this set
|
|
|
|
if width == 0 or height == 0:
|
|
|
|
width = get_resize_width(thumbnail.resolution, img.width, img.height)
|
|
|
|
height = get_resize_height(thumbnail.resolution)
|
|
|
|
canvas.blank(width, height)
|
|
|
|
|
|
|
|
dimensions = get_best_fit(width, height, img.width, img.height)
|
|
|
|
|
|
|
|
# resize and crop the image
|
2022-02-08 19:15:01 +00:00
|
|
|
img.resize(width=int(dimensions['width']), height=int(dimensions['height']),
|
|
|
|
filter='lanczos')
|
2021-09-25 08:04:38 +00:00
|
|
|
img.crop(width=int(width / 2.0), height=int(height / 2.0), gravity='center')
|
|
|
|
|
|
|
|
# add the image to the canvas
|
|
|
|
canvas.composite(img, left, top)
|
|
|
|
|
|
|
|
except Exception as ex:
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error generating thumbnail file: ' + str(ex))
|
2021-09-25 08:04:38 +00:00
|
|
|
raise ex
|
|
|
|
finally:
|
2021-09-29 07:40:12 +00:00
|
|
|
if stream is not None:
|
|
|
|
stream.close()
|
2021-09-25 08:04:38 +00:00
|
|
|
|
2023-11-07 18:30:13 +00:00
|
|
|
book_cover_filepath = os.path.join(config.get_book_path(), book.path, 'cover.jpg')
|
2021-09-25 08:04:38 +00:00
|
|
|
if not os.path.isfile(book_cover_filepath):
|
|
|
|
raise Exception('Book cover file not found')
|
|
|
|
|
|
|
|
with Image(filename=book_cover_filepath) as img:
|
|
|
|
# Use the first image in this set to determine the width and height to scale the
|
|
|
|
# other images in this set
|
|
|
|
if width == 0 or height == 0:
|
|
|
|
width = get_resize_width(thumbnail.resolution, img.width, img.height)
|
|
|
|
height = get_resize_height(thumbnail.resolution)
|
|
|
|
canvas.blank(width, height)
|
|
|
|
|
|
|
|
dimensions = get_best_fit(width, height, img.width, img.height)
|
|
|
|
|
|
|
|
# resize and crop the image
|
|
|
|
img.resize(width=int(dimensions['width']), height=int(dimensions['height']), filter='lanczos')
|
|
|
|
img.crop(width=int(width / 2.0), height=int(height / 2.0), gravity='center')
|
|
|
|
|
|
|
|
# add the image to the canvas
|
|
|
|
canvas.composite(img, left, top)
|
|
|
|
|
|
|
|
# set the coordinates for the next iteration
|
|
|
|
if left == 0 and top == 0:
|
|
|
|
left = int(width / 2.0)
|
|
|
|
elif left == int(width / 2.0) and top == 0:
|
|
|
|
left = 0
|
|
|
|
top = int(height / 2.0)
|
|
|
|
else:
|
|
|
|
left = int(width / 2.0)
|
|
|
|
|
|
|
|
canvas.format = thumbnail.format
|
|
|
|
filename = self.cache.get_cache_file_path(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS)
|
|
|
|
canvas.save(filename=filename)
|
2021-03-20 10:32:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2022-04-24 16:40:50 +00:00
|
|
|
return N_('Cover Thumbnails')
|
2021-09-29 07:40:12 +00:00
|
|
|
|
2022-03-16 19:31:25 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "GenerateSeriesThumbnails"
|
|
|
|
|
2021-09-29 07:40:12 +00:00
|
|
|
@property
|
|
|
|
def is_cancellable(self):
|
|
|
|
return True
|
2021-03-20 10:32:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TaskClearCoverThumbnailCache(CalibreTask):
|
2022-04-24 16:40:50 +00:00
|
|
|
def __init__(self, book_id, task_message=N_('Clearing cover thumbnail cache')):
|
2021-03-20 10:32:50 +00:00
|
|
|
super(TaskClearCoverThumbnailCache, self).__init__(task_message)
|
|
|
|
self.log = logger.create()
|
|
|
|
self.book_id = book_id
|
|
|
|
self.app_db_session = ub.get_new_session_instance()
|
|
|
|
self.cache = fs.FileSystem()
|
|
|
|
|
|
|
|
def run(self, worker_thread):
|
|
|
|
if self.app_db_session:
|
2024-11-26 05:13:10 +00:00
|
|
|
# delete superfluous thumbnails
|
|
|
|
if self.book_id == 0:
|
|
|
|
with app.app_context():
|
|
|
|
calibre_db = db.CalibreDB(app)
|
|
|
|
thumbnails = (calibre_db.session.query(ub.Thumbnail)
|
|
|
|
.join(db.Books, ub.Thumbnail.entity_id == db.Books.id, isouter=True)
|
|
|
|
.filter(db.Books.id==None)
|
|
|
|
.all())
|
|
|
|
# calibre_db.session.close()
|
2022-04-24 09:14:39 +00:00
|
|
|
elif self.book_id > 0: # make sure single book is selected
|
2021-03-20 10:32:50 +00:00
|
|
|
thumbnails = self.get_thumbnails_for_book(self.book_id)
|
2022-04-24 09:14:39 +00:00
|
|
|
if self.book_id < 0:
|
|
|
|
self.delete_all_thumbnails()
|
|
|
|
else:
|
2021-03-20 10:32:50 +00:00
|
|
|
for thumbnail in thumbnails:
|
2021-09-24 08:11:14 +00:00
|
|
|
self.delete_thumbnail(thumbnail)
|
2021-03-20 10:32:50 +00:00
|
|
|
self._handleSuccess()
|
|
|
|
self.app_db_session.remove()
|
|
|
|
|
|
|
|
def get_thumbnails_for_book(self, book_id):
|
2021-09-25 08:04:38 +00:00
|
|
|
return self.app_db_session \
|
|
|
|
.query(ub.Thumbnail) \
|
|
|
|
.filter(ub.Thumbnail.type == constants.THUMBNAIL_TYPE_COVER) \
|
|
|
|
.filter(ub.Thumbnail.entity_id == book_id) \
|
2021-03-20 10:32:50 +00:00
|
|
|
.all()
|
|
|
|
|
2021-09-24 08:11:14 +00:00
|
|
|
def delete_thumbnail(self, thumbnail):
|
2021-03-20 10:32:50 +00:00
|
|
|
try:
|
2021-09-25 08:04:38 +00:00
|
|
|
self.cache.delete_cache_file(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS)
|
2022-03-16 19:31:25 +00:00
|
|
|
self.app_db_session \
|
|
|
|
.query(ub.Thumbnail) \
|
|
|
|
.filter(ub.Thumbnail.type == constants.THUMBNAIL_TYPE_COVER) \
|
|
|
|
.filter(ub.Thumbnail.entity_id == thumbnail.entity_id) \
|
|
|
|
.delete()
|
|
|
|
self.app_db_session.commit()
|
2021-03-20 10:32:50 +00:00
|
|
|
except Exception as ex:
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error deleting book thumbnail: ' + str(ex))
|
2022-04-24 16:40:50 +00:00
|
|
|
self._handleError('Error deleting book thumbnail: ' + str(ex))
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2022-03-16 19:31:25 +00:00
|
|
|
def delete_all_thumbnails(self):
|
|
|
|
try:
|
|
|
|
self.app_db_session.query(ub.Thumbnail).filter(ub.Thumbnail.type == constants.THUMBNAIL_TYPE_COVER).delete()
|
|
|
|
self.app_db_session.commit()
|
|
|
|
self.cache.delete_cache_dir(constants.CACHE_TYPE_THUMBNAILS)
|
|
|
|
except Exception as ex:
|
2022-04-30 06:26:00 +00:00
|
|
|
self.log.debug('Error deleting thumbnail directory: ' + str(ex))
|
2022-04-24 16:40:50 +00:00
|
|
|
self._handleError('Error deleting thumbnail directory: ' + str(ex))
|
2022-03-16 19:31:25 +00:00
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2022-04-24 16:40:50 +00:00
|
|
|
return N_('Cover Thumbnails')
|
2021-09-29 07:40:12 +00:00
|
|
|
|
2022-04-22 18:31:03 +00:00
|
|
|
# needed for logging
|
2022-03-16 19:33:11 +00:00
|
|
|
def __str__(self):
|
2022-03-20 18:13:07 +00:00
|
|
|
if self.book_id > 0:
|
2022-04-23 17:56:02 +00:00
|
|
|
return "Replace/Delete Cover Thumbnails for book " + str(self.book_id)
|
2022-03-20 18:13:07 +00:00
|
|
|
else:
|
|
|
|
return "Delete Thumbnail cache directory"
|
2022-03-16 19:33:11 +00:00
|
|
|
|
2021-09-29 07:40:12 +00:00
|
|
|
@property
|
|
|
|
def is_cancellable(self):
|
|
|
|
return False
|