mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-23 17:50:19 +01:00
Switched to promises in file document store
This commit is contained in:
parent
b4f42219e5
commit
a4cf027900
1 changed files with 29 additions and 31 deletions
|
@ -11,51 +11,49 @@ class FileDocumentStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
//save data in a file, key as md5 - since we don't know what we can passed here
|
//save data in a file, key as md5 - since we don't know what we can passed here
|
||||||
set(key, data, callback, skipExpire){
|
async set(key, data, callback, skipExpire){
|
||||||
const _this = this;
|
const _this = this;
|
||||||
const filePath = this.getPath(key);
|
const filePath = this.getPath(key);
|
||||||
|
|
||||||
try {
|
|
||||||
if (!fs.existsSync(this.basePath)){
|
if (!fs.existsSync(this.basePath)){
|
||||||
winston.debug('creating data storage directory', { filename: this.basePath });
|
winston.debug('creating data storage directory', { filename: this.basePath });
|
||||||
fs.mkdirSync(this.basePath, {mode: '700'});
|
await fs.promises.mkdir(this.basePath, {mode: '700'})
|
||||||
|
.catch(err => {
|
||||||
|
winston.error('error while creating dir', { path: _this.basePath, error: err });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
winston.silly('set key', { type: 'file', filename: filePath });
|
||||||
fs.writeFileSync(filePath, data);
|
await fs.promises.writeFile(filePath, data, {mode: '600'})
|
||||||
|
.then(() => {
|
||||||
callback(true);
|
callback(true);
|
||||||
if (_this.expire && !skipExpire){
|
if (_this.expire && !skipExpire){
|
||||||
winston.warn('file store cannot set expirations on keys', { file: filePath });
|
winston.warn('file store cannot set expirations on keys', { file: filePath });
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
catch (err){
|
.catch(err => {
|
||||||
winston.error('error while writing document to file', { file: filePath, error: err });
|
winston.error('error while writing document to file', { file: filePath, error: err });
|
||||||
callback(false);
|
callback(false);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
catch (err){
|
|
||||||
winston.error('error while creating dir', { path: _this.basePath, error: err });
|
|
||||||
return callback(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//get data from a file
|
//get data from a file
|
||||||
get(key, callback, skipExpire){
|
async get(key, callback, skipExpire){
|
||||||
const _this = this;
|
const _this = this;
|
||||||
const filePath = this.getPath(key);
|
const filePath = this.getPath(key);
|
||||||
|
|
||||||
try {
|
|
||||||
winston.silly('get key', { type: 'file', filename: filePath });
|
winston.silly('get key', { type: 'file', filename: filePath });
|
||||||
let data = fs.readFileSync(filePath).toString();
|
await fs.promises.readFile(filePath, {encoding: 'utf8'})
|
||||||
|
.then(data => {
|
||||||
callback(data);
|
callback(data);
|
||||||
if (_this.expire && !skipExpire){
|
if (_this.expire && !skipExpire){
|
||||||
winston.warn('file store cannot set expirations on keys', { file: filePath });
|
winston.warn('file store cannot set expirations on keys', { file: filePath });
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
catch (err){
|
.catch(err => {
|
||||||
winston.debug('error while reading document', { file: filePath, error: err });
|
winston.debug('error while reading document', { file: filePath, error: err });
|
||||||
return callback(false);
|
return callback(false);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//generate a md5 hash of a key
|
//generate a md5 hash of a key
|
||||||
|
|
Loading…
Reference in a new issue