{% block content %}
-<p>Plik źródłowy: <a href='{{ audiobook.source_file.url }}'>{{ path }}</a>
+<p>Plik źródłowy: <a href='{% url download audiobook.id %}'>{{ path }}</a>
(sha1: <tt>{{ audiobook.source_sha1 }}</tt>).
</p>
<hr/>
{% if audiobook.mp3_file %}
<h2>{% trans "MP3 file" %}</h2>
- <p><a href="{{ audiobook.mp3_file.url }}">{% trans "Download MP3 file." %}</a></p>
+ <p><a href="{% url download audiobook.id 'mp3' %}">{% trans "Download MP3 file." %}</a></p>
{% if audiobook.mp3_published %}
<p>{% trans "Published:" %} {{ audiobook.mp3_published }}</a></p>
{% tags_table audiobook.mp3_published_tags.tags %}
<hr/>
{% if audiobook.ogg_file %}
<h2>{% trans "Ogg Vorbis file" %}</h2>
- <p><a href="{{ audiobook.ogg_file.url }}">{% trans "Download Ogg Vorbis file." %}</a></p>
+ <p><a href="{% url download audiobook.id 'ogg' %}">{% trans "Download Ogg Vorbis file." %}</a></p>
{% if audiobook.ogg_published %}
<p>{% trans "Published:" %} {{ audiobook.ogg_published }}</a></p>
{% tags_table audiobook.ogg_published_tags.tags %}
url(r'^file/(\d+)/$', 'archive.views.file_managed', name="file"),
url(r'^publish/(\d+)/$', 'archive.views.publish', name="publish"),
url(r'^convert/(\d+)/$', 'archive.views.publish', {'publish': False}, name="convert"),
+ url(r'^download/(\d+)/$', 'archive.views.download', name="download"),
+ url(r'^download/(\d+)\.(mp3|ogg)$', 'archive.views.download', name="download"),
url(r'^cancel/(\d+)/$', 'archive.views.cancel_publishing', name="cancel_publishing"),
url(r'^remove_to_archive/(\d+)/$', 'archive.views.remove_to_archive', name="remove_to_archive"),
from datetime import datetime
import os
import os.path
+from urllib import quote
from archive import settings
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required, permission_required
from django.core.urlresolvers import reverse
from django.db.models import Q, Max
-from django.http import Http404
+from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
return redirect(file_managed, aid)
+def download(request, aid, which="source"):
+ if which not in ("source", "mp3", "ogg"):
+ raise Http404
+ audiobook = get_object_or_404(models.Audiobook, id=aid)
+ file_ = getattr(audiobook, "%s_file" % which)
+ if not file_:
+ raise Http404
+ ext = file_.path.rsplit('.', 1)[-1]
+ response = HttpResponse(mimetype='application/force-download')
+
+ response['Content-Disposition'] = "attachment; filename*=UTF-8''%s.%s" % (
+ quote(audiobook.title.encode('utf-8'), safe=''), ext)
+ response['X-Sendfile'] = file_.path.encode('utf-8')
+ return response
+
+
@login_required
def list_unpublished(request):
division = 'unpublished'