2017-01-29 20:06:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 18:37:45 +00:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
2019-07-13 18:45:48 +00:00
|
|
|
# Copyright (C) 2012-2019 lemmsh cervinko Kennyl matthazinski OzzieIsaacs
|
2019-01-20 18:37:45 +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/>.
|
|
|
|
|
2016-06-05 15:41:47 +00:00
|
|
|
import os
|
|
|
|
import hashlib
|
2019-12-09 19:53:16 +00:00
|
|
|
from flask_babel import gettext as _
|
|
|
|
|
2021-03-21 06:28:35 +00:00
|
|
|
from . import logger, comic, isoLanguages
|
2019-07-13 18:45:48 +00:00
|
|
|
from .constants import BookMeta
|
2020-08-23 08:53:18 +00:00
|
|
|
from .helper import split_authors
|
2024-08-11 17:20:12 +00:00
|
|
|
from .file_helper import get_temp_dir
|
2024-08-12 16:41:20 +00:00
|
|
|
from .string_helper import strip_whitespaces
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
try:
|
2020-07-23 16:41:38 +00:00
|
|
|
from wand.image import Image, Color
|
2019-07-13 18:45:48 +00:00
|
|
|
from wand import version as ImageVersion
|
|
|
|
from wand.exceptions import PolicyError
|
|
|
|
use_generic_pdf_cover = False
|
|
|
|
except (ImportError, RuntimeError) as e:
|
2020-05-23 14:20:19 +00:00
|
|
|
log.debug('Cannot import Image, generating pdf covers for pdf uploads will not work: %s', e)
|
2019-07-13 18:45:48 +00:00
|
|
|
use_generic_pdf_cover = True
|
|
|
|
|
|
|
|
try:
|
2023-01-23 15:04:25 +00:00
|
|
|
from pypdf import PdfReader
|
2024-10-04 16:30:28 +00:00
|
|
|
from pypdf.generic import NullObject
|
2019-07-13 18:45:48 +00:00
|
|
|
use_pdf_meta = True
|
2021-03-17 18:06:51 +00:00
|
|
|
except ImportError as ex:
|
2023-01-22 10:31:47 +00:00
|
|
|
log.debug('PyPDF is recommended for best performance in metadata extracting from pdf files: %s', ex)
|
2021-03-17 18:06:51 +00:00
|
|
|
try:
|
2023-01-22 10:31:47 +00:00
|
|
|
from PyPDF2 import PdfReader
|
2024-10-04 16:30:28 +00:00
|
|
|
from pypdf.generic import NullObject
|
2021-03-17 18:06:51 +00:00
|
|
|
use_pdf_meta = True
|
2023-01-22 10:31:47 +00:00
|
|
|
except ImportError as ex:
|
|
|
|
log.debug('PyPDF is recommended for best performance in metadata extracting from pdf files: %s', ex)
|
|
|
|
log.debug('PyPdf2 is also possible for metadata extracting from pdf files, but not recommended anymore')
|
|
|
|
try:
|
|
|
|
from PyPDF3 import PdfFileReader as PdfReader
|
2024-10-04 16:30:28 +00:00
|
|
|
from pypdf.generic import NullObject
|
2023-01-22 10:31:47 +00:00
|
|
|
use_pdf_meta = True
|
|
|
|
except ImportError as e:
|
|
|
|
log.debug('Cannot import PyPDF3/PyPDF2, extracting pdf metadata will not work: %s / %s', e)
|
|
|
|
use_pdf_meta = False
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from . import epub
|
|
|
|
use_epub_meta = True
|
|
|
|
except ImportError as e:
|
2020-05-23 14:20:19 +00:00
|
|
|
log.debug('Cannot import epub, extracting epub metadata will not work: %s', e)
|
2019-07-13 18:45:48 +00:00
|
|
|
use_epub_meta = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
from . import fb2
|
|
|
|
use_fb2_meta = True
|
|
|
|
except ImportError as e:
|
2020-05-23 14:20:19 +00:00
|
|
|
log.debug('Cannot import fb2, extracting fb2 metadata will not work: %s', e)
|
2019-07-13 18:45:48 +00:00
|
|
|
use_fb2_meta = False
|
|
|
|
|
2024-08-07 15:23:41 +00:00
|
|
|
try:
|
|
|
|
from . import audio
|
|
|
|
use_audio_meta = True
|
|
|
|
except ImportError as e:
|
|
|
|
log.debug('Cannot import mutagen, extracting audio metadata will not work: %s', e)
|
|
|
|
use_audio_meta = False
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2024-08-18 08:12:04 +00:00
|
|
|
def process(tmp_file_path, original_file_name, original_file_extension, rar_executable, no_cover=False):
|
2023-01-22 10:25:24 +00:00
|
|
|
meta = default_meta(tmp_file_path, original_file_name, original_file_extension)
|
2020-05-11 11:15:30 +00:00
|
|
|
extension_upper = original_file_extension.upper()
|
2019-07-13 18:45:48 +00:00
|
|
|
try:
|
2020-05-11 11:15:30 +00:00
|
|
|
if ".PDF" == extension_upper:
|
2024-08-18 08:12:04 +00:00
|
|
|
meta = pdf_meta(tmp_file_path, original_file_name, original_file_extension, no_cover)
|
2020-05-17 14:22:58 +00:00
|
|
|
elif extension_upper in [".KEPUB", ".EPUB"] and use_epub_meta is True:
|
2024-08-18 08:12:04 +00:00
|
|
|
meta = epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension, no_cover)
|
2020-05-11 11:15:30 +00:00
|
|
|
elif ".FB2" == extension_upper and use_fb2_meta is True:
|
2019-07-13 18:45:48 +00:00
|
|
|
meta = fb2.get_fb2_info(tmp_file_path, original_file_extension)
|
2023-08-16 16:44:03 +00:00
|
|
|
elif extension_upper in ['.CBZ', '.CBT', '.CBR', ".CB7"]:
|
2020-04-28 14:50:08 +00:00
|
|
|
meta = comic.get_comic_info(tmp_file_path,
|
|
|
|
original_file_name,
|
|
|
|
original_file_extension,
|
2024-08-18 08:12:04 +00:00
|
|
|
rar_executable,
|
|
|
|
no_cover)
|
2024-08-11 10:16:22 +00:00
|
|
|
elif extension_upper in [".MP3", ".OGG", ".FLAC", ".WAV", ".AAC", ".AIFF", ".ASF", ".MP4",
|
2024-08-11 17:20:12 +00:00
|
|
|
".M4A", ".M4B", ".OGV", ".OPUS"] and use_audio_meta:
|
2024-08-18 08:12:04 +00:00
|
|
|
meta = audio.get_audio_file_info(tmp_file_path, original_file_extension, original_file_name, no_cover)
|
2021-03-21 06:28:35 +00:00
|
|
|
except Exception as ex:
|
2019-07-13 18:45:48 +00:00
|
|
|
log.warning('cannot parse metadata, using default: %s', ex)
|
|
|
|
|
2024-08-12 16:41:20 +00:00
|
|
|
if not strip_whitespaces(meta.title):
|
2023-04-30 11:37:08 +00:00
|
|
|
meta = meta._replace(title=original_file_name)
|
2024-08-12 16:41:20 +00:00
|
|
|
if not strip_whitespaces(meta.author) or meta.author.lower() == 'unknown':
|
2023-01-22 10:25:24 +00:00
|
|
|
meta = meta._replace(author=_('Unknown'))
|
|
|
|
return meta
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def default_meta(tmp_file_path, original_file_name, original_file_extension):
|
|
|
|
return BookMeta(
|
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
|
|
|
title=original_file_name,
|
2023-01-21 14:23:18 +00:00
|
|
|
author=_('Unknown'),
|
2022-04-26 09:28:20 +00:00
|
|
|
cover=None,
|
2019-07-13 18:45:48 +00:00
|
|
|
description="",
|
|
|
|
tags="",
|
|
|
|
series="",
|
|
|
|
series_id="",
|
2021-03-17 18:06:51 +00:00
|
|
|
languages="",
|
2022-04-05 00:26:35 +00:00
|
|
|
publisher="",
|
|
|
|
pubdate="",
|
|
|
|
identifiers=[]
|
|
|
|
)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-03-16 16:53:33 +00:00
|
|
|
def parse_xmp(pdf_file):
|
|
|
|
"""
|
2021-11-06 16:17:00 +00:00
|
|
|
Parse XMP Metadata and prepare for BookMeta object
|
2021-03-16 16:53:33 +00:00
|
|
|
"""
|
|
|
|
try:
|
2023-01-22 10:25:24 +00:00
|
|
|
xmp_info = pdf_file.xmp_metadata
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
2022-08-27 13:44:21 +00:00
|
|
|
log.debug('Can not read PDF XMP metadata {}'.format(ex))
|
2021-03-16 16:53:33 +00:00
|
|
|
return None
|
|
|
|
|
2020-09-11 10:08:55 +00:00
|
|
|
if xmp_info:
|
2021-03-16 16:53:33 +00:00
|
|
|
try:
|
|
|
|
xmp_author = xmp_info.dc_creator # list
|
2021-03-19 14:26:41 +00:00
|
|
|
except AttributeError:
|
2021-03-16 16:53:33 +00:00
|
|
|
xmp_author = ['Unknown']
|
2021-11-06 16:17:00 +00:00
|
|
|
|
|
|
|
if xmp_info.dc_title:
|
2020-09-11 10:08:55 +00:00
|
|
|
xmp_title = xmp_info.dc_title['x-default']
|
|
|
|
else:
|
|
|
|
xmp_title = ''
|
2021-03-16 16:53:33 +00:00
|
|
|
|
2020-09-11 10:08:55 +00:00
|
|
|
if xmp_info.dc_description:
|
|
|
|
xmp_description = xmp_info.dc_description['x-default']
|
|
|
|
else:
|
|
|
|
xmp_description = ''
|
2021-03-16 16:53:33 +00:00
|
|
|
|
|
|
|
languages = []
|
2021-03-19 14:26:41 +00:00
|
|
|
try:
|
|
|
|
for i in xmp_info.dc_language:
|
|
|
|
languages.append(isoLanguages.get_lang3(i))
|
|
|
|
except AttributeError:
|
|
|
|
languages.append('')
|
2021-11-06 16:17:00 +00:00
|
|
|
|
2020-09-11 10:49:45 +00:00
|
|
|
xmp_tags = ', '.join(xmp_info.dc_subject)
|
|
|
|
xmp_publisher = ', '.join(xmp_info.dc_publisher)
|
2021-03-16 16:53:33 +00:00
|
|
|
|
|
|
|
return {'author': xmp_author,
|
2021-03-19 14:26:41 +00:00
|
|
|
'title': xmp_title,
|
|
|
|
'subject': xmp_description,
|
|
|
|
'tags': xmp_tags,
|
|
|
|
'languages': languages,
|
|
|
|
'publisher': xmp_publisher
|
|
|
|
}
|
2021-03-16 16:53:33 +00:00
|
|
|
|
|
|
|
|
2024-08-18 08:12:04 +00:00
|
|
|
def pdf_meta(tmp_file_path, original_file_name, original_file_extension, no_cover_processing):
|
2020-05-11 11:15:30 +00:00
|
|
|
doc_info = None
|
2021-03-16 16:53:33 +00:00
|
|
|
xmp_info = None
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
if use_pdf_meta:
|
2020-07-21 18:14:08 +00:00
|
|
|
with open(tmp_file_path, 'rb') as f:
|
2023-01-16 05:24:44 +00:00
|
|
|
pdf_file = PdfReader(f)
|
2022-08-27 13:44:21 +00:00
|
|
|
try:
|
2023-01-22 10:25:24 +00:00
|
|
|
doc_info = pdf_file.metadata
|
2022-08-27 13:44:21 +00:00
|
|
|
except Exception as exc:
|
|
|
|
log.debug('Can not read PDF DocumentInfo {}'.format(exc))
|
2021-03-16 16:53:33 +00:00
|
|
|
xmp_info = parse_xmp(pdf_file)
|
|
|
|
|
|
|
|
if xmp_info:
|
|
|
|
author = ' & '.join(split_authors(xmp_info['author']))
|
|
|
|
title = xmp_info['title']
|
|
|
|
subject = xmp_info['subject']
|
|
|
|
tags = xmp_info['tags']
|
|
|
|
languages = xmp_info['languages']
|
|
|
|
publisher = xmp_info['publisher']
|
2021-03-19 14:26:41 +00:00
|
|
|
else:
|
2023-01-21 14:23:18 +00:00
|
|
|
author = 'Unknown'
|
2021-03-19 14:26:41 +00:00
|
|
|
title = ''
|
|
|
|
languages = [""]
|
|
|
|
publisher = ""
|
|
|
|
subject = ""
|
|
|
|
tags = ""
|
2021-03-16 16:53:33 +00:00
|
|
|
|
2021-03-17 18:06:51 +00:00
|
|
|
if doc_info:
|
|
|
|
if author == '':
|
2023-01-21 14:23:18 +00:00
|
|
|
author = ' & '.join(split_authors([doc_info.author])) if doc_info.author else 'Unknown'
|
2021-03-17 18:06:51 +00:00
|
|
|
if title == '':
|
|
|
|
title = doc_info.title if doc_info.title else original_file_name
|
|
|
|
if subject == '':
|
2022-04-13 16:37:23 +00:00
|
|
|
subject = doc_info.subject or ""
|
2021-03-17 18:06:51 +00:00
|
|
|
if tags == '' and '/Keywords' in doc_info:
|
2024-10-04 16:30:28 +00:00
|
|
|
keywords = doc_info['/Keywords']
|
|
|
|
if not isinstance(keywords, NullObject):
|
|
|
|
if isinstance(keywords, bytes):
|
|
|
|
tags = keywords.decode('utf-8')
|
|
|
|
else:
|
|
|
|
tags = keywords
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
title = original_file_name
|
2019-12-09 19:53:16 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
return BookMeta(
|
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
|
|
|
title=title,
|
2021-03-16 16:53:33 +00:00
|
|
|
author=author,
|
2024-08-18 08:12:04 +00:00
|
|
|
cover=pdf_preview(tmp_file_path, original_file_name) if not no_cover_processing else None,
|
2019-07-13 18:45:48 +00:00
|
|
|
description=subject,
|
2020-09-11 10:08:55 +00:00
|
|
|
tags=tags,
|
2019-07-13 18:45:48 +00:00
|
|
|
series="",
|
|
|
|
series_id="",
|
2021-03-17 18:06:51 +00:00
|
|
|
languages=','.join(languages),
|
2022-04-05 00:26:35 +00:00
|
|
|
publisher=publisher,
|
|
|
|
pubdate="",
|
|
|
|
identifiers=[])
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pdf_preview(tmp_file_path, tmp_dir):
|
|
|
|
if use_generic_pdf_cover:
|
|
|
|
return None
|
2020-05-11 11:15:30 +00:00
|
|
|
try:
|
2024-11-30 08:45:46 +00:00
|
|
|
cover_file_name = tmp_file_path + ".jpg"
|
2020-05-11 11:15:30 +00:00
|
|
|
with Image() as img:
|
|
|
|
img.options["pdf:use-cropbox"] = "true"
|
|
|
|
img.read(filename=tmp_file_path + '[0]', resolution=150)
|
|
|
|
img.compression_quality = 88
|
2020-07-23 16:41:38 +00:00
|
|
|
if img.alpha_channel:
|
|
|
|
img.alpha_channel = 'remove'
|
|
|
|
img.background_color = Color('white')
|
2024-11-30 08:45:46 +00:00
|
|
|
img.save(filename=cover_file_name)
|
2020-05-11 11:15:30 +00:00
|
|
|
return cover_file_name
|
|
|
|
except PolicyError as ex:
|
|
|
|
log.warning('Pdf extraction forbidden by Imagemagick policy: %s', ex)
|
|
|
|
return None
|
|
|
|
except Exception as ex:
|
|
|
|
log.warning('Cannot extract cover image, using default: %s', ex)
|
2020-07-21 18:14:08 +00:00
|
|
|
log.warning('On Windows this error could be caused by missing ghostscript')
|
2020-05-11 11:15:30 +00:00
|
|
|
return None
|
2016-06-05 15:41:47 +00:00
|
|
|
|
|
|
|
|
2022-05-01 10:36:35 +00:00
|
|
|
def get_magick_version():
|
2021-11-06 16:17:00 +00:00
|
|
|
ret = dict()
|
2019-07-13 18:45:48 +00:00
|
|
|
if not use_generic_pdf_cover:
|
2021-11-06 16:17:00 +00:00
|
|
|
ret['Image Magick'] = ImageVersion.MAGICK_VERSION
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2023-01-21 14:23:18 +00:00
|
|
|
ret['Image Magick'] = 'not installed'
|
2021-11-06 16:17:00 +00:00
|
|
|
return ret
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
|
2023-06-25 12:39:45 +00:00
|
|
|
def upload(uploadfile, rar_excecutable):
|
2023-11-11 13:48:59 +00:00
|
|
|
tmp_dir = get_temp_dir()
|
2020-05-11 11:15:30 +00:00
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
filename = uploadfile.filename
|
2016-06-05 15:41:47 +00:00
|
|
|
filename_root, file_extension = os.path.splitext(filename)
|
2021-03-15 12:48:05 +00:00
|
|
|
md5 = hashlib.md5(filename.encode('utf-8')).hexdigest() # nosec
|
2020-05-11 11:15:30 +00:00
|
|
|
tmp_file_path = os.path.join(tmp_dir, md5)
|
2019-12-08 08:40:54 +00:00
|
|
|
log.debug("Temporary file: %s", tmp_file_path)
|
2017-11-30 15:49:46 +00:00
|
|
|
uploadfile.save(tmp_file_path)
|
2023-06-25 12:39:45 +00:00
|
|
|
return process(tmp_file_path, filename_root, file_extension, rar_excecutable)
|