Improve download table

This commit is contained in:
yflory 2020-11-20 16:38:18 +01:00
parent f62f447711
commit a29b98783a
5 changed files with 91 additions and 30 deletions

View file

@ -296,6 +296,12 @@
return void CB(void 0, new Uint8Array(xhr.response));
};
xhr.send(null);
return {
cancel: function () {
if (xhr.abort) { xhr.abort(); }
}
};
};
Util.dataURIToBlob = function (dataURI) {

View file

@ -4149,6 +4149,17 @@ define([
data.name = Util.fixFileName(folderName);
data.folderName = Util.fixFileName(folderName) + '.zip';
var uo = manager.user.userObject;
if (sfId && manager.folders[sfId]) {
uo = manager.folders[sfId].userObject;
}
if (uo.getFilesRecursively) {
data.list = uo.getFilesRecursively(folderElement).map(function (el) {
var d = uo.getFileData(el);
return d.channel;
});
}
APP.FM.downloadFolder(data, function (err, obj) {
console.log(err, obj);
console.log('DONE');

View file

@ -53,9 +53,6 @@ define([
var _downloadFile = function (ctx, fData, cb, updateProgress) {
var cancelled = false;
var cancel = function () {
cancelled = true;
};
var href = (fData.href && fData.href.indexOf('#') !== -1) ? fData.href : fData.roHref;
var parsed = Hash.parsePadUrl(href);
var hash = parsed.hash;
@ -63,10 +60,13 @@ define([
var secret = Hash.getSecrets('file', hash, fData.password);
var src = (ctx.fileHost || '') + Hash.getBlobPathFromHex(secret.channel);
var key = secret.keys && secret.keys.cryptKey;
Util.fetch(src, function (err, u8) {
var fetchObj, decryptObj;
fetchObj = Util.fetch(src, function (err, u8) {
if (cancelled) { return; }
if (err) { return void cb('E404'); }
FileCrypto.decrypt(u8, key, function (err, res) {
decryptObj = FileCrypto.decrypt(u8, key, function (err, res) {
if (cancelled) { return; }
if (err) { return void cb(err); }
if (!res.content) { return void cb('EEMPTY'); }
@ -78,8 +78,25 @@ define([
content: res.content,
download: dl
});
}, updateProgress && updateProgress.progress2);
}, updateProgress && updateProgress.progress);
}, function (data) {
if (cancelled) { return; }
if (updateProgress && updateProgress.progress2) {
updateProgress.progress2(data);
}
});
}, function (data) {
if (cancelled) { return; }
if (updateProgress && updateProgress.progress) {
updateProgress.progress(data);
}
});
var cancel = function () {
cancelled = true;
if (fetchObj && fetchObj.cancel) { fetchObj.cancel(); }
if (decryptObj && decryptObj.cancel) { decryptObj.cancel(); }
};
return {
cancel: cancel
};
@ -162,10 +179,10 @@ define([
if (ctx.stop) { return; }
if (to) { clearTimeout(to); }
//setTimeout(g, 2000);
g();
w();
ctx.done++;
ctx.updateProgress('download', {max: ctx.max, current: ctx.done});
g();
w();
};
var error = function (err) {
@ -312,13 +329,14 @@ define([
delete ctx.zip;
};
return {
stop: stop
stop: stop,
cancel: stop
};
};
var _downloadFolder = function (ctx, data, cb, updateProgress) {
create(data, ctx.get, ctx.fileHost, function (blob, errors) {
return create(data, ctx.get, ctx.fileHost, function (blob, errors) {
if (errors && errors.length) { console.error(errors); } // TODO show user errors
var dl = function () {
saveAs(blob, data.folderName);
@ -332,8 +350,11 @@ define([
if (typeof progress.current !== "number") { return; }
updateProgress.folderProgress(progress.current / progress.max);
}
else if (state === "compressing") {
updateProgress.folderProgress(2);
}
else if (state === "done") {
updateProgress.folderProgress(1);
updateProgress.folderProgress(3);
}
});
};

View file

@ -47,8 +47,9 @@ define([
return 'cp-fileupload-element-' + String(Math.random()).substring(2);
};
Messages.fileTableHeader = "Downloads and uploads"; // XXX
var tableHeader = h('div.cp-fileupload-header', [
h('div.cp-fileupload-header-title', h('span', Messages.fileuploadHeader || 'Uploaded files')),
h('div.cp-fileupload-header-title', h('span', Messages.fileTableHeader)),
h('div.cp-fileupload-header-close', h('span.fa.fa-times')),
]);
@ -262,7 +263,8 @@ define([
// name
$('<td>').append($link).appendTo($tr);
// size
$('<td>').text(UIElements.prettySize(estimate)).appendTo($tr);
var size = estimate ? UIElements.prettySize(estimate) : '';
$(h('td.cp-fileupload-size')).text(size).appendTo($tr);
// progress
$('<td>', {'class': 'cp-fileupload-table-progress'}).append($progressContainer).appendTo($tr);
// cancel
@ -590,12 +592,11 @@ define([
queue.next();
};
/*
var cancelled = function () {
$row.find('.cp-fileupload-table-cancel').addClass('cancelled').html('').append(h('span.fa.fa-minus'));
queue.inProgress = false;
queue.next();
};*/
};
/**
* Update progress in the download panel, for downloading a file
@ -627,8 +628,21 @@ define([
* As updateDLProgress but for folders
* @param {number} progressValue Progression of download, between 0 and 1
*/
Messages.download_zip = "Building ZIP file..."; // XXX
Messages.download_zip_file = "File {0}/{1}"; // XXX
var updateProgress = function (progressValue) {
var text = Math.round(progressValue*100) + '%';
if (Array.isArray(data.list)) {
text = Messages._getKey('download_zip_file', [Math.round(progressValue * data.list.length), data.list.length]);
}
if (progressValue === 2) {
text = Messages.download_zip;
progressValue = 1;
}
if (progressValue === 3) {
text = "100%";
progressValue = 1;
}
$pv.text(text);
$pb.css({
width: (progressValue * 100) + '%'
@ -641,7 +655,8 @@ define([
get: common.getPad,
sframeChan: sframeChan,
};
downloadFunction(ctx, data, function (err, obj) {
var dl = downloadFunction(ctx, data, function (err, obj) {
$link.prepend($('<span>', {'class': 'fa fa-external-link'}))
.attr('href', '#')
.click(function (e) {
@ -657,19 +672,17 @@ define([
folderProgress: updateProgress,
});
/*
var $cancel = $('<span>', {'class': 'cp-fileupload-table-cancel-button fa fa-times'}).click(function () {
dl.cancel();
$cancel.remove();
$row.find('.cp-fileupload-table-progress-value').text(Messages.upload_cancelled);
cancelled();
});
*/
$row.find('.cp-fileupload-table-cancel')
.html('')
.append(h('span.fa.fa-minus'));
//.append($cancel);
var $cancel = $row.find('.cp-fileupload-table-cancel').html('');
if (dl && dl.cancel) {
$('<span>', {
'class': 'cp-fileupload-table-cancel-button fa fa-times'
}).click(function () {
dl.cancel();
$cancel.remove();
$row.find('.cp-fileupload-table-progress-value').text(Messages.upload_cancelled);
cancelled();
}).appendTo($cancel);
}
};
File.downloadFile = function (fData, cb) {

View file

@ -128,6 +128,11 @@ define([
metadata: undefined,
};
var cancelled = false;
var cancel = function () {
cancelled = true;
};
var metaBox = new Uint8Array(u8.subarray(2, 2 + metadataLength));
var metaChunk = Nacl.secretbox.open(metaBox, nonce, key);
@ -168,6 +173,7 @@ define([
var chunks = [];
var again = function () {
if (cancelled) { return; }
takeChunk(function (e, plaintext) {
if (e) {
return setTimeout(function () {
@ -188,6 +194,10 @@ define([
};
again();
return {
cancel: cancel
};
};
// metadata