Move storage mechanism to new object
This commit is contained in:
parent
b173cdd144
commit
fec02cfead
3 changed files with 49 additions and 31 deletions
|
@ -1,42 +1,17 @@
|
|||
var fs = require('fs'); // TODO won't be needed
|
||||
|
||||
var winston = require('winston');
|
||||
var hashlib = require('hashlib');
|
||||
|
||||
// For handling serving stored documents
|
||||
|
||||
var DocumentHandler = function(options) {
|
||||
if (options) {
|
||||
this.keyLength = options.keyLength || 20;
|
||||
this.keyLength = options.keyLength || 10;
|
||||
this.store = options.store;
|
||||
}
|
||||
};
|
||||
|
||||
// Save a document
|
||||
// TODO make data path configurable
|
||||
// TODO move to a separate object
|
||||
DocumentHandler.save = function(key, data, callback) {
|
||||
fs.mkdir('data', '700', function() {
|
||||
fs.writeFile('data/' + hashlib.md5(key), data, 'utf8', function() {
|
||||
callback(true); // TODO handle errors
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Retrieve a document by key
|
||||
DocumentHandler.get = function(key, callback) {
|
||||
fs.readFile('data/' + hashlib.md5(key), 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
}
|
||||
else {
|
||||
callback(data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Handle retrieving a document
|
||||
DocumentHandler.prototype.handleGet = function(key, response) {
|
||||
DocumentHandler.get(key, function(ret) {
|
||||
this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
winston.verbose('retrieved document', { key: key });
|
||||
response.writeHead(200, { 'content-type': 'application/json' });
|
||||
|
@ -52,6 +27,7 @@ DocumentHandler.prototype.handleGet = function(key, response) {
|
|||
|
||||
// Handle adding a new Document
|
||||
DocumentHandler.prototype.handlePost = function(request, response) {
|
||||
var _this = this;
|
||||
var key = this.randomKey();
|
||||
var buffer = '';
|
||||
request.on('data', function(data) {
|
||||
|
@ -61,7 +37,7 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
|||
buffer += data.toString();
|
||||
});
|
||||
request.on('end', function(end) {
|
||||
DocumentHandler.save(key, buffer, function(res) {
|
||||
_this.store.set(key, buffer, function(res) {
|
||||
if (res) {
|
||||
winston.verbose('added document', { key: key });
|
||||
response.end(JSON.stringify({ key: key }));
|
||||
|
|
36
lib/file_document_store.js
Normal file
36
lib/file_document_store.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
var fs = require('fs');
|
||||
|
||||
var winston = require('winston');
|
||||
var hashlib = require('hashlib');
|
||||
|
||||
// For storing in files
|
||||
// TODO make data path configurable
|
||||
// TODO make store type configurable
|
||||
|
||||
var FileDocumentStore = function(path) {
|
||||
this.basePath = path;
|
||||
};
|
||||
|
||||
// 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) {
|
||||
var _this = this;
|
||||
fs.mkdir(this.basePath, '700', function() {
|
||||
fs.writeFile(_this.basePath + '/' + hashlib.md5(key), data, 'utf8', function() {
|
||||
callback(true); // TODO handle errors
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Get data from a file from key
|
||||
FileDocumentStore.prototype.get = function(key, callback) {
|
||||
fs.readFile(this.basePath + '/' + hashlib.md5(key), 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
}
|
||||
else {
|
||||
callback(data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = FileDocumentStore;
|
Loading…
Add table
Add a link
Reference in a new issue