From 58cdf21defb492b28cf90ddf7c2cdf90ff74d46b Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 27 Nov 2020 17:11:12 +0100 Subject: [PATCH] Store the blob cache in the outer domain --- www/common/inner/cache.js | 35 +++++++++++++++++++++++++++++++ www/common/media-tag.js | 30 +++++++++++++++++--------- www/common/sframe-common-outer.js | 16 ++++++++++++++ www/common/sframe-common.js | 7 +++++++ 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 www/common/inner/cache.js diff --git a/www/common/inner/cache.js b/www/common/inner/cache.js new file mode 100644 index 000000000..f14890797 --- /dev/null +++ b/www/common/inner/cache.js @@ -0,0 +1,35 @@ +define([ +], function () { + var S = {}; + + S.create = function (sframeChan) { + var getBlobCache = function (id, cb) { + sframeChan.query('Q_GET_BLOB_CACHE', {id:id}, function (err, data) { + var e = err || (data && data.error); + if (e) { return void cb(e); } + if (!data || typeof(data) !== "object") { return void cb('EINVAL'); } + var arr = Object.keys(data).map(function (i) { return data[i]; }); + var u8 = Uint8Array.from(arr); + cb(null, u8); + }); + }; + var setBlobCache = function (id, u8, cb) { + sframeChan.query('Q_SET_BLOB_CACHE', { + id: id, + u8: u8 + }, function (err, data) { + var e = err || (data && data.error) || undefined; + cb(e); + }); + }; + + + return { + getBlobCache: getBlobCache, + setBlobCache: setBlobCache + }; + }; + + return S; +}); + diff --git a/www/common/media-tag.js b/www/common/media-tag.js index e4ec9ce2a..b58375302 100644 --- a/www/common/media-tag.js +++ b/www/common/media-tag.js @@ -1,5 +1,5 @@ (function (window) { -var factory = function (Cache) { +var factory = function () { var Promise = window.Promise; var cache; var cypherChunkLength = 131088; @@ -199,6 +199,19 @@ var factory = function (Cache) { return cacheKey; }; + var getBlobCache = function (id, cb) { + if (!config.Cache || typeof(config.Cache.getBlobCache) !== "function") { + return void cb('EINVAL'); + } + config.Cache.getBlobCache(id, cb); + }; + var setBlobCache = function (id, u8, cb) { + if (!config.Cache || typeof(config.Cache.setBlobCache) !== "function") { + return void cb('EINVAL'); + } + config.Cache.setBlobCache(id, u8, cb); + }; + var getFileSize = function (src, _cb) { var cb = function (e, res) { _cb(e, res); @@ -224,7 +237,7 @@ var factory = function (Cache) { if (!cacheKey) { return void check(); } - Cache.getBlobCache(cacheKey, function (err, u8) { + getBlobCache(cacheKey, function (err, u8) { if (err || !u8) { return void check(); } cb(null, 0); }); @@ -263,7 +276,7 @@ var factory = function (Cache) { if (arrayBuffer) { var u8 = new Uint8Array(arrayBuffer); if (cacheKey) { - return void Cache.setBlobCache(cacheKey, u8, function () { + return void setBlobCache(cacheKey, u8, function () { cb(null, u8); }); } @@ -276,7 +289,7 @@ var factory = function (Cache) { if (!cacheKey) { return void fetch(); } - Cache.getBlobCache(cacheKey, function (err, u8) { + getBlobCache(cacheKey, function (err, u8) { if (err || !u8) { return void fetch(); } cb(null, u8); }); @@ -628,15 +641,12 @@ var factory = function (Cache) { }; if (typeof(module) !== 'undefined' && module.exports) { - module.exports = factory( - require("./outer/cache-store.js") - ); + module.exports = factory(); } else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) { define([ - '/common/outer/cache-store.js', '/bower_components/es6-promise/es6-promise.min.js' - ], function (Cache) { - return factory(Cache); + ], function () { + return factory(); }); } else { // unsupported initialization diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index a46a94837..12327c720 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -680,6 +680,22 @@ define([ }); }); + sframeChan.on('Q_GET_BLOB_CACHE', function (data, cb) { + Utils.Cache.getBlobCache(data.id, function (err, obj) { + if (err) { return void cb({error: err}); } + cb(obj); + }); + }); + sframeChan.on('Q_SET_BLOB_CACHE', function (data, cb) { + if (!data || !data.u8 || typeof(data.u8) !== "object") { return void cb({error: 'EINVAL'}); } + var arr = Object.keys(data.u8).map(function (i) { return data.u8[i]; }); + var u8 = Uint8Array.from(arr); + Utils.Cache.setBlobCache(data.id, u8, function (err) { + if (err) { return void cb({error: err}); } + cb(); + }); + }); + sframeChan.on('Q_GET_ATTRIBUTE', function (data, cb) { Cryptpad.getAttribute(data.key, function (e, data) { cb({ diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index 328bd70c8..67ede0d64 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -11,6 +11,7 @@ define([ '/common/sframe-common-codemirror.js', '/common/sframe-common-cursor.js', '/common/sframe-common-mailbox.js', + '/common/inner/cache.js', '/common/inner/common-mediatag.js', '/common/metadata-manager.js', @@ -36,6 +37,7 @@ define([ CodeMirror, Cursor, Mailbox, + Cache, MT, MetadataMgr, AppConfig, @@ -815,6 +817,11 @@ define([ MT.MediaTag.setDefaultConfig('maxDownloadSize', maxMtSize); } + if (MT.MediaTag && Cache) { + var cache = Cache.create(ctx.sframeChan); + MT.MediaTag.setDefaultConfig('Cache', cache); + } + try { var feedback = privateData.feedbackAllowed; Feedback.init(feedback);