2011-11-18 22:54:57 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var winston = require('winston');
|
|
|
|
var hashlib = require('hashlib');
|
|
|
|
|
|
|
|
// 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-18 22:54:57 +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:13:14 +00:00
|
|
|
FileDocumentStore.prototype.set = function(key, data, callback, setExpire) {
|
2011-11-18 23:08:04 +00:00
|
|
|
try {
|
|
|
|
var _this = this;
|
|
|
|
fs.mkdir(this.basePath, '700', function() {
|
|
|
|
fs.writeFile(_this.basePath + '/' + hashlib.md5(key), data, 'utf8', function(err) {
|
|
|
|
if (err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
};
|
|
|
|
|
|
|
|
// Get data from a file from key
|
2011-11-28 06:13:14 +00:00
|
|
|
FileDocumentStore.prototype.get = function(key, callback, setExpire) {
|
2011-11-18 22:54:57 +00:00
|
|
|
fs.readFile(this.basePath + '/' + hashlib.md5(key), 'utf8', function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = FileDocumentStore;
|