From: Radek Czajka Date: Wed, 11 Jul 2012 14:31:29 +0000 (+0200) Subject: pretty filenames for download X-Git-Url: https://git.mdrn.pl/audio.git/commitdiff_plain/82a29bd6e0f7da1aa759d7f4481e6cf3c736f981?ds=sidebyside;hp=c175a7540d20336179dfd453183bfe69e6f5827e pretty filenames for download --- diff --git a/apps/archive/templates/archive/file_managed.html b/apps/archive/templates/archive/file_managed.html index 5a54adb..01847a6 100755 --- a/apps/archive/templates/archive/file_managed.html +++ b/apps/archive/templates/archive/file_managed.html @@ -4,7 +4,7 @@ {% block content %} -

Plik źródłowy: {{ path }} +

Plik źródłowy: {{ path }} (sha1: {{ audiobook.source_sha1 }}).

@@ -65,7 +65,7 @@
{% if audiobook.mp3_file %}

{% trans "MP3 file" %}

-

{% trans "Download MP3 file." %}

+

{% trans "Download MP3 file." %}

{% if audiobook.mp3_published %}

{% trans "Published:" %} {{ audiobook.mp3_published }}

{% tags_table audiobook.mp3_published_tags.tags %} @@ -79,7 +79,7 @@
{% if audiobook.ogg_file %}

{% trans "Ogg Vorbis file" %}

-

{% trans "Download Ogg Vorbis file." %}

+

{% trans "Download Ogg Vorbis file." %}

{% if audiobook.ogg_published %}

{% trans "Published:" %} {{ audiobook.ogg_published }}

{% tags_table audiobook.ogg_published_tags.tags %} diff --git a/apps/archive/urls.py b/apps/archive/urls.py index 3f7839a..a67bd43 100644 --- a/apps/archive/urls.py +++ b/apps/archive/urls.py @@ -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"), diff --git a/apps/archive/views.py b/apps/archive/views.py index 369e7e4..e76da74 100644 --- a/apps/archive/views.py +++ b/apps/archive/views.py @@ -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'