mirror of
https://github.com/MrUnknownDE/unknownbin.git
synced 2026-04-21 23:23:44 +02:00
first push
This commit is contained in:
38
lib/file_storage.js
Normal file
38
lib/file_storage.js
Normal file
@@ -0,0 +1,38 @@
|
||||
var fs = require('fs');
|
||||
var crypto = require('crypto');
|
||||
|
||||
var logger = require('winston');
|
||||
|
||||
// handles saving and retrieving all documents
|
||||
var FileDocumentStore = function(options) {
|
||||
this.basePath = options.path || './data';
|
||||
logger.info('Path to data: ' + this.basePath);
|
||||
};
|
||||
|
||||
// saves a new file to the filesystem
|
||||
FileDocumentStore.prototype.set = function(key, data, callback) {
|
||||
var _this = this;
|
||||
fs.mkdir(this.basePath, '700', function(err) {
|
||||
fs.writeFile(_this.basePath + '/' + key, data, 'utf8', function(err) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
} else {
|
||||
callback(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// gets an exisiting file from the filesystem
|
||||
FileDocumentStore.prototype.get = function(key, callback) {
|
||||
var _this = this;
|
||||
fs.readFile(this.basePath + '/' + key, 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
} else {
|
||||
callback(data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = FileDocumentStore;
|
||||
Reference in New Issue
Block a user