rewrote file document storage

This commit is contained in:
zneix 2020-08-29 00:06:49 +02:00
parent d303416dba
commit 15b8899629
1 changed files with 55 additions and 47 deletions

View File

@ -7,57 +7,65 @@ const winston = require('winston');
// options[type] = file
// options[path] - Where to store
const FileDocumentStore = function(options){
this.basePath = options.path || './data';
this.expire = options.expire;
};
class FileDocumentStore {
// Generate md5 of a string
FileDocumentStore.md5 = function(str){
let md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
};
// Save data in a file, key as md5 - since we don't know what we could
// be passed here
FileDocumentStore.prototype.set = function(key, data, callback, skipExpire){
try {
const _this = this;
fs.mkdir(this.basePath, '700', function(){
const fn = _this.basePath + '/' + FileDocumentStore.md5(key);
fs.writeFile(fn, data, 'utf8', function(err){
if (err){
callback(false);
}
else {
callback(true);
if (_this.expire && !skipExpire){
winston.warn('file store cannot set expirations on keys');
}
}
});
});
} catch(err){
callback(false);
constructor(options){
this.basePath = options.path || './data';
this.expire = options.expire;
}
};
// Get data from a file from key
FileDocumentStore.prototype.get = function(key, callback, skipExpire){
const _this = this;
const fn = require('path').join(this.basePath, FileDocumentStore.md5(key));
fs.readFile(fn, 'utf8', function(err, data){
if (err){
callback(false);
}
else {
callback(data);
if (_this.expire && !skipExpire){
winston.warn('file store cannot set expirations on keys');
//save data in a file, key as md5 - since we don't know what we can passed here
set(key, data, callback, skipExpire){
const _this = this;
const filePath = this.getPath(key);
try {
if (!fs.existsSync(this.basePath)){
winston.debug('creating data storage directory', { filename: this.basePath });
fs.mkdirSync(this.basePath, {mode: '700'});
}
try {
fs.writeFileSync(filePath, data);
callback(true);
if (_this.expire && !skipExpire){
winston.warn('file store cannot set expirations on keys', { file: filePath });
}
}
catch (err){
winston.error('error while writing document to file', { file: filePath, error: err });
callback(false);
}
}
});
};
catch (err){
winston.error('error while creating dir', { path: _this.basePath, error: err });
return callback(false);
}
}
//get data from a file
get(key, callback, skipExpire){
const _this = this;
const filePath = this.getPath(key);
try {
winston.silly('get key', { type: 'file', filename: filePath });
let data = fs.readFileSync(filePath).toString();
callback(data);
if (_this.expire && !skipExpire){
winston.warn('file store cannot set expirations on keys', { file: filePath });
}
}
catch (err){
winston.debug('error while reading document', { file: filePath, error: err });
return callback(false);
}
}
//generate a md5 hash of a key
getPath(str){
return require('path').join(this.basePath, crypto.createHash('md5').update(str).digest('hex'));
}
}
module.exports = FileDocumentStore;