mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-23 17:50:19 +01:00
rewrote file document storage
This commit is contained in:
parent
d303416dba
commit
15b8899629
1 changed files with 55 additions and 47 deletions
|
@ -7,57 +7,65 @@ const winston = require('winston');
|
||||||
// options[type] = file
|
// options[type] = file
|
||||||
// options[path] - Where to store
|
// options[path] - Where to store
|
||||||
|
|
||||||
const FileDocumentStore = function(options){
|
class FileDocumentStore {
|
||||||
this.basePath = options.path || './data';
|
|
||||||
this.expire = options.expire;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generate md5 of a string
|
constructor(options){
|
||||||
FileDocumentStore.md5 = function(str){
|
this.basePath = options.path || './data';
|
||||||
let md5sum = crypto.createHash('md5');
|
this.expire = options.expire;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// Get data from a file from key
|
//save data in a file, key as md5 - since we don't know what we can passed here
|
||||||
FileDocumentStore.prototype.get = function(key, callback, skipExpire){
|
set(key, data, callback, skipExpire){
|
||||||
const _this = this;
|
const _this = this;
|
||||||
const fn = require('path').join(this.basePath, FileDocumentStore.md5(key));
|
const filePath = this.getPath(key);
|
||||||
fs.readFile(fn, 'utf8', function(err, data){
|
|
||||||
if (err){
|
try {
|
||||||
callback(false);
|
if (!fs.existsSync(this.basePath)){
|
||||||
}
|
winston.debug('creating data storage directory', { filename: this.basePath });
|
||||||
else {
|
fs.mkdirSync(this.basePath, {mode: '700'});
|
||||||
callback(data);
|
}
|
||||||
if (_this.expire && !skipExpire){
|
|
||||||
winston.warn('file store cannot set expirations on keys');
|
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;
|
module.exports = FileDocumentStore;
|
||||||
|
|
Loading…
Reference in a new issue