Add unrar Support

This commit is contained in:
Ozzieisaacs 2020-04-28 20:23:39 +02:00
parent 46c0ae3ccc
commit 27eb09fb19
4 changed files with 35 additions and 12 deletions

View File

@ -33,10 +33,16 @@ except ImportError as e:
log.debug('cannot import comicapi, extracting comic metadata will not work: %s', e)
import zipfile
import tarfile
try:
import rarfile
use_rarfile = True
except ImportError as e:
log.debug('cannot import rarfile, extracting cover files from rar files will not work: %s', e)
use_rarfile = False
use_comic_meta = False
def extractCover(tmp_file_name, original_file_extension):
def _extractCover(tmp_file_name, original_file_extension, rarExceutable):
if use_comic_meta:
archive = ComicArchive(tmp_file_name)
cover_data = None
@ -66,6 +72,20 @@ def extractCover(tmp_file_name, original_file_extension):
if extension == '.jpg' or extension == '.jpeg':
cover_data = cf.extractfile(name).read()
break
elif original_file_extension.upper() == '.CBR' and use_rarfile:
try:
rarfile.UNRAR_TOOL = rarExceutable
cf = rarfile.RarFile(tmp_file_name)
for name in cf.getnames():
ext = os.path.splitext(name)
if len(ext) > 1:
extension = ext[1].lower()
if extension == '.jpg' or extension == '.jpeg':
cover_data = cf.extractfile(name).read()
break
except Exception as e:
log.debug('Rarfile failed with error: %s', e)
prefix = os.path.dirname(tmp_file_name)
if cover_data:
tmp_cover_name = prefix + '/cover' + extension
@ -77,7 +97,7 @@ def extractCover(tmp_file_name, original_file_extension):
return tmp_cover_name
def get_comic_info(tmp_file_path, original_file_name, original_file_extension):
def get_comic_info(tmp_file_path, original_file_name, original_file_extension, rarExceutable):
if use_comic_meta:
archive = ComicArchive(tmp_file_path)
if archive.seemsToBeAComicArchive():
@ -105,7 +125,7 @@ def get_comic_info(tmp_file_path, original_file_name, original_file_extension):
extension=original_file_extension,
title=loadedMetadata.title or original_file_name,
author=" & ".join([credit["person"] for credit in loadedMetadata.credits if credit["role"] == "Writer"]) or u'Unknown',
cover=extractCover(tmp_file_path, original_file_extension),
cover=_extractCover(tmp_file_path, original_file_extension, rarExceutable),
description=loadedMetadata.comments or "",
tags="",
series=loadedMetadata.series or "",
@ -118,7 +138,7 @@ def get_comic_info(tmp_file_path, original_file_name, original_file_extension):
extension=original_file_extension,
title=original_file_name,
author=u'Unknown',
cover=extractCover(tmp_file_path, original_file_extension),
cover=_extractCover(tmp_file_path, original_file_extension, rarExceutable),
description="",
tags="",
series="",

View File

@ -652,7 +652,7 @@ def upload():
# extract metadata from file
try:
meta = uploader.upload(requested_file)
meta = uploader.upload(requested_file, config.config_rarfile_location)
except (IOError, OSError):
log.error("File %s could not saved to temp dir", requested_file.filename)
flash(_(u"File %(filename)s could not saved to temp dir",

View File

@ -602,7 +602,7 @@ def check_unrar(unrarLocation):
return
if not os.path.exists(unrarLocation):
return 'Unrar binary file not found'
return _('Unrar binary file not found')
try:
if sys.version_info < (3, 0):
@ -615,7 +615,7 @@ def check_unrar(unrarLocation):
log.debug("unrar version %s", version)
except OSError as err:
log.exception(err)
return 'Error excecuting UnRar'
return _('Error excecuting UnRar')
def json_serial(obj):

View File

@ -76,7 +76,7 @@ except ImportError as e:
__author__ = 'lemmsh'
def process(tmp_file_path, original_file_name, original_file_extension):
def process(tmp_file_path, original_file_name, original_file_extension, rarExcecutable):
meta = None
try:
if ".PDF" == original_file_extension.upper():
@ -85,8 +85,11 @@ def process(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:
meta = fb2.get_fb2_info(tmp_file_path, original_file_extension)
if original_file_extension.upper() in ['.CBZ', '.CBT']:
meta = comic.get_comic_info(tmp_file_path, original_file_name, original_file_extension)
if original_file_extension.upper() in ['.CBZ', '.CBT', 'CBR']:
meta = comic.get_comic_info(tmp_file_path,
original_file_name,
original_file_extension,
rarExcecutable)
except Exception as ex:
log.warning('cannot parse metadata, using default: %s', ex)
@ -194,7 +197,7 @@ def get_versions():
'Comic_API': ComicVersion}
def upload(uploadfile):
def upload(uploadfile, rarExcecutable):
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
@ -206,5 +209,5 @@ def upload(uploadfile):
tmp_file_path = os.path.join(tmp_dir, md5.hexdigest())
log.debug("Temporary file: %s", tmp_file_path)
uploadfile.save(tmp_file_path)
meta = process(tmp_file_path, filename_root, file_extension)
meta = process(tmp_file_path, filename_root, file_extension, rarExcecutable)
return meta