Ensure file paths in uploads are non-empty, fix #183 (#184)

* Ensure file paths in uploads are non-empty, fix #183

* Fix AttributeError in process(), handle strings of spaces
This commit is contained in:
Matt Hazinski 2017-04-23 02:22:10 -04:00 committed by Ozzie Isaacs
parent ede8ae6742
commit 6f7a240ce2
3 changed files with 16 additions and 8 deletions

View File

@ -41,16 +41,21 @@ except ImportError as e:
def process(tmp_file_path, original_file_name, original_file_extension):
meta = None
try:
if ".PDF" == original_file_extension.upper():
return 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:
return 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:
return fb2.get_fb2_info(tmp_file_path, original_file_extension)
meta = fb2.get_fb2_info(tmp_file_path, original_file_extension)
except Exception as e:
logger.warning('cannot parse metadata, using default: %s', e)
return default_meta(tmp_file_path, original_file_name, original_file_extension)
if meta and meta.title.strip() and meta.author.strip():
return meta
else:
return default_meta(tmp_file_path, original_file_name, original_file_extension)
def default_meta(tmp_file_path, original_file_name, original_file_extension):
@ -76,8 +81,8 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
doc_info = None
if doc_info is not None:
author = doc_info.author if doc_info.author is not None else u"Unknown"
title = doc_info.title if doc_info.title is not None else original_file_name
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
else:
author = u"Unknown"
@ -115,4 +120,4 @@ def get_versions():
PVersion=PyPdfVersion
else:
PVersion=_(u'not installed')
return {'ImageVersion':IVersion,'PyPdfVersion':PVersion}
return {'ImageVersion':IVersion,'PyPdfVersion':PVersion}

View File

@ -89,7 +89,7 @@ def get_epub_info(tmp_file_path, original_file_name, original_file_extension):
else:
coverfile = extractCover(epubZip, coversection[0], coverpath, tmp_file_path)
if epub_metadata['title'] is None:
if not epub_metadata['title']:
title = original_file_name
else:
title = epub_metadata['title']

View File

@ -261,6 +261,9 @@ def get_valid_filename(value, replace_whitespace=True):
value = re.sub('[\*\+:\\\"/<>\?]+', u'_', value, flags=re.U)
value = value[:128]
if not value:
raise ValueError("Filename cannot be empty")
return value
def get_sorted_author(value):