|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
define([
|
|
|
|
|
'jquery',
|
|
|
|
|
'/file/file-crypto.js',
|
|
|
|
|
'/common/make-backup.js',
|
|
|
|
|
'/common/common-thumbnail.js',
|
|
|
|
|
'/common/common-interface.js',
|
|
|
|
|
'/common/common-ui-elements.js',
|
|
|
|
@ -11,9 +12,8 @@ define([
|
|
|
|
|
|
|
|
|
|
'/bower_components/file-saver/FileSaver.min.js',
|
|
|
|
|
'/bower_components/tweetnacl/nacl-fast.min.js',
|
|
|
|
|
], function ($, FileCrypto, Thumb, UI, UIElements, Util, Hash, h, Messages) {
|
|
|
|
|
], function ($, FileCrypto, MakeBackup, Thumb, UI, UIElements, Util, Hash, h, Messages) {
|
|
|
|
|
var Nacl = window.nacl;
|
|
|
|
|
var saveAs = window.saveAs;
|
|
|
|
|
var module = {};
|
|
|
|
|
|
|
|
|
|
var blobToArrayBuffer = function (blob, cb) {
|
|
|
|
@ -446,88 +446,73 @@ define([
|
|
|
|
|
|
|
|
|
|
createUploader(config.dropArea, config.hoverArea, config.body);
|
|
|
|
|
|
|
|
|
|
File.downloadFile = function (fData, cb) {
|
|
|
|
|
console.error("SFC FILE -DOWNLOAD FILE");
|
|
|
|
|
console.log("fData", fData);
|
|
|
|
|
var parsed = Hash.parsePadUrl(fData.href || fData.roHref);
|
|
|
|
|
var hash = parsed.hash;
|
|
|
|
|
var name = fData.filename || fData.title;
|
|
|
|
|
var secret = Hash.getSecrets('file', hash, fData.password);
|
|
|
|
|
var src = Hash.getBlobPathFromHex(secret.channel);
|
|
|
|
|
var key = secret.keys && secret.keys.cryptKey;
|
|
|
|
|
console.log('key', key);
|
|
|
|
|
console.log('src', src);
|
|
|
|
|
console.log('secret', secret);
|
|
|
|
|
console.log('name', name);
|
|
|
|
|
console.log('hash', hash);
|
|
|
|
|
console.log('parsed', parsed);
|
|
|
|
|
common.getFileSize(secret.channel, function (e, data) {
|
|
|
|
|
console.warn("GET FILE SIZE");
|
|
|
|
|
console.log("data", data);
|
|
|
|
|
var todo = function (file) {
|
|
|
|
|
console.warn("TODO");
|
|
|
|
|
console.log("file", file);
|
|
|
|
|
if (queue.inProgress) { return; }
|
|
|
|
|
queue.inProgress = true;
|
|
|
|
|
var id = file.id;
|
|
|
|
|
|
|
|
|
|
var $row = $table.find('tr[id="'+id+'"]');
|
|
|
|
|
var $pv = $row.find('.cp-fileupload-table-progress-value');
|
|
|
|
|
var $pb = $row.find('.cp-fileupload-table-progress-container');
|
|
|
|
|
var $pc = $row.find('.cp-fileupload-table-progress');
|
|
|
|
|
var $link = $row.find('.cp-fileupload-table-link');
|
|
|
|
|
|
|
|
|
|
var done = function () {
|
|
|
|
|
$row.find('.cp-fileupload-table-cancel').text('-');
|
|
|
|
|
queue.inProgress = false;
|
|
|
|
|
queue.next();
|
|
|
|
|
};
|
|
|
|
|
var updateProgressbar = function (file, data, downloadFunction, cb) {
|
|
|
|
|
if (queue.inProgress) { return; }
|
|
|
|
|
queue.inProgress = true;
|
|
|
|
|
var id = file.id;
|
|
|
|
|
|
|
|
|
|
var updateDLProgress = function (progressValue) {
|
|
|
|
|
var text = Math.round(progressValue*100) + '%';
|
|
|
|
|
text += ' ('+ Messages.download_step1 +'...)';
|
|
|
|
|
$pv.text(text);
|
|
|
|
|
$pb.css({
|
|
|
|
|
width: progressValue * $pc.width()+'px'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var updateProgress = function (progressValue) {
|
|
|
|
|
var text = Math.round(progressValue*100) + '%';
|
|
|
|
|
text += progressValue === 1 ? '' : ' ('+ Messages.download_step2 +'...)';
|
|
|
|
|
$pv.text(text);
|
|
|
|
|
$pb.css({
|
|
|
|
|
width: progressValue * $pc.width()+'px'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var $row = $table.find('tr[id="'+id+'"]');
|
|
|
|
|
var $pv = $row.find('.cp-fileupload-table-progress-value');
|
|
|
|
|
var $pb = $row.find('.cp-fileupload-table-progress-container');
|
|
|
|
|
var $pc = $row.find('.cp-fileupload-table-progress');
|
|
|
|
|
var $link = $row.find('.cp-fileupload-table-link');
|
|
|
|
|
|
|
|
|
|
var dl = module.downloadFile(fData, function (err, obj) {
|
|
|
|
|
$link.prepend($('<span>', {'class': 'fa fa-external-link'}))
|
|
|
|
|
.attr('href', '#')
|
|
|
|
|
.click(function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
obj.download();
|
|
|
|
|
});
|
|
|
|
|
done();
|
|
|
|
|
if (obj) { obj.download(); }
|
|
|
|
|
cb(err, obj);
|
|
|
|
|
}, {
|
|
|
|
|
src: src,
|
|
|
|
|
key: key,
|
|
|
|
|
name: name,
|
|
|
|
|
progress: updateDLProgress,
|
|
|
|
|
progress2: updateProgress,
|
|
|
|
|
});
|
|
|
|
|
var done = function () {
|
|
|
|
|
$row.find('.cp-fileupload-table-cancel').text('-');
|
|
|
|
|
queue.inProgress = false;
|
|
|
|
|
queue.next();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
$row.find('.cp-fileupload-table-cancel').html('').append($cancel);
|
|
|
|
|
};
|
|
|
|
|
var updateDLProgress = function (progressValue) {
|
|
|
|
|
var text = Math.round(progressValue * 100) + '%';
|
|
|
|
|
text += ' (' + Messages.download_step1 + '...)';
|
|
|
|
|
$pv.text(text);
|
|
|
|
|
$pb.css({
|
|
|
|
|
width: progressValue * $pc.width() + 'px'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var updateProgress = function (progressValue) {
|
|
|
|
|
var text = Math.round(progressValue*100) + '%';
|
|
|
|
|
text += progressValue === 1 ? '' : ' (' + Messages.download_step2 + '...)';
|
|
|
|
|
$pv.text(text);
|
|
|
|
|
$pb.css({
|
|
|
|
|
width: progressValue * $pc.width()+'px'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var ctx = {
|
|
|
|
|
get: common.getPad,
|
|
|
|
|
};
|
|
|
|
|
var dl = downloadFunction(ctx, data, function (err, obj) {
|
|
|
|
|
$link.prepend($('<span>', {'class': 'fa fa-external-link'}))
|
|
|
|
|
.attr('href', '#')
|
|
|
|
|
.click(function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
obj.download();
|
|
|
|
|
});
|
|
|
|
|
done();
|
|
|
|
|
if (obj) { obj.download(); }
|
|
|
|
|
cb(err, obj);
|
|
|
|
|
}, {
|
|
|
|
|
progress: updateDLProgress,
|
|
|
|
|
progress2: 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);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
$row.find('.cp-fileupload-table-cancel').html('').append($cancel);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
File.downloadFile = function (fData, cb) {
|
|
|
|
|
var name = fData.filename || fData.title;
|
|
|
|
|
common.getFileSize(fData.channel, function (e, data) {
|
|
|
|
|
queue.push({
|
|
|
|
|
dl: todo,
|
|
|
|
|
dl: function (file) { updateProgressbar(file, fData, MakeBackup.downloadFile, cb); },
|
|
|
|
|
size: data,
|
|
|
|
|
name: name
|
|
|
|
|
});
|
|
|
|
@ -535,70 +520,8 @@ define([
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
File.downloadPad = function (pData, cb) {
|
|
|
|
|
console.error("SFC FILE -DOWNLOAD PAD");
|
|
|
|
|
console.log("pData", pData);
|
|
|
|
|
var todo = function (file) {
|
|
|
|
|
console.warn("TODO");
|
|
|
|
|
console.log("file", file);
|
|
|
|
|
if (queue.inProgress) { return; }
|
|
|
|
|
queue.inProgress = true;
|
|
|
|
|
var id = file.id;
|
|
|
|
|
|
|
|
|
|
var $row = $table.find('tr[id="'+id+'"]');
|
|
|
|
|
var $pv = $row.find('.cp-fileupload-table-progress-value');
|
|
|
|
|
var $pb = $row.find('.cp-fileupload-table-progress-container');
|
|
|
|
|
var $pc = $row.find('.cp-fileupload-table-progress');
|
|
|
|
|
var $link = $row.find('.cp-fileupload-table-link');
|
|
|
|
|
|
|
|
|
|
var done = function () {
|
|
|
|
|
$row.find('.cp-fileupload-table-cancel').text('-');
|
|
|
|
|
queue.inProgress = false;
|
|
|
|
|
queue.next();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var updateDLProgress = function (progressValue) {
|
|
|
|
|
var text = Math.round(progressValue*100) + '%';
|
|
|
|
|
text += ' ('+ Messages.download_step1 +'...)';
|
|
|
|
|
$pv.text(text);
|
|
|
|
|
$pb.css({
|
|
|
|
|
width: progressValue * $pc.width()+'px'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var updateProgress = function (progressValue) {
|
|
|
|
|
var text = Math.round(progressValue*100) + '%';
|
|
|
|
|
text += progressValue === 1 ? '' : ' ('+ Messages.download_step2 +'...)';
|
|
|
|
|
$pv.text(text);
|
|
|
|
|
$pb.css({
|
|
|
|
|
width: progressValue * $pc.width()+'px'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var dl = module.downloadPad(pData, function (err, obj) {
|
|
|
|
|
$link.prepend($('<span>', {'class': 'fa fa-external-link'}))
|
|
|
|
|
.attr('href', '#')
|
|
|
|
|
.click(function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
obj.download();
|
|
|
|
|
});
|
|
|
|
|
done();
|
|
|
|
|
if (obj) { obj.download(); }
|
|
|
|
|
cb(err, obj);
|
|
|
|
|
}, {
|
|
|
|
|
common: common,
|
|
|
|
|
progress: updateDLProgress,
|
|
|
|
|
progress2: 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);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
$row.find('.cp-fileupload-table-cancel').html('').append($cancel);
|
|
|
|
|
};
|
|
|
|
|
queue.push({
|
|
|
|
|
dl: todo,
|
|
|
|
|
dl: function (file) { updateProgressbar(file, pData, MakeBackup.downloadPad, cb); },
|
|
|
|
|
size: 0,
|
|
|
|
|
name: pData.title,
|
|
|
|
|
});
|
|
|
|
@ -607,135 +530,6 @@ define([
|
|
|
|
|
return File;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.downloadFile = function (fData, cb, obj) {
|
|
|
|
|
var cancelled = false;
|
|
|
|
|
var cancel = function () {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
var src, key, name;
|
|
|
|
|
if (obj && obj.src && obj.key && obj.name) {
|
|
|
|
|
src = obj.src;
|
|
|
|
|
key = obj.key;
|
|
|
|
|
name = obj.name;
|
|
|
|
|
} else {
|
|
|
|
|
var parsed = Hash.parsePadUrl(fData.href || fData.roHref);
|
|
|
|
|
var hash = parsed.hash;
|
|
|
|
|
name = fData.filename || fData.title;
|
|
|
|
|
var secret = Hash.getSecrets('file', hash, fData.password);
|
|
|
|
|
src = Hash.getBlobPathFromHex(secret.channel);
|
|
|
|
|
key = secret.keys && secret.keys.cryptKey;
|
|
|
|
|
}
|
|
|
|
|
Util.fetch(src, function (err, u8) {
|
|
|
|
|
if (cancelled) { return; }
|
|
|
|
|
if (err) { return void cb('E404'); }
|
|
|
|
|
FileCrypto.decrypt(u8, key, function (err, res) {
|
|
|
|
|
if (cancelled) { return; }
|
|
|
|
|
if (err) { return void cb(err); }
|
|
|
|
|
if (!res.content) { return void cb('EEMPTY'); }
|
|
|
|
|
var dl = function () {
|
|
|
|
|
saveAs(res.content, name || res.metadata.name);
|
|
|
|
|
};
|
|
|
|
|
cb(null, {
|
|
|
|
|
metadata: res.metadata,
|
|
|
|
|
content: res.content,
|
|
|
|
|
download: dl
|
|
|
|
|
});
|
|
|
|
|
}, obj && obj.progress2);
|
|
|
|
|
}, obj && obj.progress);
|
|
|
|
|
return {
|
|
|
|
|
cancel: cancel
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var getPad = function (common, data, cb) {
|
|
|
|
|
var sframeChan = common.getSframeChannel();
|
|
|
|
|
sframeChan.query("Q_CRYPTGET", data, function (err, obj) {
|
|
|
|
|
if (err) { return void cb(err); }
|
|
|
|
|
if (obj.error) { return void cb(obj.error); }
|
|
|
|
|
cb(null, obj.data);
|
|
|
|
|
}, { timeout: 60000 });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var transform = function (type, sjson, cb) {
|
|
|
|
|
console.error("SFCfile - transform");
|
|
|
|
|
console.log('type', type);
|
|
|
|
|
console.log('sjson', sjson);
|
|
|
|
|
|
|
|
|
|
var result = {
|
|
|
|
|
data: sjson,
|
|
|
|
|
ext: '.json',
|
|
|
|
|
};
|
|
|
|
|
console.log('result', result);
|
|
|
|
|
var json;
|
|
|
|
|
try {
|
|
|
|
|
json = JSON.parse(sjson);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return void cb(result);
|
|
|
|
|
}
|
|
|
|
|
console.log('json', json);
|
|
|
|
|
var path = '/' + type + '/export.js';
|
|
|
|
|
require([path], function (Exporter) {
|
|
|
|
|
Exporter.main(json, function (data) {
|
|
|
|
|
result.ext = Exporter.ext || '';
|
|
|
|
|
result.data = data;
|
|
|
|
|
cb(result);
|
|
|
|
|
});
|
|
|
|
|
}, function () {
|
|
|
|
|
cb(result);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.downloadPad = function (pData, cb, obj) {
|
|
|
|
|
console.error("SFC file - downloadPad");
|
|
|
|
|
console.log(pData, pData);
|
|
|
|
|
var cancelled = false;
|
|
|
|
|
var cancel = function () {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var parsed = Hash.parsePadUrl(pData.href || pData.roHref);
|
|
|
|
|
var hash = parsed.hash;
|
|
|
|
|
var name = pData.filename || pData.title;
|
|
|
|
|
var opts = {
|
|
|
|
|
password: pData.password
|
|
|
|
|
};
|
|
|
|
|
console.log('parsed', parsed);
|
|
|
|
|
console.log('hash', hash);
|
|
|
|
|
console.log('name', name);
|
|
|
|
|
console.log('opts', opts);
|
|
|
|
|
obj.progress(0);
|
|
|
|
|
getPad(obj.common, {
|
|
|
|
|
hash: parsed.hash,
|
|
|
|
|
opts: opts
|
|
|
|
|
}, function (err, val) {
|
|
|
|
|
if (cancelled) { return; }
|
|
|
|
|
if (err) { return; }
|
|
|
|
|
if (!val) { return; }
|
|
|
|
|
console.log('val', val);
|
|
|
|
|
obj.progress(1);
|
|
|
|
|
|
|
|
|
|
transform(parsed.type, val, function (res) {
|
|
|
|
|
console.error("transform callback");
|
|
|
|
|
console.log('res', res);
|
|
|
|
|
if (cancelled) { return; }
|
|
|
|
|
if (!res.data) { return; }
|
|
|
|
|
obj.progress2(1);
|
|
|
|
|
var dl = function () {
|
|
|
|
|
saveAs(res.data, Util.fixFileName(name));
|
|
|
|
|
};
|
|
|
|
|
cb(null, {
|
|
|
|
|
metadata: res.metadata,
|
|
|
|
|
content: res.data,
|
|
|
|
|
download: dl
|
|
|
|
|
});
|
|
|
|
|
console.log('DONE ---- ' + name);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
cancel: cancel
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return module;
|
|
|
|
|
});
|
|
|
|
|