diff --git a/cps/admin.py b/cps/admin.py index 3b6a846f..2bfca96b 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -50,11 +50,11 @@ feature_support = { 'kobo': bool(services.kobo) } -# try: -# import rarfile -# feature_support['rar'] = True -# except ImportError: -# feature_support['rar'] = False +try: + import rarfile + feature_support['rar'] = True +except ImportError: + feature_support['rar'] = False try: from .oauth_bb import oauth_check, oauthblueprints diff --git a/cps/comic.py b/cps/comic.py old mode 100755 new mode 100644 index bfb8ee36..2ee182c4 --- a/cps/comic.py +++ b/cps/comic.py @@ -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): cover_data = extension = None if use_comic_meta: archive = ComicArchive(tmp_file_name) @@ -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.read(name) + 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,9 +97,9 @@ 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) + archive = ComicArchive(tmp_file_path, rar_exe_path=rarExceutable) if archive.seemsToBeAComicArchive(): if archive.hasMetadata(MetaDataStyle.CIX): style = MetaDataStyle.CIX @@ -100,27 +120,26 @@ def get_comic_info(tmp_file_path, original_file_name, original_file_extension): else: loadedMetadata.language = "" - return BookMeta( + return BookMeta( file_path=tmp_file_path, 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 "", series_id=loadedMetadata.issue or "", languages=loadedMetadata.language) - else: - return BookMeta( - file_path=tmp_file_path, - extension=original_file_extension, - title=original_file_name, - author=u'Unknown', - cover=extractCover(tmp_file_path, original_file_extension), - description="", - tags="", - series="", - series_id="", - languages="") + return BookMeta( + file_path=tmp_file_path, + extension=original_file_extension, + title=original_file_name, + author=u'Unknown', + cover=_extractCover(tmp_file_path, original_file_extension, rarExceutable), + description="", + tags="", + series="", + series_id="", + languages="") diff --git a/cps/editbooks.py b/cps/editbooks.py index d18faab5..81262760 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -588,7 +588,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", diff --git a/cps/helper.py b/cps/helper.py index f65432ba..34fec6ee 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -583,7 +583,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): @@ -596,7 +596,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') diff --git a/cps/templates/config_edit.html b/cps/templates/config_edit.html index 553be036..f3187011 100644 --- a/cps/templates/config_edit.html +++ b/cps/templates/config_edit.html @@ -344,7 +344,7 @@ - {% if rarfile_support %} + {% if feature_support['rar'] %}