keeweb/app/scripts/storage/storage-file-cache.js

97 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-12-06 21:32:41 +01:00
'use strict';
2016-03-27 09:06:23 +02:00
var StorageBase = require('./storage-base'),
Launcher = require('../comp/launcher');
2015-12-12 09:53:50 +01:00
2016-03-27 09:06:23 +02:00
var StorageFileCache = StorageBase.extend({
2015-12-06 21:32:41 +01:00
name: 'cache',
enabled: !!Launcher,
2016-03-13 17:08:25 +01:00
system: true,
2015-12-06 21:32:41 +01:00
path: null,
getPath: function(id) {
return Launcher.req('path').join(this.path, id);
},
2016-03-27 09:06:23 +02:00
initFs: function(callback) {
2015-12-06 21:32:41 +01:00
if (this.path) {
2015-12-07 22:00:44 +01:00
return callback && callback();
2015-12-06 21:32:41 +01:00
}
2015-12-07 22:00:44 +01:00
try {
var path = Launcher.getUserDataPath('OfflineFiles');
var fs = Launcher.req('fs');
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
2015-12-06 21:32:41 +01:00
}
2015-12-07 22:00:44 +01:00
this.path = path;
2015-12-13 15:59:41 +01:00
callback();
2015-12-07 22:00:44 +01:00
} catch (e) {
2016-03-27 09:06:23 +02:00
this.logger.error('Error opening local offline storage', e);
2015-12-07 22:00:44 +01:00
if (callback) { callback(e); }
2015-12-06 21:32:41 +01:00
}
},
2016-03-12 17:49:52 +01:00
save: function(id, opts, data, callback) {
2016-03-27 09:06:23 +02:00
var that = this;
that.logger.debug('Save', id);
that.initFs(function(err) {
2015-12-06 21:32:41 +01:00
if (err) {
2015-12-07 22:00:44 +01:00
return callback && callback(err);
2015-12-06 21:32:41 +01:00
}
2016-03-27 09:46:43 +02:00
var ts = that.logger.ts();
2015-12-06 21:32:41 +01:00
try {
2016-03-27 09:06:23 +02:00
Launcher.writeFile(that.getPath(id), data);
2016-03-27 09:46:43 +02:00
that.logger.debug('Saved', id, that.logger.ts(ts));
2015-12-07 22:00:44 +01:00
if (callback) { callback(); }
2015-12-06 21:32:41 +01:00
} catch (e) {
2016-03-27 09:46:43 +02:00
that.logger.error('Error saving to cache', id, e);
2015-12-07 22:00:44 +01:00
if (callback) { callback(e); }
2015-12-06 21:32:41 +01:00
}
2016-03-27 09:06:23 +02:00
});
2015-12-06 21:32:41 +01:00
},
2016-03-12 17:49:52 +01:00
load: function(id, opts, callback) {
2016-03-27 09:06:23 +02:00
var that = this;
that.logger.debug('Load', id);
that.initFs(function(err) {
2015-12-06 21:32:41 +01:00
if (err) {
2015-12-07 22:00:44 +01:00
return callback && callback(null, err);
2015-12-06 21:32:41 +01:00
}
2016-03-27 09:06:23 +02:00
var ts = that.logger.ts();
2015-12-06 21:32:41 +01:00
try {
2016-03-27 09:06:23 +02:00
var data = Launcher.readFile(that.getPath(id));
that.logger.debug('Loaded', id, that.logger.ts(ts));
2015-12-07 22:00:44 +01:00
if (callback) { callback(null, data.buffer); }
2015-12-06 21:32:41 +01:00
} catch (e) {
2016-03-27 09:06:23 +02:00
that.logger.error('Error loading from cache', id, e);
2015-12-07 22:00:44 +01:00
if (callback) { callback(e, null); }
2015-12-06 21:32:41 +01:00
}
2016-03-27 09:06:23 +02:00
});
2015-12-06 21:32:41 +01:00
},
2016-03-12 17:49:52 +01:00
remove: function(id, opts, callback) {
2016-03-27 09:06:23 +02:00
var that = this;
that.logger.debug('Remove', id);
that.initFs(function(err) {
2015-12-06 21:32:41 +01:00
if (err) {
2015-12-07 22:00:44 +01:00
return callback && callback(err);
2015-12-06 21:32:41 +01:00
}
2016-03-27 09:06:23 +02:00
var ts = that.logger.ts();
2015-12-06 21:32:41 +01:00
try {
2016-03-27 09:06:23 +02:00
var path = that.getPath(id);
2015-12-07 22:20:18 +01:00
if (Launcher.fileExists(path)) {
Launcher.deleteFile(path);
}
2016-03-27 09:06:23 +02:00
that.logger.debug('Removed', id, that.logger.ts(ts));
2015-12-07 22:00:44 +01:00
if (callback) { callback(); }
2015-12-06 21:32:41 +01:00
} catch(e) {
2016-03-27 09:06:23 +02:00
that.logger.error('Error removing from cache', id, e);
2015-12-07 22:00:44 +01:00
if (callback) { callback(e); }
2015-12-06 21:32:41 +01:00
}
2016-03-27 09:06:23 +02:00
});
2015-12-06 21:32:41 +01:00
}
2016-03-27 09:06:23 +02:00
});
2015-12-06 21:32:41 +01:00
2016-03-27 09:46:43 +02:00
module.exports = new StorageFileCache();