support caching local files, lookup by oshash

This commit is contained in:
j 2016-02-14 00:32:14 +05:30
parent 81ade83945
commit b121b58a86
4 changed files with 65 additions and 18 deletions

View file

@ -711,6 +711,16 @@ class Stream(models.Model):
_self.update_status(ok, error) _self.update_status(ok, error)
return _self 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): def update_status(self, ok, error):
if ok: if ok:
if not self.media: if not self.media:

View file

@ -666,15 +666,21 @@ def getMediaInfo(request, data):
id: string // oshash of media file id: string // oshash of media file
} }
returns { returns {
item: string, // item id item: string, // item id
file: string // oshash of source file file: string // oshash of source file
resolution: int // stream resolution
index: int // stream index
} }
''' '''
f = None f = None
resolution = None
index = None
qs = models.Stream.objects.filter(oshash=data['id']) qs = models.Stream.objects.filter(oshash=data['id'])
if qs.count() > 0: if qs.count() > 0:
s = qs[0] s = qs[0]
f = s.file f = s.file
resolution = s.resolution
index = s.get_index()
else: else:
qs = models.File.objects.filter(oshash=data['id']) qs = models.File.objects.filter(oshash=data['id'])
if qs.count() > 0: if qs.count() > 0:
@ -685,6 +691,10 @@ def getMediaInfo(request, data):
'file': f.oshash, 'file': f.oshash,
'item': f.item.public_id 'item': f.item.public_id
} }
if resolution:
response['data']['resolution'] = resolution
if index:
response['data']['index'] = index
return render_to_json_response(response) return render_to_json_response(response)
actions.register(getMediaInfo) actions.register(getMediaInfo)

View file

@ -134,17 +134,41 @@ pandora.ui.cacheDialog = function() {
.appendTo($item), .appendTo($item),
$fileButton = Ox.FileButton({ $fileButton = Ox.FileButton({
title: 'Select Video...', title: 'Select Videos...',
width: 128, width: 128,
disabled: pandora.user.ui.section != 'items' disabled: false,
|| pandora.user.ui.item == ''
|| !!pandora.fs.getVideoURL(pandora.user.ui.item, pandora.user.ui.videoResolution, 1)
}) })
.css({ .css({
margin: '8px' margin: '8px'
}) })
.bindEvent({ .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), .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() { function updateActiveDownloads() {
pandora.fs.getVideos(function(files) { pandora.fs.getVideos(function(files) {
files.forEach(function(file) { files.forEach(function(file) {

View file

@ -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) { that.getVideoName = function(id, resolution, part, track) {
return pandora.getVideoURLName(id, resolution, part, track).replace(id + '\/', id + '::' + resolution + '/'); return pandora.getVideoURLName(id, resolution, part, track).replace(id + '\/', id + '::' + resolution + '/');
}; };