Refactor a bit cps/uploader.py

pull/1379/head
jvoisin 5 years ago
parent 92db00692a
commit e69c4cd1dc

@ -76,21 +76,22 @@ except ImportError as e:
__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
extension_upper = original_file_extension.upper()
try:
if ".PDF" == original_file_extension.upper():
if ".PDF" == extension_upper:
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)
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)
if original_file_extension.upper() in ['.CBZ', '.CBT', '.CBR']:
elif extension_upper in ['.CBZ', '.CBT', '.CBR']:
meta = comic.get_comic_info(tmp_file_path,
original_file_name,
original_file_extension,
rarExcecutable)
rarExecutable)
except Exception as 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':
meta = meta._replace(author=_(u'Unknown'))
return meta
else:
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):
if use_pdf_meta:
pdf = PdfFileReader(open(tmp_file_path, 'rb'))
doc_info = pdf.getDocumentInfo()
else:
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'
title = doc_info.title if doc_info.title else original_file_name
subject = doc_info.subject
@ -149,7 +146,6 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
def pdf_preview(tmp_file_path, tmp_dir):
if use_generic_pdf_cover:
return None
else:
try:
cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg"
with Image() as img:
@ -202,12 +198,11 @@ def upload(uploadfile, rarExcecutable):
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
filename = uploadfile.filename
filename_root, file_extension = os.path.splitext(filename)
md5 = hashlib.md5()
md5.update(filename.encode('utf-8'))
tmp_file_path = os.path.join(tmp_dir, md5.hexdigest())
md5 = hashlib.md5(filename.encode('utf-8')).hexdigest()
tmp_file_path = os.path.join(tmp_dir, md5)
log.debug("Temporary file: %s", tmp_file_path)
uploadfile.save(tmp_file_path)
meta = process(tmp_file_path, filename_root, file_extension, rarExcecutable)
return meta
return process(tmp_file_path, filename_root, file_extension, rarExcecutable)

Loading…
Cancel
Save