2011-11-18 22:54:57 +00:00
|
|
|
var fs = require('fs');
|
2012-01-13 16:16:42 +00:00
|
|
|
var crypto = require('crypto');
|
2011-11-18 22:54:57 +00:00
|
|
|
|
|
|
|
var winston = require('winston');
|
|
|
|
|
|
|
|
// For storing in files
|
2011-11-22 03:03:50 +00:00
|
|
|
// options[type] = file
|
|
|
|
// options[path] - Where to store
|
2011-11-18 22:54:57 +00:00
|
|
|
|
2011-11-18 23:04:24 +00:00
|
|
|
var FileDocumentStore = function(options) {
|
|
|
|
this.basePath = options.path || './data';
|
2011-11-28 06:15:52 +00:00
|
|
|
this.expire = options.expire;
|
2011-11-18 22:54:57 +00:00
|
|
|
};
|
|
|
|
|
2016-07-21 20:03:33 +00:00
|
|
|
// Generate secret (10 chars)
|
|
|
|
FileDocumentStore.secret = function() {
|
|
|
|
return Math.random().toString(24).slice(-10);
|
|
|
|
};
|
|
|
|
|
2012-01-13 16:16:42 +00:00
|
|
|
// Generate md5 of a string
|
|
|
|
FileDocumentStore.md5 = function(str) {
|
|
|
|
var md5sum = crypto.createHash('md5');
|
|
|
|
md5sum.update(str);
|
|
|
|
return md5sum.digest('hex');
|
|
|
|
};
|
|
|
|
|
2012-01-21 20:19:55 +00:00
|
|
|
// Save data in a file, key as md5 - since we don't know what we could
|
|
|
|
// be passed here
|
2011-11-28 06:15:52 +00:00
|
|
|
FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
2011-11-18 23:08:04 +00:00
|
|
|
try {
|
|
|
|
var _this = this;
|
|
|
|
fs.mkdir(this.basePath, '700', function() {
|
2016-07-21 20:03:33 +00:00
|
|
|
var sc = FileDocumentStore.secret();
|
2012-01-24 05:01:38 +00:00
|
|
|
var fn = _this.basePath + '/' + FileDocumentStore.md5(key);
|
2016-07-21 20:03:33 +00:00
|
|
|
var sfn = fn + "-" + sc;
|
2012-01-21 20:19:55 +00:00
|
|
|
fs.writeFile(fn, data, 'utf8', function(err) {
|
2011-11-18 23:08:04 +00:00
|
|
|
if (err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
else {
|
2016-07-21 20:03:33 +00:00
|
|
|
fs.writeFile(sfn, "A validation file for " + fn, 'utf8', function (err) {
|
|
|
|
if (!err) callback(true, sc);
|
|
|
|
});
|
2011-11-28 06:15:52 +00:00
|
|
|
if (_this.expire && !skipExpire) {
|
2012-01-21 20:19:55 +00:00
|
|
|
winston.warn('file store cannot set expirations on keys');
|
2011-11-28 06:15:52 +00:00
|
|
|
}
|
2011-11-18 23:08:04 +00:00
|
|
|
}
|
|
|
|
});
|
2011-11-18 22:54:57 +00:00
|
|
|
});
|
2011-11-18 23:08:04 +00:00
|
|
|
} catch(err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
2011-11-18 22:54:57 +00:00
|
|
|
};
|
|
|
|
|
2016-07-21 20:03:33 +00:00
|
|
|
FileDocumentStore.prototype.remove = function(str, key, callback) {
|
|
|
|
var file = FileDocumentStore.md5(str);
|
|
|
|
var kfn = key;
|
|
|
|
fs.exists(this.basePath + "/" + file + "-" + kfn, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
try {
|
|
|
|
fs.unlink(this.basePath + "/" + file, function() {
|
|
|
|
fs.unlink(this.basePath + "/" + file + "-" + kfn, function() {
|
|
|
|
callback(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch(err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-11-18 22:54:57 +00:00
|
|
|
// Get data from a file from key
|
2011-11-28 06:15:52 +00:00
|
|
|
FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
|
|
|
var _this = this;
|
2012-01-24 05:01:38 +00:00
|
|
|
var fn = this.basePath + '/' + FileDocumentStore.md5(key);
|
2012-01-21 20:19:55 +00:00
|
|
|
fs.readFile(fn, 'utf8', function(err, data) {
|
2011-11-18 22:54:57 +00:00
|
|
|
if (err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(data);
|
2011-11-28 06:15:52 +00:00
|
|
|
if (_this.expire && !skipExpire) {
|
2012-01-21 20:19:55 +00:00
|
|
|
winston.warn('file store cannot set expirations on keys');
|
2011-11-28 06:15:52 +00:00
|
|
|
}
|
2011-11-18 22:54:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = FileDocumentStore;
|