From: Jan Szejko Date: Thu, 15 Dec 2016 15:02:42 +0000 (+0100) Subject: add mobi download X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/b103d6f69bec99cbcc3eb2f0509d950f0a1c0642 add mobi download --- diff --git a/apps/catalogue/templates/catalogue/book_text.html b/apps/catalogue/templates/catalogue/book_text.html index 83e40032..0444bcc6 100644 --- a/apps/catalogue/templates/catalogue/book_text.html +++ b/apps/catalogue/templates/catalogue/book_text.html @@ -154,14 +154,16 @@ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00000000', e
{% trans "Download PDF" %} -{% trans "Download EPUB" %} + {% trans "Download MOBI" %}
diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index 148851b2..6fd00bbd 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -36,4 +36,5 @@ urlpatterns = patterns( url(r'^(?P\d+)/rev(?P\d+)/preview/$', 'book_html', {'preview': True}, name="catalogue_preview_rev"), url(r'^(?P\d+)/rev(?P\d+)/pdf/$', 'book_pdf', name="catalogue_pdf"), url(r'^(?P\d+)/rev(?P\d+)/epub/$', 'book_epub', name="catalogue_epub"), + url(r'^(?P\d+)/rev(?P\d+)/mobi/$', 'book_mobi', name="catalogue_mobi"), ) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 1d1b36ec..e3d98d8c 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -6,6 +6,8 @@ import logging import os import shutil +import subprocess +from tempfile import NamedTemporaryFile from django.conf import settings from django.contrib import auth @@ -250,6 +252,38 @@ def book_epub(request, pk, rev_pk): return serve_file(epub_file.get_filename(), '%d.epub' % doc.pk, 'application/epub+zip') +@never_cache +def book_mobi(request, pk, rev_pk): + from librarian.utils import Context + from librarian.document import Document as SST + from librarian.formats.epub import EpubFormat + + doc = get_object_or_404(Document, pk=pk) + rev = get_object_or_404(Revision, pk=rev_pk) + + sst = SST.from_string(rev.materialize()) + + ctx = Context( + files_path='http://%s/media/dynamic/uploads/%s/' % (request.get_host(), pk), + source_url='http://%s%s' % (request.get_host(), reverse('catalogue_html', args=[doc.pk])), + ) + if doc.owner_organization is not None and doc.owner_organization.logo: + ctx.cover_logo = 'http://%s%s' % (request.get_host(), doc.owner_organization.logo.url) + try: + epub_file = EpubFormat(sst).build(ctx) + except BuildError as e: + from django.http import HttpResponse + return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400') + + output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi', delete=False) + output_file.close() + subprocess.check_call( + ['ebook-convert', epub_file.get_filename(), output_file.name, '--no-inline-toc']) + + from catalogue.ebook_utils import serve_file + return serve_file(output_file.name, '%d.mobi' % doc.pk, 'application/epub+zip') + + # @never_cache # def revision(request, slug, chunk=None): # try: