48 lines
1.0 KiB
JavaScript
48 lines
1.0 KiB
JavaScript
var fs = require('fs');
|
|
var crypto = require('crypto');
|
|
|
|
var FileDocumentStore = function() {
|
|
this.basePath = '/data';
|
|
this.expire = null;
|
|
};
|
|
|
|
FileDocumentStore.md5 = function(str) {
|
|
var md5sum = crypto.createHash('md5');
|
|
md5sum.update(str);
|
|
return md5sum.digest('hex');
|
|
};
|
|
|
|
FileDocumentStore.prototype.set = function(key, data, callback) {
|
|
try {
|
|
var _this = this;
|
|
fs.mkdir(this.basePath, '700', function() {
|
|
var fn = _this.basePath + '/' + FileDocumentStore.md5(key);
|
|
fs.writeFile(fn, data, 'utf8', function(err) {
|
|
if (err) {
|
|
callback(false);
|
|
}
|
|
else {
|
|
callback(true);
|
|
}
|
|
});
|
|
});
|
|
} catch(err) {
|
|
callback(false);
|
|
}
|
|
};
|
|
|
|
FileDocumentStore.prototype.get = function(key, callback) {
|
|
var _this = this;
|
|
var fn = this.basePath + '/' + FileDocumentStore.md5(key);
|
|
fs.readFile(fn, 'utf8', function(err, data) {
|
|
if (err) {
|
|
callback(false);
|
|
}
|
|
else {
|
|
callback(data);
|
|
}
|
|
});
|
|
};
|
|
|
|
module.exports = FileDocumentStore;
|