2015-12-06 21:32:41 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-12 09:53:50 +01:00
|
|
|
var Launcher = require('../comp/launcher'),
|
|
|
|
Logger = require('../util/logger');
|
|
|
|
|
|
|
|
var logger = new Logger('storage-file-cache');
|
2015-12-06 21:32:41 +01:00
|
|
|
|
|
|
|
var StorageFileCache = {
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function(callback) {
|
|
|
|
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) {
|
2015-12-12 09:53:50 +01:00
|
|
|
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) {
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.debug('Save', id);
|
2015-12-06 21:32:41 +01:00
|
|
|
this.init((function(err) {
|
|
|
|
if (err) {
|
2015-12-07 22:00:44 +01:00
|
|
|
return callback && callback(err);
|
2015-12-06 21:32:41 +01:00
|
|
|
}
|
2015-12-12 09:53:50 +01:00
|
|
|
var ts = logger.ts();
|
2015-12-06 21:32:41 +01:00
|
|
|
try {
|
2015-12-07 22:00:44 +01:00
|
|
|
Launcher.writeFile(this.getPath(id), data);
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.debug('Saved', id, logger.ts(ts));
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(); }
|
2015-12-06 21:32:41 +01:00
|
|
|
} catch (e) {
|
2015-12-12 09:53:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
load: function(id, opts, callback) {
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.debug('Load', id);
|
2015-12-06 21:32:41 +01:00
|
|
|
this.init((function(err) {
|
|
|
|
if (err) {
|
2015-12-07 22:00:44 +01:00
|
|
|
return callback && callback(null, err);
|
2015-12-06 21:32:41 +01:00
|
|
|
}
|
2015-12-12 09:53:50 +01:00
|
|
|
var ts = logger.ts();
|
2015-12-06 21:32:41 +01:00
|
|
|
try {
|
2015-12-07 22:00:44 +01:00
|
|
|
var data = Launcher.readFile(this.getPath(id));
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.debug('Loaded', id, 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) {
|
2015-12-12 09:53:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
remove: function(id, opts, callback) {
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.debug('Remove', id);
|
2015-12-06 21:32:41 +01:00
|
|
|
this.init((function(err) {
|
|
|
|
if (err) {
|
2015-12-07 22:00:44 +01:00
|
|
|
return callback && callback(err);
|
2015-12-06 21:32:41 +01:00
|
|
|
}
|
2015-12-12 09:53:50 +01:00
|
|
|
var ts = logger.ts();
|
2015-12-06 21:32:41 +01:00
|
|
|
try {
|
2015-12-07 22:20:18 +01:00
|
|
|
var path = this.getPath(id);
|
|
|
|
if (Launcher.fileExists(path)) {
|
|
|
|
Launcher.deleteFile(path);
|
|
|
|
}
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.debug('Removed', id, logger.ts(ts));
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(); }
|
2015-12-06 21:32:41 +01:00
|
|
|
} catch(e) {
|
2015-12-12 09:53:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}).bind(this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = StorageFileCache;
|