Add cryptget and blob cache

pull/1/head
yflory 4 years ago
parent 7756306966
commit 82c869f4cd

@ -274,32 +274,71 @@
// given a path, asynchronously return an arraybuffer // given a path, asynchronously return an arraybuffer
Util.fetch = function (src, cb, progress) { var getCacheKey = function (src) {
var CB = Util.once(cb); var _src = src.replace(/(\/)*$/, ''); // Remove trailing slashes
var idx = _src.lastIndexOf('/');
var cacheKey = _src.slice(idx+1);
if (!/^[a-f0-9]{48}$/.test(cacheKey)) { cacheKey = undefined; }
return cacheKey;
};
Util.fetch = function (src, cb, progress, cache) {
var CB = Util.once(Util.mkAsync(cb));
var cacheKey = getCacheKey(src);
var getBlobCache = function (id, cb) {
if (!cache || typeof(cache.getBlobCache) !== "function") { return void cb('EINVAL'); }
cache.getBlobCache(id, cb);
};
var setBlobCache = function (id, u8, cb) {
if (!cache || typeof(cache.setBlobCache) !== "function") { return void cb('EINVAL'); }
cache.setBlobCache(id, u8, cb);
};
var xhr = new XMLHttpRequest(); var xhr;
xhr.open("GET", src, true);
if (progress) { var fetch = function () {
xhr.addEventListener("progress", function (evt) { xhr = new XMLHttpRequest();
if (evt.lengthComputable) { xhr.open("GET", src, true);
var percentComplete = evt.loaded / evt.total; if (progress) {
progress(percentComplete); xhr.addEventListener("progress", function (evt) {
} if (evt.lengthComputable) {
}, false); var percentComplete = evt.loaded / evt.total;
} progress(percentComplete);
xhr.responseType = "arraybuffer"; }
xhr.onerror = function (err) { CB(err); }; }, false);
xhr.onload = function () {
if (/^4/.test(''+this.status)) {
return CB('XHR_ERROR');
} }
return void CB(void 0, new Uint8Array(xhr.response)); xhr.responseType = "arraybuffer";
xhr.onerror = function (err) { CB(err); };
xhr.onload = function () {
if (/^4/.test(''+this.status)) {
return CB('XHR_ERROR');
}
var arrayBuffer = xhr.response;
if (arrayBuffer) {
var u8 = new Uint8Array(arrayBuffer);
if (cacheKey) {
return void setBlobCache(cacheKey, u8, function () {
CB(null, u8);
});
}
return void CB(void 0, u8);
}
CB('ENOENT');
};
xhr.send(null);
}; };
xhr.send(null);
if (!cacheKey) { return void fetch(); }
getBlobCache(cacheKey, function (err, u8) {
if (err || !u8) { return void fetch(); }
CB(void 0, u8);
});
return { return {
cancel: function () { cancel: function () {
if (xhr.abort) { xhr.abort(); } if (xhr && xhr.abort) { xhr.abort(); }
} }
}; };
}; };

@ -6,10 +6,11 @@ define([
'/common/common-hash.js', '/common/common-hash.js',
'/common/common-realtime.js', '/common/common-realtime.js',
'/common/outer/network-config.js', '/common/outer/network-config.js',
'/common/outer/cache-store.js',
'/common/pinpad.js', '/common/pinpad.js',
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
'/bower_components/chainpad/chainpad.dist.js', '/bower_components/chainpad/chainpad.dist.js',
], function (Crypto, CPNetflux, Netflux, Util, Hash, Realtime, NetConfig, Pinpad, nThen) { ], function (Crypto, CPNetflux, Netflux, Util, Hash, Realtime, NetConfig, Cache, Pinpad, nThen) {
var finish = function (S, err, doc) { var finish = function (S, err, doc) {
if (S.done) { return; } if (S.done) { return; }
S.cb(err, doc); S.cb(err, doc);
@ -92,7 +93,8 @@ define([
validateKey: secret.keys.validateKey || undefined, validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys), crypto: Crypto.createEncryptor(secret.keys),
logLevel: 0, logLevel: 0,
initialState: opt.initialState initialState: opt.initialState,
Cache: Cache
}; };
return config; return config;
}; };
@ -132,9 +134,11 @@ define([
}; };
config.onError = function (info) { config.onError = function (info) {
console.warn(info);
finish(Session, info.error); finish(Session, info.error);
}; };
config.onChannelError = function (info) { config.onChannelError = function (info) {
console.error(info);
finish(Session, info.error); finish(Session, info.error);
}; };

@ -3,6 +3,7 @@ define([
'/customize/messages.js', '/customize/messages.js',
'/common/common-util.js', '/common/common-util.js',
'/common/common-hash.js', '/common/common-hash.js',
'/common/outer/cache-store.js',
'/common/common-messaging.js', '/common/common-messaging.js',
'/common/common-constants.js', '/common/common-constants.js',
'/common/common-feedback.js', '/common/common-feedback.js',
@ -14,7 +15,7 @@ define([
'/customize/application_config.js', '/customize/application_config.js',
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
], function (Config, Messages, Util, Hash, ], function (Config, Messages, Util, Hash, Cache,
Messaging, Constants, Feedback, Visible, UserObject, LocalStore, Channel, Block, Messaging, Constants, Feedback, Visible, UserObject, LocalStore, Channel, Block,
AppConfig, Nthen) { AppConfig, Nthen) {
@ -760,7 +761,7 @@ define([
u8 = _u8; u8 = _u8;
}), function (progress) { }), function (progress) {
onProgress(progress * 50); onProgress(progress * 50);
}); }, Cache);
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
require(["/file/file-crypto.js"], waitFor(function (FileCrypto) { require(["/file/file-crypto.js"], waitFor(function (FileCrypto) {
FileCrypto.decrypt(u8, key, waitFor(function (err, _res) { FileCrypto.decrypt(u8, key, waitFor(function (err, _res) {

@ -1,17 +1,18 @@
define([ define([
'jquery', 'jquery',
'/common/cryptget.js',
'/file/file-crypto.js', '/file/file-crypto.js',
'/common/common-hash.js', '/common/common-hash.js',
'/common/common-util.js', '/common/common-util.js',
'/common/common-interface.js', '/common/common-interface.js',
'/common/hyperscript.js', '/common/hyperscript.js',
'/common/common-feedback.js', '/common/common-feedback.js',
'/common/inner/cache.js',
'/customize/messages.js', '/customize/messages.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, FileCrypto, Hash, Util, UI, h, Feedback, Messages, nThen, Saferphore, JsZip) { ], function ($, FileCrypto, Hash, Util, UI, h, Feedback,
Cache, Messages, nThen, Saferphore, JsZip) {
var saveAs = window.saveAs; var saveAs = window.saveAs;
var sanitize = function (str) { var sanitize = function (str) {
@ -89,7 +90,7 @@ define([
if (updateProgress && updateProgress.progress) { if (updateProgress && updateProgress.progress) {
updateProgress.progress(data); updateProgress.progress(data);
} }
}); }, ctx.cache);
var cancel = function () { var cancel = function () {
cancelled = true; cancelled = true;
@ -291,7 +292,7 @@ define([
}; };
// Main function. Create the empty zip and fill it starting from drive.root // Main function. Create the empty zip and fill it starting from drive.root
var create = function (data, getPad, fileHost, cb, progress) { var create = function (data, getPad, fileHost, cb, progress, cache) {
if (!data || !data.uo || !data.uo.drive) { return void cb('EEMPTY'); } if (!data || !data.uo || !data.uo.drive) { return void cb('EEMPTY'); }
var sem = Saferphore.create(5); var sem = Saferphore.create(5);
var ctx = { var ctx = {
@ -305,7 +306,8 @@ define([
sem: sem, sem: sem,
updateProgress: progress, updateProgress: progress,
max: 0, max: 0,
done: 0 done: 0,
cache: cache
}; };
var filesData = data.sharedFolderId && ctx.sf[data.sharedFolderId] ? ctx.sf[data.sharedFolderId].filesData : ctx.data.filesData; var filesData = data.sharedFolderId && ctx.sf[data.sharedFolderId] ? ctx.sf[data.sharedFolderId].filesData : ctx.data.filesData;
progress('reading', -1); progress('reading', -1);
@ -356,7 +358,7 @@ define([
else if (state === "done") { else if (state === "done") {
updateProgress.folderProgress(3); updateProgress.folderProgress(3);
} }
}); }, ctx.cache);
}; };
var createExportUI = function (origin) { var createExportUI = function (origin) {

@ -1419,7 +1419,7 @@ define([
console.error(e); console.error(e);
callback(""); callback("");
} }
}); }, void 0, common.getCache());
}; };
APP.docEditor = new window.DocsAPI.DocEditor("cp-app-oo-placeholder-a", APP.ooconfig); APP.docEditor = new window.DocsAPI.DocEditor("cp-app-oo-placeholder-a", APP.ooconfig);

@ -654,6 +654,7 @@ define([
fileHost: privateData.fileHost, fileHost: privateData.fileHost,
get: common.getPad, get: common.getPad,
sframeChan: sframeChan, sframeChan: sframeChan,
cache: common.getCache()
}; };
var dl = downloadFunction(ctx, data, function (err, obj) { var dl = downloadFunction(ctx, data, function (err, obj) {

@ -607,6 +607,10 @@ define([
}); });
}; };
funcs.getCache = function () {
return ctx.cache;
};
/* funcs.storeLinkToClipboard = function (readOnly, cb) { /* funcs.storeLinkToClipboard = function (readOnly, cb) {
ctx.sframeChan.query('Q_STORE_LINK_TO_CLIPBOARD', readOnly, function (err) { ctx.sframeChan.query('Q_STORE_LINK_TO_CLIPBOARD', readOnly, function (err) {
if (cb) { cb(err); } if (cb) { cb(err); }
@ -805,6 +809,8 @@ define([
modules[type].onEvent(obj.data); modules[type].onEvent(obj.data);
}); });
ctx.cache = Cache.create(ctx.sframeChan);
ctx.metadataMgr.onReady(waitFor()); ctx.metadataMgr.onReady(waitFor());
}).nThen(function () { }).nThen(function () {
@ -817,9 +823,8 @@ define([
MT.MediaTag.setDefaultConfig('maxDownloadSize', maxMtSize); MT.MediaTag.setDefaultConfig('maxDownloadSize', maxMtSize);
} }
if (MT.MediaTag && Cache) { if (MT.MediaTag && ctx.cache) {
var cache = Cache.create(ctx.sframeChan); MT.MediaTag.setDefaultConfig('Cache', ctx.cache);
MT.MediaTag.setDefaultConfig('Cache', cache);
} }
try { try {

@ -831,7 +831,7 @@ define([
Feedback.send('FULL_DRIVE_EXPORT_COMPLETE'); Feedback.send('FULL_DRIVE_EXPORT_COMPLETE');
saveAs(blob, filename); saveAs(blob, filename);
}, errors); }, errors);
}, ui.update); }, ui.update, common.getCache());
ui.onCancel(function() { ui.onCancel(function() {
ui.close(); ui.close();
bu.stop(); bu.stop();

@ -1071,7 +1071,7 @@ define([
Feedback.send('FULL_TEAMDRIVE_EXPORT_COMPLETE'); Feedback.send('FULL_TEAMDRIVE_EXPORT_COMPLETE');
saveAs(blob, filename); saveAs(blob, filename);
}, errors); }, errors);
}, ui.update); }, ui.update, common.getCache);
ui.onCancel(function() { ui.onCancel(function() {
ui.close(); ui.close();
bu.stop(); bu.stop();

Loading…
Cancel
Save