diff --git a/www/common/common-util.js b/www/common/common-util.js index 435a72280..603e38a30 100644 --- a/www/common/common-util.js +++ b/www/common/common-util.js @@ -274,32 +274,71 @@ // given a path, asynchronously return an arraybuffer - Util.fetch = function (src, cb, progress) { - var CB = Util.once(cb); + var getCacheKey = function (src) { + 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(); - xhr.open("GET", src, true); - if (progress) { - xhr.addEventListener("progress", function (evt) { - if (evt.lengthComputable) { - var percentComplete = evt.loaded / evt.total; - progress(percentComplete); - } - }, false); - } - xhr.responseType = "arraybuffer"; - xhr.onerror = function (err) { CB(err); }; - xhr.onload = function () { - if (/^4/.test(''+this.status)) { - return CB('XHR_ERROR'); + var xhr; + + var fetch = function () { + xhr = new XMLHttpRequest(); + xhr.open("GET", src, true); + if (progress) { + xhr.addEventListener("progress", function (evt) { + if (evt.lengthComputable) { + var percentComplete = evt.loaded / evt.total; + progress(percentComplete); + } + }, false); } - 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 { cancel: function () { - if (xhr.abort) { xhr.abort(); } + if (xhr && xhr.abort) { xhr.abort(); } } }; }; diff --git a/www/common/cryptget.js b/www/common/cryptget.js index e394788d7..1cbd5056e 100644 --- a/www/common/cryptget.js +++ b/www/common/cryptget.js @@ -6,10 +6,11 @@ define([ '/common/common-hash.js', '/common/common-realtime.js', '/common/outer/network-config.js', + '/common/outer/cache-store.js', '/common/pinpad.js', '/bower_components/nthen/index.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) { if (S.done) { return; } S.cb(err, doc); @@ -92,7 +93,8 @@ define([ validateKey: secret.keys.validateKey || undefined, crypto: Crypto.createEncryptor(secret.keys), logLevel: 0, - initialState: opt.initialState + initialState: opt.initialState, + Cache: Cache }; return config; }; @@ -132,9 +134,11 @@ define([ }; config.onError = function (info) { + console.warn(info); finish(Session, info.error); }; config.onChannelError = function (info) { + console.error(info); finish(Session, info.error); }; diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 5dd029def..46f569a3e 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -3,6 +3,7 @@ define([ '/customize/messages.js', '/common/common-util.js', '/common/common-hash.js', + '/common/outer/cache-store.js', '/common/common-messaging.js', '/common/common-constants.js', '/common/common-feedback.js', @@ -14,7 +15,7 @@ define([ '/customize/application_config.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, AppConfig, Nthen) { @@ -760,7 +761,7 @@ define([ u8 = _u8; }), function (progress) { onProgress(progress * 50); - }); + }, Cache); }).nThen(function (waitFor) { require(["/file/file-crypto.js"], waitFor(function (FileCrypto) { FileCrypto.decrypt(u8, key, waitFor(function (err, _res) { diff --git a/www/common/make-backup.js b/www/common/make-backup.js index 88602c8bb..9ba925fd7 100644 --- a/www/common/make-backup.js +++ b/www/common/make-backup.js @@ -1,17 +1,18 @@ define([ 'jquery', - '/common/cryptget.js', '/file/file-crypto.js', '/common/common-hash.js', '/common/common-util.js', '/common/common-interface.js', '/common/hyperscript.js', '/common/common-feedback.js', + '/common/inner/cache.js', '/customize/messages.js', '/bower_components/nthen/index.js', '/bower_components/saferphore/index.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 sanitize = function (str) { @@ -89,7 +90,7 @@ define([ if (updateProgress && updateProgress.progress) { updateProgress.progress(data); } - }); + }, ctx.cache); var cancel = function () { cancelled = true; @@ -291,7 +292,7 @@ define([ }; // 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'); } var sem = Saferphore.create(5); var ctx = { @@ -305,7 +306,8 @@ define([ sem: sem, updateProgress: progress, max: 0, - done: 0 + done: 0, + cache: cache }; var filesData = data.sharedFolderId && ctx.sf[data.sharedFolderId] ? ctx.sf[data.sharedFolderId].filesData : ctx.data.filesData; progress('reading', -1); @@ -356,7 +358,7 @@ define([ else if (state === "done") { updateProgress.folderProgress(3); } - }); + }, ctx.cache); }; var createExportUI = function (origin) { diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index c9efb1eda..976daa887 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -1419,7 +1419,7 @@ define([ console.error(e); callback(""); } - }); + }, void 0, common.getCache()); }; APP.docEditor = new window.DocsAPI.DocEditor("cp-app-oo-placeholder-a", APP.ooconfig); diff --git a/www/common/sframe-common-file.js b/www/common/sframe-common-file.js index 8a5be3764..13fea31b4 100644 --- a/www/common/sframe-common-file.js +++ b/www/common/sframe-common-file.js @@ -654,6 +654,7 @@ define([ fileHost: privateData.fileHost, get: common.getPad, sframeChan: sframeChan, + cache: common.getCache() }; var dl = downloadFunction(ctx, data, function (err, obj) { diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index 67ede0d64..9329b467a 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -607,6 +607,10 @@ define([ }); }; + funcs.getCache = function () { + return ctx.cache; + }; + /* funcs.storeLinkToClipboard = function (readOnly, cb) { ctx.sframeChan.query('Q_STORE_LINK_TO_CLIPBOARD', readOnly, function (err) { if (cb) { cb(err); } @@ -805,6 +809,8 @@ define([ modules[type].onEvent(obj.data); }); + ctx.cache = Cache.create(ctx.sframeChan); + ctx.metadataMgr.onReady(waitFor()); }).nThen(function () { @@ -817,9 +823,8 @@ define([ MT.MediaTag.setDefaultConfig('maxDownloadSize', maxMtSize); } - if (MT.MediaTag && Cache) { - var cache = Cache.create(ctx.sframeChan); - MT.MediaTag.setDefaultConfig('Cache', cache); + if (MT.MediaTag && ctx.cache) { + MT.MediaTag.setDefaultConfig('Cache', ctx.cache); } try { diff --git a/www/settings/inner.js b/www/settings/inner.js index 9ff57aae7..9051d3c34 100644 --- a/www/settings/inner.js +++ b/www/settings/inner.js @@ -831,7 +831,7 @@ define([ Feedback.send('FULL_DRIVE_EXPORT_COMPLETE'); saveAs(blob, filename); }, errors); - }, ui.update); + }, ui.update, common.getCache()); ui.onCancel(function() { ui.close(); bu.stop(); diff --git a/www/teams/inner.js b/www/teams/inner.js index 070d11825..7af3d7afc 100644 --- a/www/teams/inner.js +++ b/www/teams/inner.js @@ -1071,7 +1071,7 @@ define([ Feedback.send('FULL_TEAMDRIVE_EXPORT_COMPLETE'); saveAs(blob, filename); }, errors); - }, ui.update); + }, ui.update, common.getCache); ui.onCancel(function() { ui.close(); bu.stop();