|
|
@ -1,24 +1,51 @@
|
|
|
|
define([
|
|
|
|
define([
|
|
|
|
'/common/cryptget.js',
|
|
|
|
'/common/cryptget.js',
|
|
|
|
'/common/common-hash.js',
|
|
|
|
'/common/common-hash.js',
|
|
|
|
|
|
|
|
'/common/common-util.js',
|
|
|
|
|
|
|
|
'/file/file-crypto.js',
|
|
|
|
'/bower_components/nthen/index.js',
|
|
|
|
'/bower_components/nthen/index.js',
|
|
|
|
'/bower_components/saferphore/index.js',
|
|
|
|
'/bower_components/saferphore/index.js',
|
|
|
|
'/bower_components/jszip/dist/jszip.min.js',
|
|
|
|
'/bower_components/jszip/dist/jszip.min.js',
|
|
|
|
], function (Crypt, Hash, nThen, Saferphore, JsZip) {
|
|
|
|
], function (Crypt, Hash, Util, FileCrypto, nThen, Saferphore, JsZip) {
|
|
|
|
|
|
|
|
|
|
|
|
var sanitize = function (str) {
|
|
|
|
var sanitize = function (str) {
|
|
|
|
return str.replace(/[^a-z0-9]/gi, '_').toLowerCase();
|
|
|
|
return str.replace(/[\\/?%*:|"<>]/gi, '_')/*.toLowerCase()*/;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var getUnique = function (name, ext, existing) {
|
|
|
|
var getUnique = function (name, ext, existing) {
|
|
|
|
var n = name;
|
|
|
|
var n = name + ext;
|
|
|
|
var i = 1;
|
|
|
|
var i = 1;
|
|
|
|
while (existing.indexOf(n) !== -1) {
|
|
|
|
while (existing.indexOf(n.toLowerCase()) !== -1) {
|
|
|
|
n = name + ' ('+ i++ + ')';
|
|
|
|
n = name + ' ('+ i++ + ')' + ext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
return n;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var transform = function (ctx, type, sjson, cb) {
|
|
|
|
|
|
|
|
var result = {
|
|
|
|
|
|
|
|
data: sjson,
|
|
|
|
|
|
|
|
ext: '.json',
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var json;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
json = JSON.parse(sjson);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
return void cb(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var path = '/' + type + '/export.js';
|
|
|
|
|
|
|
|
require([path], function (Exporter) {
|
|
|
|
|
|
|
|
Exporter.main(json, function (data) {
|
|
|
|
|
|
|
|
result.ext = '.' + Exporter.type;
|
|
|
|
|
|
|
|
result.data = data;
|
|
|
|
|
|
|
|
cb(result);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}, function () {
|
|
|
|
|
|
|
|
cb(result);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add a file to the zip. We have to cryptget&transform it if it's a pad
|
|
|
|
|
|
|
|
// or fetch&decrypt it if it's a file.
|
|
|
|
var addFile = function (ctx, zip, fData, existingNames) {
|
|
|
|
var addFile = function (ctx, zip, fData, existingNames) {
|
|
|
|
if (!fData.href && !fData.roHref) {
|
|
|
|
if (!fData.href && !fData.roHref) {
|
|
|
|
return void ctx.errors.push({
|
|
|
|
return void ctx.errors.push({
|
|
|
@ -28,70 +55,121 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var parsed = Hash.parsePadUrl(fData.href || fData.roHref);
|
|
|
|
var parsed = Hash.parsePadUrl(fData.href || fData.roHref);
|
|
|
|
// TODO deal with files here
|
|
|
|
if (['pad', 'file'].indexOf(parsed.hashData.type) === -1) { return; }
|
|
|
|
if (parsed.hashData.type !== 'pad') { return; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// waitFor is used to make sure all the pads and files are process before downloading the zip.
|
|
|
|
var w = ctx.waitFor();
|
|
|
|
var w = ctx.waitFor();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Work with only 10 pad/files at a time
|
|
|
|
ctx.sem.take(function (give) {
|
|
|
|
ctx.sem.take(function (give) {
|
|
|
|
var opts = {
|
|
|
|
var opts = {
|
|
|
|
password: fData.password
|
|
|
|
password: fData.password
|
|
|
|
};
|
|
|
|
};
|
|
|
|
var rawName = fData.fileName || fData.title || 'File';
|
|
|
|
var rawName = fData.filename || fData.title || 'File';
|
|
|
|
console.log(rawName);
|
|
|
|
console.log(rawName);
|
|
|
|
ctx.get({
|
|
|
|
var g = give();
|
|
|
|
hash: parsed.hash,
|
|
|
|
|
|
|
|
opts: opts
|
|
|
|
var done = function () {
|
|
|
|
}, give(function (err, val) {
|
|
|
|
//setTimeout(g, 2000);
|
|
|
|
|
|
|
|
g();
|
|
|
|
w();
|
|
|
|
w();
|
|
|
|
if (err) {
|
|
|
|
};
|
|
|
|
return void ctx.errors.push({
|
|
|
|
var error = function (err) {
|
|
|
|
error: err,
|
|
|
|
done();
|
|
|
|
data: fData
|
|
|
|
return void ctx.errors.push({
|
|
|
|
|
|
|
|
error: err,
|
|
|
|
|
|
|
|
data: fData
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pads (pad,code,slide,kanban,poll,...)
|
|
|
|
|
|
|
|
var todoPad = function () {
|
|
|
|
|
|
|
|
ctx.get({
|
|
|
|
|
|
|
|
hash: parsed.hash,
|
|
|
|
|
|
|
|
opts: opts
|
|
|
|
|
|
|
|
}, function (err, val) {
|
|
|
|
|
|
|
|
if (err) { return void error(err); }
|
|
|
|
|
|
|
|
if (!val) { return void error('EEMPTY'); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var opts = {
|
|
|
|
|
|
|
|
binary: true,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
transform(ctx, parsed.type, val, function (res) {
|
|
|
|
|
|
|
|
if (!res.data) { return void error('EEMPTY'); }
|
|
|
|
|
|
|
|
var fileName = getUnique(sanitize(rawName), res.ext, existingNames);
|
|
|
|
|
|
|
|
existingNames.push(fileName.toLowerCase());
|
|
|
|
|
|
|
|
zip.file(fileName, res.data, opts);
|
|
|
|
|
|
|
|
console.log('DONE ---- ' + fileName);
|
|
|
|
|
|
|
|
setTimeout(done, 1000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// TODO transform file here
|
|
|
|
};
|
|
|
|
// var blob = transform(val, type);
|
|
|
|
|
|
|
|
var opts = {};
|
|
|
|
// Files (mediatags...)
|
|
|
|
var fileName = getUnique(sanitize(rawName), '.txt', existingNames);
|
|
|
|
var todoFile = function () {
|
|
|
|
existingNames.push(fileName);
|
|
|
|
var secret = Hash.getSecrets('file', parsed.hash, fData.password);
|
|
|
|
zip.file(fileName, val, opts);
|
|
|
|
var hexFileName = secret.channel;
|
|
|
|
console.log('DONE ---- ' + rawName);
|
|
|
|
var src = Hash.getBlobPathFromHex(hexFileName);
|
|
|
|
}));
|
|
|
|
var key = secret.keys && secret.keys.cryptKey;
|
|
|
|
|
|
|
|
Util.fetch(src, function (err, u8) {
|
|
|
|
|
|
|
|
if (err) { return void error('E404'); }
|
|
|
|
|
|
|
|
FileCrypto.decrypt(u8, key, function (err, res) {
|
|
|
|
|
|
|
|
if (err) { return void error(err); }
|
|
|
|
|
|
|
|
var opts = {
|
|
|
|
|
|
|
|
binary: true,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var extIdx = rawName.lastIndexOf('.');
|
|
|
|
|
|
|
|
var name = extIdx !== -1 ? rawName.slice(0,extIdx) : rawName;
|
|
|
|
|
|
|
|
var ext = extIdx !== -1 ? rawName.slice(extIdx) : "";
|
|
|
|
|
|
|
|
var fileName = getUnique(sanitize(name), ext, existingNames);
|
|
|
|
|
|
|
|
existingNames.push(fileName.toLowerCase());
|
|
|
|
|
|
|
|
zip.file(fileName, res.content, opts);
|
|
|
|
|
|
|
|
console.log('DONE ---- ' + fileName);
|
|
|
|
|
|
|
|
setTimeout(done, 1000);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if (parsed.hashData.type === 'file') {
|
|
|
|
|
|
|
|
return void todoFile();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
todoPad();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// cb(err, blob);
|
|
|
|
// cb(err, blob);
|
|
|
|
// wiht blob.name not undefined
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var makeFolder = function (ctx, root, zip) {
|
|
|
|
// Add folders and their content recursively in the zip
|
|
|
|
|
|
|
|
var makeFolder = function (ctx, root, zip, fd) {
|
|
|
|
if (typeof (root) !== "object") { return; }
|
|
|
|
if (typeof (root) !== "object") { return; }
|
|
|
|
var existingNames = [];
|
|
|
|
var existingNames = [];
|
|
|
|
Object.keys(root).forEach(function (k) {
|
|
|
|
Object.keys(root).forEach(function (k) {
|
|
|
|
var el = root[k];
|
|
|
|
var el = root[k];
|
|
|
|
if (typeof el === "object") {
|
|
|
|
if (typeof el === "object") {
|
|
|
|
var fName = getUnique(sanitize(k), '', existingNames);
|
|
|
|
var fName = getUnique(sanitize(k), '', existingNames);
|
|
|
|
existingNames.push(fName);
|
|
|
|
existingNames.push(fName.toLowerCase());
|
|
|
|
return void makeFolder(ctx, el, zip.folder(fName));
|
|
|
|
return void makeFolder(ctx, el, zip.folder(fName), fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ctx.data.sharedFolders[el]) {
|
|
|
|
if (ctx.data.sharedFolders[el]) {
|
|
|
|
// TODO later...
|
|
|
|
var sfData = ctx.sf[el].metadata;
|
|
|
|
return;
|
|
|
|
var sfName = getUnique(sanitize(sfData.title || 'Folder'), '', existingNames);
|
|
|
|
|
|
|
|
existingNames.push(sfName.toLowerCase());
|
|
|
|
|
|
|
|
return void makeFolder(ctx, ctx.sf[el].root, zip.folder(sfName), ctx.sf[el].filesData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var fData = ctx.data.filesData[el];
|
|
|
|
var fData = fd[el];
|
|
|
|
if (fData) {
|
|
|
|
if (fData) {
|
|
|
|
addFile(ctx, zip, fData, existingNames);
|
|
|
|
addFile(ctx, zip, fData, existingNames);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// What is this element?
|
|
|
|
|
|
|
|
console.error(el);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Main function. Create the empty zip and fill it starting from drive.root
|
|
|
|
var create = function (data, getPad, cb) {
|
|
|
|
var create = function (data, getPad, cb) {
|
|
|
|
if (!data || !data.drive) { return void cb('EEMPTY'); }
|
|
|
|
if (!data || !data.uo || !data.uo.drive) { return void cb('EEMPTY'); }
|
|
|
|
var sem = Saferphore.create(10);
|
|
|
|
var sem = Saferphore.create(5);
|
|
|
|
var ctx = {
|
|
|
|
var ctx = {
|
|
|
|
get: getPad,
|
|
|
|
get: getPad,
|
|
|
|
data: data.drive,
|
|
|
|
data: data.uo.drive,
|
|
|
|
|
|
|
|
sf: data.sf,
|
|
|
|
zip: new JsZip(),
|
|
|
|
zip: new JsZip(),
|
|
|
|
errors: [],
|
|
|
|
errors: [],
|
|
|
|
sem: sem,
|
|
|
|
sem: sem,
|
|
|
@ -99,9 +177,8 @@ define([
|
|
|
|
nThen(function (waitFor) {
|
|
|
|
nThen(function (waitFor) {
|
|
|
|
ctx.waitFor = waitFor;
|
|
|
|
ctx.waitFor = waitFor;
|
|
|
|
var zipRoot = ctx.zip.folder('Root');
|
|
|
|
var zipRoot = ctx.zip.folder('Root');
|
|
|
|
makeFolder(ctx, data.drive.root, zipRoot);
|
|
|
|
makeFolder(ctx, ctx.data.root, zipRoot, ctx.data.filesData);
|
|
|
|
}).nThen(function () {
|
|
|
|
}).nThen(function () {
|
|
|
|
// TODO call cb with ctx.zip here
|
|
|
|
|
|
|
|
console.log(ctx.zip);
|
|
|
|
console.log(ctx.zip);
|
|
|
|
console.log(ctx.errors);
|
|
|
|
console.log(ctx.errors);
|
|
|
|
ctx.zip.generateAsync({type: 'blob'}).then(function (content) {
|
|
|
|
ctx.zip.generateAsync({type: 'blob'}).then(function (content) {
|
|
|
|