From b121b58a862466e9eb4b6534b63fa6a42410e52b Mon Sep 17 00:00:00 2001 From: j Date: Sun, 14 Feb 2016 00:32:14 +0530 Subject: [PATCH] support caching local files, lookup by oshash --- pandora/archive/models.py | 10 +++++++++ pandora/archive/views.py | 14 ++++++++++-- static/js/cacheDialog.js | 45 +++++++++++++++++++++++++-------------- static/js/fs.js | 14 ++++++++++++ 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/pandora/archive/models.py b/pandora/archive/models.py index 49e4a00d..323bd12b 100644 --- a/pandora/archive/models.py +++ b/pandora/archive/models.py @@ -711,6 +711,16 @@ class Stream(models.Model): _self.update_status(ok, error) return _self + def get_index(self): + index = 1 + for s in self.file.item.streams(): + if self.source and self.source == s: + return index + if s == self: + return index + index += 1 + return None + def update_status(self, ok, error): if ok: if not self.media: diff --git a/pandora/archive/views.py b/pandora/archive/views.py index ac48bd07..8c406b22 100644 --- a/pandora/archive/views.py +++ b/pandora/archive/views.py @@ -666,15 +666,21 @@ def getMediaInfo(request, data): id: string // oshash of media file } returns { - item: string, // item id - file: string // oshash of source file + item: string, // item id + file: string // oshash of source file + resolution: int // stream resolution + index: int // stream index } ''' f = None + resolution = None + index = None qs = models.Stream.objects.filter(oshash=data['id']) if qs.count() > 0: s = qs[0] f = s.file + resolution = s.resolution + index = s.get_index() else: qs = models.File.objects.filter(oshash=data['id']) if qs.count() > 0: @@ -685,6 +691,10 @@ def getMediaInfo(request, data): 'file': f.oshash, 'item': f.item.public_id } + if resolution: + response['data']['resolution'] = resolution + if index: + response['data']['index'] = index return render_to_json_response(response) actions.register(getMediaInfo) diff --git a/static/js/cacheDialog.js b/static/js/cacheDialog.js index ad651548..28d6bd5f 100644 --- a/static/js/cacheDialog.js +++ b/static/js/cacheDialog.js @@ -134,17 +134,41 @@ pandora.ui.cacheDialog = function() { .appendTo($item), $fileButton = Ox.FileButton({ - title: 'Select Video...', + title: 'Select Videos...', width: 128, - disabled: pandora.user.ui.section != 'items' - || pandora.user.ui.item == '' - || !!pandora.fs.getVideoURL(pandora.user.ui.item, pandora.user.ui.videoResolution, 1) + disabled: false, }) .css({ margin: '8px' }) .bindEvent({ - click: selectVideo + click: function selectVideos(data) { + $fileButton.options({disabled: true}); + var files = []; + for (var i = 0; i < data.files.length; i++) { + files.push(data.files[i]); + } + Ox.serialForEach(files, function(blob, index, array, next) { + Ox.oshash(blob, function(oshash) { + pandora.api.getMediaInfo({id: oshash}, function(result) { + var index = parseInt(result.data.index || 1); + if (result.data.item && result.data.resolution) { + pandora.fs.cacheBlob(blob, result.data.item, result.data.resolution, index, function(response) { + next(); + }); + } else { + next(); + } + }); + }); + }, function() { + $fileButton.options({disabled: false}); + getCachedVideos(function(files) { + cachedVideos = Ox.api(files); + $list.reloadList(true); + }); + }); + } }) .appendTo($item), @@ -310,17 +334,6 @@ pandora.ui.cacheDialog = function() { }); } - function selectVideo(data) { - var blob = data.files[0], - name = pandora.fs.getVideoName(pandora.user.ui.item, pandora.user.ui.videoResolution, 1); - pandora.fs.storeBlob(blob, name, function() { - getCachedVideos(function(files) { - cachedVideos = Ox.api(files); - cachedVideos(data, callback); - }); - }); - } - function updateActiveDownloads() { pandora.fs.getVideos(function(files) { files.forEach(function(file) { diff --git a/static/js/fs.js b/static/js/fs.js index 73998d41..47f4bc32 100644 --- a/static/js/fs.js +++ b/static/js/fs.js @@ -170,6 +170,20 @@ pandora.fs = (function() { }; + that.cacheBlob = function(blob, id, resolution, index, callback) { + var key = id[0] + '/' + id + '::' + resolution, + name = that.getVideoName(id, resolution, index); + createTree(key, function(folder) { + that.storeBlob(blob, name[0] + '/' + name, function(response) { + if (response.progress == -1) { + callback(response); + } else { + that.storeBlob(new Blob(['ok']), key + '/done', callback); + } + }); + }); + }; + that.getVideoName = function(id, resolution, part, track) { return pandora.getVideoURLName(id, resolution, part, track).replace(id + '\/', id + '::' + resolution + '/'); };