Merge remote-tracking branch 'refactor/refactor_uploader'

This commit is contained in:
Ozzieisaacs 2020-05-15 20:36:00 +02:00
commit a33515d907

View file

@ -76,21 +76,22 @@ except ImportError as e:
__author__ = 'lemmsh' __author__ = 'lemmsh'
def process(tmp_file_path, original_file_name, original_file_extension, rarExcecutable): def process(tmp_file_path, original_file_name, original_file_extension, rarExecutable):
"""Get the metadata for tmp_file_path."""
meta = None meta = None
extension_upper = original_file_extension.upper()
try: try:
if ".PDF" == original_file_extension.upper(): if ".PDF" == extension_upper:
meta = pdf_meta(tmp_file_path, original_file_name, original_file_extension) meta = pdf_meta(tmp_file_path, original_file_name, original_file_extension)
if ".EPUB" == original_file_extension.upper() and use_epub_meta is True: elif ".EPUB" == extension_upper and use_epub_meta is True:
meta = epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension) meta = epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension)
if ".FB2" == original_file_extension.upper() and use_fb2_meta is True: elif ".FB2" == extension_upper and use_fb2_meta is True:
meta = fb2.get_fb2_info(tmp_file_path, original_file_extension) meta = fb2.get_fb2_info(tmp_file_path, original_file_extension)
if original_file_extension.upper() in ['.CBZ', '.CBT', '.CBR']: elif extension_upper in ['.CBZ', '.CBT', '.CBR']:
meta = comic.get_comic_info(tmp_file_path, meta = comic.get_comic_info(tmp_file_path,
original_file_name, original_file_name,
original_file_extension, original_file_extension,
rarExcecutable) rarExecutable)
except Exception as ex: except Exception as ex:
log.warning('cannot parse metadata, using default: %s', ex) log.warning('cannot parse metadata, using default: %s', ex)
@ -98,7 +99,6 @@ def process(tmp_file_path, original_file_name, original_file_extension, rarExcec
if meta.author.lower() == 'unknown': if meta.author.lower() == 'unknown':
meta = meta._replace(author=_(u'Unknown')) meta = meta._replace(author=_(u'Unknown'))
return meta return meta
else:
return default_meta(tmp_file_path, original_file_name, original_file_extension) return default_meta(tmp_file_path, original_file_name, original_file_extension)
@ -117,14 +117,11 @@ def default_meta(tmp_file_path, original_file_name, original_file_extension):
def pdf_meta(tmp_file_path, original_file_name, original_file_extension): def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
if use_pdf_meta:
pdf = PdfFileReader(open(tmp_file_path, 'rb'))
doc_info = pdf.getDocumentInfo()
else:
doc_info = None doc_info = None
if use_pdf_meta:
doc_info = PdfFileReader(open(tmp_file_path, 'rb')).getDocumentInfo()
if doc_info is not None: if doc_info:
author = doc_info.author if doc_info.author else u'Unknown' author = doc_info.author if doc_info.author else u'Unknown'
title = doc_info.title if doc_info.title else original_file_name title = doc_info.title if doc_info.title else original_file_name
subject = doc_info.subject subject = doc_info.subject
@ -149,12 +146,11 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
def pdf_preview(tmp_file_path, tmp_dir): def pdf_preview(tmp_file_path, tmp_dir):
if use_generic_pdf_cover: if use_generic_pdf_cover:
return None return None
else:
try: try:
cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg" cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg"
with Image() as img: with Image() as img:
img.options["pdf:use-cropbox"] = "true" img.options["pdf:use-cropbox"] = "true"
img.read(filename=tmp_file_path + '[0]', resolution = 150) img.read(filename=tmp_file_path + '[0]', resolution=150)
img.compression_quality = 88 img.compression_quality = 88
img.save(filename=os.path.join(tmp_dir, cover_file_name)) img.save(filename=os.path.join(tmp_dir, cover_file_name))
return cover_file_name return cover_file_name
@ -202,12 +198,11 @@ def upload(uploadfile, rarExcecutable):
if not os.path.isdir(tmp_dir): if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir) os.mkdir(tmp_dir)
filename = uploadfile.filename filename = uploadfile.filename
filename_root, file_extension = os.path.splitext(filename) filename_root, file_extension = os.path.splitext(filename)
md5 = hashlib.md5() md5 = hashlib.md5(filename.encode('utf-8')).hexdigest()
md5.update(filename.encode('utf-8')) tmp_file_path = os.path.join(tmp_dir, md5)
tmp_file_path = os.path.join(tmp_dir, md5.hexdigest())
log.debug("Temporary file: %s", tmp_file_path) log.debug("Temporary file: %s", tmp_file_path)
uploadfile.save(tmp_file_path) uploadfile.save(tmp_file_path)
meta = process(tmp_file_path, filename_root, file_extension, rarExcecutable) return process(tmp_file_path, filename_root, file_extension, rarExcecutable)
return meta