pretty filenames for download
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 11 Jul 2012 14:31:29 +0000 (16:31 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 11 Jul 2012 14:48:13 +0000 (16:48 +0200)
apps/archive/templates/archive/file_managed.html
apps/archive/urls.py
apps/archive/views.py

index 5a54adb..01847a6 100755 (executable)
@@ -4,7 +4,7 @@
 
 {% 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>
 
@@ -65,7 +65,7 @@
 <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 %}
@@ -79,7 +79,7 @@
 <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 %}
index 3f7839a..a67bd43 100644 (file)
@@ -13,6 +13,8 @@ urlpatterns = patterns('',
     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"),
 
index 369e7e4..e76da74 100644 (file)
@@ -3,13 +3,14 @@
 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
 
@@ -183,6 +184,22 @@ def cancel_publishing(request, aid):
     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'