From f3c9d7b7afd241134eefd9ee03c31d9fbe48b952 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 14 Sep 2013 14:09:37 +0000 Subject: [PATCH] add option to have direct download instead of torrent --- pandora/config.0xdb.jsonc | 1 + pandora/config.indiancinema.jsonc | 1 + pandora/config.padma.jsonc | 1 + pandora/config.pandora.jsonc | 2 +- pandora/item/urls.py | 3 +++ pandora/item/views.py | 30 ++++++++++++++++++++++++++++++ static/js/editor.js | 2 +- static/js/player.js | 2 +- static/js/utils.js | 3 +++ 9 files changed, 42 insertions(+), 3 deletions(-) diff --git a/pandora/config.0xdb.jsonc b/pandora/config.0xdb.jsonc index c733a8db..beed13d7 100644 --- a/pandora/config.0xdb.jsonc +++ b/pandora/config.0xdb.jsonc @@ -890,6 +890,7 @@ // fixme: this should include colors "userLevels": ["guest", "member", "friend", "staff", "admin"], "video": { + "torrent": true, "formats": ["webm", "mp4"], // fixme: this should be named "ratio" or "defaultRatio", // as it also applies to clip lists (on load) diff --git a/pandora/config.indiancinema.jsonc b/pandora/config.indiancinema.jsonc index ff5b16c7..11dc817c 100644 --- a/pandora/config.indiancinema.jsonc +++ b/pandora/config.indiancinema.jsonc @@ -895,6 +895,7 @@ // fixme: this should include colors "userLevels": ["guest", "member", "student", "staff", "admin"], "video": { + "torrent": true, "formats": ["webm", "mp4"], "previewRatio": 1.375, "resolutions": [240, 480] diff --git a/pandora/config.padma.jsonc b/pandora/config.padma.jsonc index d1895f8a..b735c451 100644 --- a/pandora/config.padma.jsonc +++ b/pandora/config.padma.jsonc @@ -774,6 +774,7 @@ }, "userLevels": ["guest", "member", "staff", "admin"], "video": { + "torrent": true, "formats": ["webm", "mp4"], "previewRatio": 1.3333333333, //supported resolutions are diff --git a/pandora/config.pandora.jsonc b/pandora/config.pandora.jsonc index 8d0461c2..dfe4657a 100644 --- a/pandora/config.pandora.jsonc +++ b/pandora/config.pandora.jsonc @@ -691,7 +691,7 @@ }, "userLevels": ["guest", "member", "staff", "admin"], "video": { - "download": true, + "torrent": true, //supported formats: webm, mp4 "formats": ["webm", "mp4"], "previewRatio": 1.3333333333, diff --git a/pandora/item/urls.py b/pandora/item/urls.py index 637e00bd..0fcd8fc9 100644 --- a/pandora/item/urls.py +++ b/pandora/item/urls.py @@ -19,6 +19,9 @@ urlpatterns = patterns("item.views", (r'^(?P[A-Z0-9].*)/torrent$', 'torrent'), (r'^(?P[A-Z0-9].*)/torrent/(?P.*?)$', 'torrent'), + #download + (r'^(?P[A-Z0-9].*)/download/$', 'download'), + #export (r'^(?P[A-Z0-9].*)/json$', 'item_json'), (r'^(?P[A-Z0-9].*)/xml$', 'item_xml'), diff --git a/pandora/item/views.py b/pandora/item/views.py index 9cfcb567..fa8b5676 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -831,6 +831,36 @@ def timeline(request, id, size, position=-1, format='jpg', mode=None): path = timeline() return HttpFileResponse(path, content_type='image/jpeg') +def download(request, id, index=1): + item = get_object_or_404(models.Item, itemId=id) + resolution = max(settings.CONFIG['video']['resolutions']) + format = 'webm' + + if not item.access(request.user): + return HttpResponseForbidden() + if index: + index = int(index) - 1 + else: + index = 0 + streams = item.streams() + if index + 1 > streams.count(): + raise Http404 + stream = streams[index].get(resolution, format) + if not stream.available or not stream.media: + raise Http404 + path = stream.media.path + content_type = mimetypes.guess_type(path)[0] + ext = os.path.splitext(path)[-1] + filename = "%s - %s %s%s" % ( + item.get('title'), + settings.SITENAME, + item.itemId, + ext + ) + response = HttpFileResponse(path, content_type=content_type) + response['Content-Disposition'] = "attachment; filename*=UTF-8''%s" % quote(filename.encode('utf-8')) + return response + def torrent(request, id, filename=None): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): diff --git a/static/js/editor.js b/static/js/editor.js index ecb6610e..bbb4ba92 100644 --- a/static/js/editor.js +++ b/static/js/editor.js @@ -127,7 +127,7 @@ pandora.ui.editor = function(data) { pandora.$ui[dialog] = pandora.ui[dialog](data).open(); }, downloadvideo: function(data) { - document.location.href = '/' + ui.item + '/torrent/'; + document.location.href = pandora.getDownloadLink(ui.item); }, downloadselection: function(data) { document.location.href = '/' + ui.item diff --git a/static/js/player.js b/static/js/player.js index 17728fb7..ed930ae0 100644 --- a/static/js/player.js +++ b/static/js/player.js @@ -84,7 +84,7 @@ pandora.ui.player = function(data) { }), 'clip'); }, downloadvideo: function(data) { - document.location.href = '/' + ui.item + '/torrent/'; + document.location.href = pandora.getDownloadLink(ui.item); }, find: function(data) { pandora.UI.set({itemFind: data.find}); diff --git a/static/js/utils.js b/static/js/utils.js index 21727acb..9a92cf0d 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -928,6 +928,9 @@ pandora.getClipVideos = function(clip, resolution) { }; }()); +pandora.getDownloadLink = function(item) { + return '/' + item + (pandora.site.video.torrent ? '/torrent/' : '/download/'); +} pandora.getEditTooltip = function(title) { return function(e) {