Skip to content

Commit

Permalink
Fix cover upload url with spaces at the end
Browse files Browse the repository at this point in the history
Support image/jpg as upload format mimetype, remove redundant check of mimetype
  • Loading branch information
OzzieIsaacs committed Apr 12, 2022
1 parent 028e685 commit 83b99fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cps/editbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ def edit_book(book_id):
if to_save["cover_url"].endswith('/static/generic_cover.jpg'):
book.has_cover = 0
else:
result, error = helper.save_cover_from_url(to_save["cover_url"], book.path)
result, error = helper.save_cover_from_url(to_save["cover_url"].strip(), book.path)
if result is True:
book.has_cover = 1
modify_date = True
Expand Down
30 changes: 16 additions & 14 deletions cps/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ def save_cover_from_url(url, book_path):
log.error("python modul advocate is not installed but is needed")
return False, _("Python modul 'advocate' is not installed but is needed for cover downloads")
img.raise_for_status()
# # cover_processing()
# move_coverfile(meta, db_book)

return save_cover(img, book_path)
except (socket.gaierror,
requests.exceptions.HTTPError,
Expand Down Expand Up @@ -802,24 +805,23 @@ def save_cover(img, book_path):
content_type = img.headers.get('content-type')

if use_IM:
if content_type not in ('image/jpeg', 'image/png', 'image/webp', 'image/bmp'):
if content_type not in ('image/jpeg', 'image/jpg', 'image/png', 'image/webp', 'image/bmp'):
log.error("Only jpg/jpeg/png/webp/bmp files are supported as coverfile")
return False, _("Only jpg/jpeg/png/webp/bmp files are supported as coverfile")
# convert to jpg because calibre only supports jpg
if content_type != 'image/jpg':
try:
if hasattr(img, 'stream'):
imgc = Image(blob=img.stream)
else:
imgc = Image(blob=io.BytesIO(img.content))
imgc.format = 'jpeg'
imgc.transform_colorspace("rgb")
img = imgc
except (BlobError, MissingDelegateError):
log.error("Invalid cover file content")
return False, _("Invalid cover file content")
try:
if hasattr(img, 'stream'):
imgc = Image(blob=img.stream)
else:
imgc = Image(blob=io.BytesIO(img.content))
imgc.format = 'jpeg'
imgc.transform_colorspace("rgb")
img = imgc
except (BlobError, MissingDelegateError):
log.error("Invalid cover file content")
return False, _("Invalid cover file content")
else:
if content_type not in 'image/jpeg':
if content_type not in ['image/jpeg', 'image/jpg']:
log.error("Only jpg/jpeg files are supported as coverfile")
return False, _("Only jpg/jpeg files are supported as coverfile")

Expand Down

0 comments on commit 83b99fc

Please sign in to comment.