Content server: Fix download button not working with non-ebook formats. Fixes #1850078 [Calibre content server download issues](https://bugs.launchpad.net/calibre/+bug/1850078)

This commit is contained in:
Kovid Goyal 2019-10-28 08:58:47 +05:30
parent 167cb89dc7
commit 42f553e367
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -45,15 +45,20 @@ def sort_formats_key(fmt):
ans = FORMAT_PRIORITIES.length ans = FORMAT_PRIORITIES.length
return ans return ans
def get_preferred_format(metadata, output_format, input_formats): def get_preferred_format(metadata, output_format, input_formats, for_download):
formats = (metadata and metadata.formats) or v'[]' formats = (metadata and metadata.formats) or v'[]'
formats = [f.toUpperCase() for f in formats] formats = [f.toUpperCase() for f in formats]
fmt = 'EPUB' if output_format is 'PDF' else output_format fmt = 'EPUB' if output_format is 'PDF' else output_format
if formats.length and formats.indexOf(fmt) is -1: if formats.length and formats.indexOf(fmt) is -1:
for q in sorted(formats, key=sort_formats_key): found = False
formats = sorted(formats, key=sort_formats_key)
for q in formats:
if input_formats[q]: if input_formats[q]:
fmt = q fmt = q
found = True
break break
if for_download and not found and formats.length:
fmt = formats[0]
return fmt.toUpperCase() return fmt.toUpperCase()
IGNORED_FIELDS = {'title', 'sort', 'uuid', 'id', 'urls_from_identifiers', 'lang_names', 'last_modified', 'path'} IGNORED_FIELDS = {'title', 'sort', 'uuid', 'id', 'urls_from_identifiers', 'lang_names', 'last_modified', 'path'}
@ -409,9 +414,9 @@ def on_img_err(err):
if img.parentNode: if img.parentNode:
img.parentNode.style.display = 'none' img.parentNode.style.display = 'none'
def preferred_format(book_id): def preferred_format(book_id, for_download):
interface_data = get_interface_data() interface_data = get_interface_data()
return get_preferred_format(book_metadata(book_id), interface_data.output_format, interface_data.input_formats) return get_preferred_format(book_metadata(book_id), interface_data.output_format, interface_data.input_formats, for_download)
def read_format(book_id, fmt): def read_format(book_id, fmt):
@ -428,7 +433,7 @@ def download_format(book_id, fmt):
def download_book(book_id): def download_book(book_id):
fmt = preferred_format(book_id) fmt = preferred_format(book_id, True)
download_format(book_id, fmt) download_format(book_id, fmt)
@ -485,7 +490,7 @@ def render_book(container_id, book_id):
)) ))
container = c.lastChild.firstChild container = c.lastChild.firstChild
read_button = create_button(_('Read'), 'book', read_book.bind(None, book_id), _('Read this book')) read_button = create_button(_('Read'), 'book', read_book.bind(None, book_id), _('Read this book'))
fmt = preferred_format(book_id) fmt = preferred_format(book_id, True)
download_button = create_button(_('Download'), 'cloud-download', download_url(book_id, fmt), download_button = create_button(_('Download'), 'cloud-download', download_url(book_id, fmt),
_('Download this book in the {0} format ({1})').format(fmt, human_readable(metadata.format_sizes[fmt] or 0)), _('Download this book in the {0} format ({1})').format(fmt, human_readable(metadata.format_sizes[fmt] or 0)),
download_filename=f'{metadata.title}.{fmt.toLowerCase()}') download_filename=f'{metadata.title}.{fmt.toLowerCase()}')