2019-09-15 14:16:32 +02:00
|
|
|
import { Launcher } from 'comp/launcher';
|
|
|
|
import { StorageBase } from 'storage/storage-base';
|
2015-12-12 09:53:50 +01:00
|
|
|
|
2019-09-18 21:26:43 +02:00
|
|
|
class StorageFileCache extends StorageBase {
|
|
|
|
name = 'cache';
|
|
|
|
enabled = !!Launcher;
|
|
|
|
system = true;
|
2015-12-06 21:32:41 +01:00
|
|
|
|
2019-09-18 21:26:43 +02:00
|
|
|
path = null;
|
2015-12-06 21:32:41 +01:00
|
|
|
|
2019-08-18 10:17:09 +02:00
|
|
|
getPath(id) {
|
2017-03-19 00:30:29 +01:00
|
|
|
return Launcher.joinPath(this.path, id);
|
2019-09-18 21:26:43 +02:00
|
|
|
}
|
2015-12-06 21:32:41 +01:00
|
|
|
|
2019-08-18 10:17:09 +02:00
|
|
|
initFs(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
|
|
|
}
|
2017-02-01 02:37:43 +01:00
|
|
|
|
|
|
|
const path = Launcher.getUserDataPath('OfflineFiles');
|
|
|
|
|
2020-06-01 16:53:51 +02:00
|
|
|
const setPath = (err) => {
|
2017-02-11 23:59:26 +01:00
|
|
|
this.path = err ? null : path;
|
2017-02-21 19:55:50 +01:00
|
|
|
if (err) {
|
|
|
|
this.logger.error('Error opening local offline storage', err);
|
|
|
|
}
|
2017-02-11 23:59:26 +01:00
|
|
|
return callback && callback(err);
|
|
|
|
};
|
|
|
|
|
2020-06-01 16:53:51 +02:00
|
|
|
Launcher.fileExists(path, (exists) => {
|
2017-02-11 23:59:26 +01:00
|
|
|
if (exists) {
|
|
|
|
setPath();
|
2017-02-05 11:49:22 +01:00
|
|
|
} else {
|
2017-02-21 19:55:50 +01:00
|
|
|
Launcher.mkdir(path, setPath);
|
2015-12-06 21:32:41 +01:00
|
|
|
}
|
2017-02-01 02:37:43 +01:00
|
|
|
});
|
2019-09-18 21:26:43 +02:00
|
|
|
}
|
2015-12-06 21:32:41 +01:00
|
|
|
|
2019-08-18 10:17:09 +02:00
|
|
|
save(id, opts, data, callback) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Save', id);
|
2020-06-01 16:53:51 +02:00
|
|
|
this.initFs((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
|
|
|
}
|
2017-01-31 07:50:28 +01:00
|
|
|
const ts = this.logger.ts();
|
2020-06-01 16:53:51 +02:00
|
|
|
Launcher.writeFile(this.getPath(id), data, (err) => {
|
2017-02-05 11:08:26 +01:00
|
|
|
if (err) {
|
|
|
|
this.logger.error('Error saving to cache', id, err);
|
2017-02-05 11:49:22 +01:00
|
|
|
return callback && callback(err);
|
2017-02-05 11:08:26 +01:00
|
|
|
}
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Saved', id, this.logger.ts(ts));
|
2019-08-16 23:05:39 +02:00
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2017-02-01 02:37:43 +01:00
|
|
|
});
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2019-09-18 21:26:43 +02:00
|
|
|
}
|
2015-12-06 21:32:41 +01:00
|
|
|
|
2019-08-18 10:17:09 +02:00
|
|
|
load(id, opts, callback) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Load', id);
|
2020-06-01 16:53:51 +02:00
|
|
|
this.initFs((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
|
|
|
}
|
2017-02-01 02:37:43 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const ts = this.logger.ts();
|
2017-02-01 02:37:43 +01:00
|
|
|
|
2017-02-05 11:08:26 +01:00
|
|
|
Launcher.readFile(this.getPath(id), undefined, (data, err) => {
|
|
|
|
if (err) {
|
2017-02-05 11:49:22 +01:00
|
|
|
this.logger.error('Error loading from cache', id, err);
|
2017-02-05 11:08:26 +01:00
|
|
|
return callback && callback(err, null);
|
|
|
|
}
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Loaded', id, this.logger.ts(ts));
|
2017-02-05 11:49:22 +01:00
|
|
|
return callback && callback(null, data.buffer);
|
2017-02-01 02:37:43 +01:00
|
|
|
});
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2019-09-18 21:26:43 +02:00
|
|
|
}
|
2015-12-06 21:32:41 +01:00
|
|
|
|
2019-08-18 10:17:09 +02:00
|
|
|
remove(id, opts, callback) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Remove', id);
|
2020-06-01 16:53:51 +02:00
|
|
|
this.initFs((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
|
|
|
}
|
2017-02-01 02:37:43 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const ts = this.logger.ts();
|
2017-02-01 02:37:43 +01:00
|
|
|
const path = this.getPath(id);
|
|
|
|
|
2020-06-01 16:53:51 +02:00
|
|
|
Launcher.fileExists(path, (exists) => {
|
2017-02-01 02:37:43 +01:00
|
|
|
if (exists) {
|
2020-06-01 16:53:51 +02:00
|
|
|
Launcher.deleteFile(path, (err) => {
|
2017-02-05 11:49:22 +01:00
|
|
|
if (err) {
|
|
|
|
this.logger.error('Error removing from cache', id, err);
|
|
|
|
} else {
|
|
|
|
this.logger.debug('Removed', id, this.logger.ts(ts));
|
2017-02-01 02:37:43 +01:00
|
|
|
}
|
2017-02-05 11:49:22 +01:00
|
|
|
return callback && callback(err);
|
2017-02-01 02:37:43 +01:00
|
|
|
});
|
2017-02-05 11:49:22 +01:00
|
|
|
} else if (callback) {
|
|
|
|
callback();
|
2015-12-07 22:20:18 +01:00
|
|
|
}
|
2017-02-01 02:37:43 +01:00
|
|
|
});
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2015-12-06 21:32:41 +01:00
|
|
|
}
|
2019-09-18 21:26:43 +02:00
|
|
|
}
|
2015-12-06 21:32:41 +01:00
|
|
|
|
2019-09-15 14:16:32 +02:00
|
|
|
export { StorageFileCache };
|