mirror of
https://github.com/janeczku/calibre-web
synced 2026-10-12 19:11:41 +00:00
fix(img): generate cover upon uploading picture
This commit is contained in:
+3
-3
@@ -143,8 +143,8 @@ del env_CALIBRE_PORT
|
||||
|
||||
|
||||
EXTENSIONS_AUDIO = {'mp3', 'ogg', 'opus', 'wav', 'flac', 'm4a', 'm4b'}
|
||||
EXTENSIONS_VIDEO = {'mp4', 'avi', 'mkv', 'webm', 'm4v', 'mov', 'wmv', 'mpg', 'mpeg', 'flv', '3gp', '3g2', 'ogv'}
|
||||
EXTENSIONS_IMAGE = {'jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'tif', 'svg', 'ico', 'webp'}
|
||||
EXTENSIONS_VIDEO = {'mp4', 'webm', 'avi', 'mkv', 'm4v', 'mpg', 'mpeg', 'ogv'}
|
||||
EXTENSIONS_IMAGE = {'jpg', 'jpeg', 'png', 'gif', 'svg', 'webp'}
|
||||
EXTENSIONS_CONVERT_FROM = ['pdf', 'epub', 'mobi', 'azw3', 'docx', 'rtf', 'fb2', 'lit', 'lrf',
|
||||
'txt', 'htmlz', 'rtf', 'odt', 'cbz', 'cbr']
|
||||
EXTENSIONS_CONVERT_TO = ['pdf', 'epub', 'mobi', 'azw3', 'docx', 'rtf', 'fb2',
|
||||
@@ -152,7 +152,7 @@ EXTENSIONS_CONVERT_TO = ['pdf', 'epub', 'mobi', 'azw3', 'docx', 'rtf', 'fb2',
|
||||
EXTENSIONS_UPLOAD = {'txt', 'pdf', 'epub', 'kepub', 'mobi', 'azw', 'azw3', 'cbr', 'cbz', 'cbt', 'djvu', 'djv',
|
||||
'prc', 'doc', 'docx', 'fb2', 'html', 'rtf', 'lit', 'odt', 'mp3', 'mp4', 'ogg',
|
||||
'opus', 'wav', 'flac', 'm4a', 'm4b', "avi", "mkv", "webm", "m4v", "mov", "wmv", "mpg",
|
||||
"mpeg", "flv", "3gp", "3g2", "ogv", "jpg", "jpeg", "png", "gif", "bmp", "tif", "tiff"}
|
||||
"mpeg", "flv", "3gp", "3g2", "ogv", "jpg", "jpeg", "png", "gif", "svg", "webp"}
|
||||
|
||||
|
||||
def has_flag(value, bit_flag):
|
||||
|
||||
+4
-5
@@ -753,11 +753,10 @@ def move_coverfile(meta, db_book):
|
||||
cover_file = os.path.join(constants.STATIC_DIR, 'generic_cover.jpg')
|
||||
new_cover_path = os.path.join(config.config_calibre_dir, db_book.path)
|
||||
try:
|
||||
if cover_file != "img":
|
||||
os.makedirs(new_cover_path, exist_ok=True)
|
||||
copyfile(cover_file, os.path.join(new_cover_path, "cover.jpg"))
|
||||
if meta.cover:
|
||||
os.unlink(meta.cover)
|
||||
os.makedirs(new_cover_path, exist_ok=True)
|
||||
copyfile(cover_file, os.path.join(new_cover_path, "cover.jpg"))
|
||||
if meta.cover:
|
||||
os.unlink(meta.cover)
|
||||
except OSError as e:
|
||||
log.error("Failed to move cover file %s: %s", new_cover_path, e)
|
||||
flash(_("Failed to Move Cover File %(file)s: %(error)s", file=new_cover_path,
|
||||
|
||||
+2
-6
@@ -54,7 +54,7 @@ from . import calibre_db, cli_param
|
||||
from .tasks.convert import TaskConvert
|
||||
from . import logger, config, db, ub, fs
|
||||
from . import gdriveutils as gd
|
||||
from .constants import STATIC_DIR as _STATIC_DIR, CACHE_TYPE_THUMBNAILS, THUMBNAIL_TYPE_COVER, THUMBNAIL_TYPE_SERIES, EXTENSIONS_IMAGE
|
||||
from .constants import STATIC_DIR as _STATIC_DIR, CACHE_TYPE_THUMBNAILS, THUMBNAIL_TYPE_COVER, THUMBNAIL_TYPE_SERIES
|
||||
from .subproc_wrapper import process_wait
|
||||
from .services.worker import WorkerThread
|
||||
from .tasks.mail import TaskEmail
|
||||
@@ -783,11 +783,7 @@ def get_book_cover_internal(book, use_generic_cover_on_failure, resolution=None)
|
||||
# Send the book cover from the Calibre directory
|
||||
else:
|
||||
cover_file_path = os.path.join(config.config_calibre_dir, book.path)
|
||||
image_files = [f for f in os.listdir(cover_file_path) if os.path.isfile(os.path.join(cover_file_path, f)) and
|
||||
f.lower().endswith(EXTENSIONS_IMAGE)]
|
||||
if len(image_files) == 1:
|
||||
return send_from_directory(cover_file_path, image_files[0])
|
||||
elif len(image_files) > 1 or os.path.isfile(os.path.join(cover_file_path, "cover.jpg")):
|
||||
if os.path.isfile(os.path.join(cover_file_path, "cover.jpg")):
|
||||
return send_from_directory(cover_file_path, "cover.jpg")
|
||||
else:
|
||||
return get_cover_on_failure(use_generic_cover_on_failure)
|
||||
|
||||
+6
-5
@@ -18,6 +18,7 @@
|
||||
|
||||
import os
|
||||
import hashlib
|
||||
import shutil
|
||||
import subprocess
|
||||
from tempfile import gettempdir
|
||||
from flask_babel import gettext as _
|
||||
@@ -85,9 +86,9 @@ def process(tmp_file_path, original_file_name, original_file_extension, rarExecu
|
||||
original_file_name,
|
||||
original_file_extension,
|
||||
rarExecutable)
|
||||
elif extension_upper in EXTENSIONS_VIDEO:
|
||||
elif extension_upper in ['.MP4', '.WEBM', '.AVI', '.MKV', '.M4V', '.MPG', '.MPEG','.OGV']:
|
||||
meta = video_metadata(tmp_file_path, original_file_name, original_file_extension)
|
||||
elif extension_upper in EXTENSIONS_IMAGE:
|
||||
elif extension_upper in ['.JPG', '.JPEG', '.PNG', '.GIF', '.SVG', '.WEBP']:
|
||||
meta = image_metadata(tmp_file_path, original_file_name, original_file_extension)
|
||||
|
||||
except Exception as ex:
|
||||
@@ -267,20 +268,20 @@ def video_cover(tmp_file_path):
|
||||
ffmpeg_executable = os.getenv('FFMPEG_PATH', 'ffmpeg')
|
||||
try:
|
||||
subprocess.call([ffmpeg_executable, '-i', tmp_file_path, '-vframes', '1', '-y', os.path.splitext(tmp_file_path)[0] + '.cover.jpg'])
|
||||
tmp_file_path = os.path.splitext(tmp_file_path)[0] + '.cover.jpg'
|
||||
return tmp_file_path
|
||||
return None
|
||||
except Exception as ex:
|
||||
log.warning('Cannot extract cover image, using default: %s', ex)
|
||||
return None
|
||||
|
||||
|
||||
def image_metadata(tmp_file_path, original_file_name, original_file_extension):
|
||||
shutil.copyfile(tmp_file_path, os.path.splitext(tmp_file_path)[0] + '.cover.jpg')
|
||||
meta = BookMeta(
|
||||
file_path=tmp_file_path,
|
||||
extension=original_file_extension,
|
||||
title=original_file_name,
|
||||
author='Unknown',
|
||||
cover='img'
|
||||
cover=os.path.splitext(tmp_file_path)[0] + '.cover.jpg',
|
||||
description='',
|
||||
tags='',
|
||||
series="",
|
||||
|
||||
+6
-1
@@ -1551,7 +1551,12 @@ def read_book(book_id, book_format):
|
||||
log.debug("Start mp3 listening for %d", book_id)
|
||||
return render_title_template('listenmp3.html', mp3file=book_id, audioformat=book_format.lower(),
|
||||
entry=entries, bookmark=bookmark)
|
||||
for fileExt in constants.EXTENSIONS_VIDEO + constants.EXTENSIONS_IMAGE:
|
||||
for fileExt in constants.EXTENSIONS_IMAGE:
|
||||
if book_format.lower() == fileExt:
|
||||
entries = calibre_db.get_filtered_book(book_id)
|
||||
log.debug("Start image viewing for %d", book_id)
|
||||
return serve_book.__closure__[0].cell_contents(book_id, book_format.lower(), anyname="")
|
||||
for fileExt in constants.EXTENSIONS_VIDEO:
|
||||
if book_format.lower() == fileExt:
|
||||
entries = calibre_db.get_filtered_book(book_id)
|
||||
log.debug("Start video watching for %d", book_id)
|
||||
|
||||
Reference in New Issue
Block a user