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;
|
||||||
if (offset + blobSize < total) {
|
setTimeout(function() {
|
||||||
partialDownload(offset + blobSize + 1);
|
that.storeBlob(blob, partialName, function(response) {
|
||||||
} else {
|
if (offset + blobSize < total) {
|
||||||
setTimeout(function() {
|
partialDownload(offset + blobSize + 1);
|
||||||
that.storeBlob(new Blob(blobs), name, callback);
|
} else {
|
||||||
});
|
renameFile(partialName, name, function() {
|
||||||
}
|
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,23 +252,27 @@ pandora.fs = (function() {
|
||||||
var n = 0;
|
var n = 0;
|
||||||
if (results.length) {
|
if (results.length) {
|
||||||
results.forEach(function(fileEntry) {
|
results.forEach(function(fileEntry) {
|
||||||
fileEntry.getMetadata(function(meta) {
|
if (Ox.startsWith(fileEntry.name, 'partial::')) {
|
||||||
var item = fileEntry.name.split('::')[0],
|
|
||||||
resolution = parseInt(fileEntry.name.split('::')[1].split('p')[0]),
|
|
||||||
part = parseInt(fileEntry.name.split('::')[1].split('p')[1].split('.')[0]),
|
|
||||||
key = item + '::' + resolution;
|
|
||||||
if (!(that.downloads && that.downloads[item])) {
|
|
||||||
files[key] = Ox.extend(files[key] || {}, {
|
|
||||||
added: meta.modificationTime,
|
|
||||||
id: item + '::' + resolution,
|
|
||||||
item: item,
|
|
||||||
progress: 1,
|
|
||||||
resolution: resolution,
|
|
||||||
size: files[key] ? files[key].size + meta.size: meta.size
|
|
||||||
});
|
|
||||||
}
|
|
||||||
++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 {
|
||||||
|
fileEntry.getMetadata(function(meta) {
|
||||||
|
var item = fileEntry.name.split('::')[0],
|
||||||
|
resolution = parseInt(fileEntry.name.split('::')[1].split('p')[0]),
|
||||||
|
part = parseInt(fileEntry.name.split('::')[1].split('p')[1].split('.')[0]),
|
||||||
|
key = item + '::' + resolution;
|
||||||
|
if (!(that.downloads && that.downloads[item])) {
|
||||||
|
files[key] = Ox.extend(files[key] || {}, {
|
||||||
|
added: meta.modificationTime,
|
||||||
|
id: item + '::' + resolution,
|
||||||
|
item: item,
|
||||||
|
progress: 1,
|
||||||
|
resolution: resolution,
|
||||||
|
size: files[key] ? files[key].size + meta.size: meta.size
|
||||||
|
});
|
||||||
|
}
|
||||||
|
++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