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) {
|
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});
|
||||||
|
@ -127,10 +133,11 @@ pandora.fs = (function() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
that.storeBlob = function(blob, name, callback) {
|
that.storeBlob = function(blob, name, callback, append) {
|
||||||
requestQuota(blob.size, function() {
|
requestQuota(blob.size, function() {
|
||||||
that.fs.root.getFile(name, {create: true}, function(fileEntry) {
|
that.fs.root.getFile(name, {create: true}, function(fileEntry) {
|
||||||
fileEntry.createWriter(function(fileWriter) {
|
fileEntry.createWriter(function(fileWriter) {
|
||||||
|
append && fileWriter.seek(fileWriter.length);
|
||||||
fileWriter.onwriteend = function(e) {
|
fileWriter.onwriteend = function(e) {
|
||||||
that.local[name] = fileEntry.toURL();
|
that.local[name] = fileEntry.toURL();
|
||||||
callback({progress: 1, url: that.local[name]});
|
callback({progress: 1, url: that.local[name]});
|
||||||
|
@ -167,10 +174,15 @@ pandora.fs = (function() {
|
||||||
//fixme: would be nice to download videos from subdomains,
|
//fixme: would be nice to download videos from subdomains,
|
||||||
// currently throws a cross domain error
|
// currently throws a cross domain error
|
||||||
var name = that.getVideoName(id, resolution, part, track),
|
var name = that.getVideoName(id, resolution, part, track),
|
||||||
|
partialName = 'partial::' + name,
|
||||||
url = '/' + pandora.getVideoURLName(id, resolution, part, track),
|
url = '/' + pandora.getVideoURLName(id, resolution, part, track),
|
||||||
blobs = [], blobSize = 5*1024*1024, total;
|
blobs = [], blobSize = 5*1024*1024, total;
|
||||||
Ox.Log('FS', 'start downloading', url);
|
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) {
|
function partialDownload(offset) {
|
||||||
var end = offset + blobSize;
|
var end = offset + blobSize;
|
||||||
if (total) {
|
if (total) {
|
||||||
|
@ -201,14 +213,18 @@ pandora.fs = (function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
xhr.addEventListener('load', 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) {
|
if (offset + blobSize < total) {
|
||||||
partialDownload(offset + blobSize + 1);
|
partialDownload(offset + blobSize + 1);
|
||||||
} else {
|
} else {
|
||||||
setTimeout(function() {
|
renameFile(partialName, name, function() {
|
||||||
that.storeBlob(new Blob(blobs), name, callback);
|
callback(response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}, true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
xhr.addEventListener('error', function (event) {
|
xhr.addEventListener('error', function (event) {
|
||||||
Ox.print('partial download failed. retrying in 1 second');
|
Ox.print('partial download failed. retrying in 1 second');
|
||||||
|
@ -236,6 +252,9 @@ pandora.fs = (function() {
|
||||||
var n = 0;
|
var n = 0;
|
||||||
if (results.length) {
|
if (results.length) {
|
||||||
results.forEach(function(fileEntry) {
|
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) {
|
fileEntry.getMetadata(function(meta) {
|
||||||
var item = fileEntry.name.split('::')[0],
|
var item = fileEntry.name.split('::')[0],
|
||||||
resolution = parseInt(fileEntry.name.split('::')[1].split('p')[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)));
|
++n == results.length && callback(Ox.values(files).concat(Ox.values(that.downloads)));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(Ox.values(that.downloads));
|
callback(Ox.values(that.downloads));
|
||||||
|
|
Loading…
Reference in a new issue