diff --git a/src/calibre/srv/content.py b/src/calibre/srv/content.py index 11918cd441..53f99ca3d7 100644 --- a/src/calibre/srv/content.py +++ b/src/calibre/srv/content.py @@ -156,6 +156,17 @@ def cover(ctx, rd, library_id, db, book_id, width=None, height=None): return create_file_copy(ctx, rd, prefix, library_id, book_id, 'jpg', mtime, copy_func) +def book_filename(rd, book_id, mi, fmt): + au = authors_to_string(mi.authors or [_('Unknown')]) + title = mi.title or _('Unknown') + ext = fmt + if ext == 'kepub' and 'Kobo Touch' in rd.inheaders.get('User-Agent', ''): + ext = 'kepub.epub' + fname = '%s - %s_%s.%s' % (title[:30], au[:30], book_id, ext) + fname = ascii_filename(fname).replace('"', '_') + return fname + + def book_fmt(ctx, rd, library_id, db, book_id, fmt): mdata = db.format_metadata(book_id, fmt) if not mdata: @@ -186,14 +197,7 @@ def book_fmt(ctx, rd, library_id, db, book_id, fmt): set_metadata(dest, mi, fmt) dest.seek(0) - au = authors_to_string(mi.authors or [_('Unknown')]) - title = mi.title or _('Unknown') - ext = fmt - if ext == 'kepub' and 'Kobo Touch' in rd.inheaders.get('User-Agent', ''): - ext = 'kepub.epub' - fname = '%s - %s_%s.%s' % (title[:30], au[:30], book_id, ext) - fname = ascii_filename(fname).replace('"', '_') - rd.outheaders['Content-Disposition'] = 'attachment; filename="%s"' % fname + rd.outheaders['Content-Disposition'] = 'attachment; filename="%s"' % book_filename(rd, book_id, mi, fmt) return create_file_copy(ctx, rd, 'fmt', library_id, book_id, fmt, mtime, copy_func, extra_etag_data=extra_etag_data) # }}} diff --git a/src/calibre/srv/legacy.py b/src/calibre/srv/legacy.py index 33691838b4..b9150cdc55 100644 --- a/src/calibre/srv/legacy.py +++ b/src/calibre/srv/legacy.py @@ -13,6 +13,7 @@ from calibre import strftime from calibre.constants import __appname__ from calibre.db.view import sanitize_sort_field_name from calibre.ebooks.metadata import authors_to_string +from calibre.srv.content import get, book_filename from calibre.srv.errors import HTTPRedirect, HTTPBadRequest from calibre.srv.routes import endpoint from calibre.srv.utils import get_library_data, http_date @@ -33,6 +34,7 @@ def E(tag, *children, **attribs): attribs = {k.rstrip('_').replace('_', '-'):clean(v) for k, v in attribs.iteritems()} return getattr(E_, tag)(*children, **attribs) + for tag in 'HTML HEAD TITLE LINK DIV IMG BODY OPTION SELECT INPUT FORM SPAN TABLE TR TD A HR META'.split(): setattr(E, tag, partial(E, tag)) tag = tag.lower() @@ -128,7 +130,7 @@ def build_choose_library(ctx, library_map): id='choose_library') -def build_index(books, num, search, sort, order, start, total, url_base, field_metadata, ctx, library_map, library_id): # {{{ +def build_index(rd, books, num, search, sort, order, start, total, url_base, field_metadata, ctx, library_map, library_id): # {{{ logo = E.div(E.img(src=ctx.url_for('/static', what='calibre.png'), alt=__appname__), id='logo') search_box = build_search_box(num, search, sort, order, ctx, field_metadata) navigation = build_navigation(start, num, total, url_base) @@ -160,7 +162,7 @@ def build_index(books, num, search, sort, order, start, total, url_base, field_m s = E.span( E.a( fmt.lower(), - href=ctx.url_for('/get', what=fmt, book_id=book.id, library_id=library_id) + href=ctx.url_for('/legacy/get', what=fmt, book_id=book.id, library_id=library_id, filename=book_filename(rd, book.id, book, fmt)) ), class_='button') s.tail = u'' @@ -241,7 +243,7 @@ def mobile(ctx, rd): q = {b'search':search.encode('utf-8'), b'order':bytes(order), b'sort':sort_by.encode('utf-8'), b'num':bytes(num), 'library_id':library_id} url_base = ctx.url_for('/mobile') + '?' + urlencode(q) lm = {k:v for k, v in library_map.iteritems() if k != library_id} - return build_index(books, num, search, sort_by, order, start, total, url_base, db.field_metadata, ctx, lm, library_id) + return build_index(rd, books, num, search, sort_by, order, start, total, url_base, db.field_metadata, ctx, lm, library_id) # }}} @@ -253,3 +255,8 @@ def browse(ctx, rd, rest): @endpoint('/stanza/{+rest=""}') def stanza(ctx, rd, rest): raise HTTPRedirect(ctx.url_for('/opds')) + + +@endpoint('/legacy/get/{what}/{book_id}/{library_id}/{+filename=""}') +def legacy_get(ctx, rd, what, book_id, library_id, filename): + return get(ctx, rd, what, book_id, library_id)