fix download queue

This commit is contained in:
j 2015-03-21 13:06:16 +05:30
parent 3229c732d4
commit d3ce9198ce

View file

@ -6,6 +6,7 @@ pandora.fs = (function() {
downloads: {}, downloads: {},
enabled: false, enabled: false,
}, },
active,
queue = [], queue = [],
requestedBytes = 100*1024*1024*1024; // 100GB requestedBytes = 100*1024*1024*1024; // 100GB
@ -29,6 +30,40 @@ pandora.fs = (function() {
return pandora.getVideoURLName(id, resolution, part, track).replace('/', '::'); return pandora.getVideoURLName(id, resolution, part, track).replace('/', '::');
} }
function cacheVideo(id, callback) {
active = true;
pandora.api.get({id: id, keys: ['parts']}, function(result) {
var parts = result.data.parts, sizes = [];
downloadPart(0);
function downloadPart(part) {
that.downloadVideoURL(id, pandora.user.ui.videoResolution, part + 1, false, function(result) {
result.progress = 1/parts * (part + result.progress);
that.downloads[id].progress = result.progress;
if (result.cancel) {
that.downloads[id].cancel = result.cancel;
}
if (result.total) {
sizes[part] = result.total;
that.downloads[id].size = Ox.sum(sizes);
}
if (result.url) {
if (part + 1 == parts) {
delete that.downloads[id];
active = false;
if (queue.length) {
cacheVideo.apply(null, queue.shift());
}
} else {
downloadPart(part + 1);
}
}
callback && callback(result);
});
}
});
}
that.cacheVideo = function(id, callback) { that.cacheVideo = function(id, callback) {
if (that.getVideoURL(id, pandora.user.ui.videoResolution, 1) || that.downloads[id]) { if (that.getVideoURL(id, pandora.user.ui.videoResolution, 1) || that.downloads[id]) {
callback({progress: 1}); callback({progress: 1});
@ -46,42 +81,11 @@ pandora.fs = (function() {
size: 0 size: 0
}; };
queue.length (active || queue.length)
? queue.push([id, callback]) ? queue.push([id, callback])
: startDownload(id, callback); : cacheVideo(id, callback);
} }
function startDownload(id, callback) {
pandora.api.get({id: id, keys: ['parts']}, function(result) {
var parts = result.data.parts, sizes = [];
downloadPart(0);
function downloadPart(part) {
that.downloadVideoURL(id, pandora.user.ui.videoResolution, part + 1, false, function(result) {
result.progress = 1/parts * (part + result.progress);
that.downloads[id].progress = result.progress;
if (result.cancel) {
that.downloads[id].cancel = result.cancel;
}
if (result.total) {
sizes[part] = result.total;
that.downloads[id].size = Ox.sum(sizes);
}
if (result.url) {
if (part + 1 == parts) {
delete that.downloads[id];
if (queue.length) {
startDownload.apply(null, queue.shift());
}
} else {
downloadPart(part + 1);
}
}
callback && callback(result);
});
}
});
}
}; };
that.removeVideo = function(id, callback) { that.removeVideo = function(id, callback) {