forked from 0x2620/pandora
store stream in chunks and continue existing partial downloads if they exist
This commit is contained in:
parent
a4e0d78d54
commit
82a6caf4ac
1 changed files with 46 additions and 26 deletions
|
@ -65,6 +65,12 @@ pandora.fs = (function() {
|
|||
});
|
||||
}
|
||||
|
||||
function renameFile(old, name, callback) {
|
||||
that.fs.root.getFile(old, {}, function(fileEntry) {
|
||||
fileEntry.moveTo(that.fs.root, name);
|
||||
}, callback);
|
||||
}
|
||||
|
||||
that.cacheVideo = function(id, callback) {
|
||||
if (that.getVideoURL(id, pandora.user.ui.videoResolution, 1) || that.downloads[id]) {
|
||||
callback({progress: 1});
|
||||
|
@ -127,10 +133,11 @@ pandora.fs = (function() {
|
|||
}
|
||||
};
|
||||
|
||||
that.storeBlob = function(blob, name, callback) {
|
||||
that.storeBlob = function(blob, name, callback, append) {
|
||||
requestQuota(blob.size, function() {
|
||||
that.fs.root.getFile(name, {create: true}, function(fileEntry) {
|
||||
fileEntry.createWriter(function(fileWriter) {
|
||||
append && fileWriter.seek(fileWriter.length);
|
||||
fileWriter.onwriteend = function(e) {
|
||||
that.local[name] = fileEntry.toURL();
|
||||
callback({progress: 1, url: that.local[name]});
|
||||
|
@ -167,10 +174,15 @@ pandora.fs = (function() {
|
|||
//fixme: would be nice to download videos from subdomains,
|
||||
// currently throws a cross domain error
|
||||
var name = that.getVideoName(id, resolution, part, track),
|
||||
partialName = 'partial::' + name,
|
||||
url = '/' + pandora.getVideoURLName(id, resolution, part, track),
|
||||
blobs = [], blobSize = 5*1024*1024, total;
|
||||
Ox.Log('FS', 'start downloading', url);
|
||||
partialDownload(0);
|
||||
that.fs.root.getFile(partialName, {create: true}, function(fileEntry) {
|
||||
fileEntry.getMetadata(function(meta) {
|
||||
partialDownload(meta.size);
|
||||
});
|
||||
});
|
||||
function partialDownload(offset) {
|
||||
var end = offset + blobSize;
|
||||
if (total) {
|
||||
|
@ -201,14 +213,18 @@ pandora.fs = (function() {
|
|||
}
|
||||
});
|
||||
xhr.addEventListener('load', function() {
|
||||
blobs.push(xhr.response);
|
||||
var blob = xhr.response;
|
||||
setTimeout(function() {
|
||||
that.storeBlob(blob, partialName, function(response) {
|
||||
if (offset + blobSize < total) {
|
||||
partialDownload(offset + blobSize + 1);
|
||||
} else {
|
||||
setTimeout(function() {
|
||||
that.storeBlob(new Blob(blobs), name, callback);
|
||||
renameFile(partialName, name, function() {
|
||||
callback(response);
|
||||
});
|
||||
}
|
||||
}, true);
|
||||
});
|
||||
});
|
||||
xhr.addEventListener('error', function (event) {
|
||||
Ox.print('partial download failed. retrying in 1 second');
|
||||
|
@ -236,6 +252,9 @@ pandora.fs = (function() {
|
|||
var n = 0;
|
||||
if (results.length) {
|
||||
results.forEach(function(fileEntry) {
|
||||
if (Ox.startsWith(fileEntry.name, 'partial::')) {
|
||||
++n == results.length && callback(Ox.values(files).concat(Ox.values(that.downloads)));
|
||||
} else {
|
||||
fileEntry.getMetadata(function(meta) {
|
||||
var item = fileEntry.name.split('::')[0],
|
||||
resolution = parseInt(fileEntry.name.split('::')[1].split('p')[0]),
|
||||
|
@ -253,6 +272,7 @@ pandora.fs = (function() {
|
|||
}
|
||||
++n == results.length && callback(Ox.values(files).concat(Ox.values(that.downloads)));
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback(Ox.values(that.downloads));
|
||||
|
|
Loading…
Reference in a new issue