1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-01 01:30:21 +01:00
haste-server/lib/document_handler.js

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2011-11-18 23:12:28 +01:00
var fs = require('fs'); // TODO won't be needed
2011-11-18 21:51:38 +01:00
var winston = require('winston');
2011-11-18 23:12:28 +01:00
var hashlib = require('hashlib');
2011-11-18 21:51:38 +01:00
// For handling serving stored documents
var DocumentHandler = function(options) {
2011-11-18 22:57:23 +01:00
if (options) {
this.keyLength = options.keyLength || 20;
}
2011-11-18 21:51:38 +01:00
};
2011-11-18 23:12:28 +01:00
// 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);
}
});
};
2011-11-18 21:51:38 +01:00
2011-11-18 22:45:48 +01:00
// Handle retrieving a document
2011-11-18 21:51:38 +01:00
DocumentHandler.prototype.handleGet = function(key, response) {
2011-11-18 23:12:28 +01:00
DocumentHandler.get(key, function(ret) {
if (ret) {
winston.verbose('retrieved document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.end(JSON.stringify({ data: ret, key: key }));
}
else {
winston.warn('document not found', { key: key });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'document not found' }));
}
});
2011-11-18 21:51:38 +01:00
};
2011-11-18 22:45:48 +01:00
// Handle adding a new Document
2011-11-18 21:51:38 +01:00
DocumentHandler.prototype.handlePost = function(request, response) {
2011-11-18 22:45:48 +01:00
var key = this.randomKey();
2011-11-18 23:12:28 +01:00
var buffer = '';
2011-11-18 21:51:38 +01:00
request.on('data', function(data) {
2011-11-18 23:12:28 +01:00
if (!buffer) {
2011-11-18 22:28:09 +01:00
response.writeHead(200, { 'content-type': 'application/json' });
2011-11-18 21:51:38 +01:00
}
2011-11-18 23:12:28 +01:00
buffer += data.toString();
2011-11-18 21:51:38 +01:00
});
request.on('end', function(end) {
2011-11-18 23:12:28 +01:00
DocumentHandler.save(key, buffer, function(res) {
if (res) {
winston.verbose('added document', { key: key });
response.end(JSON.stringify({ key: key }));
}
else {
winston.verbose('error adding document');
response.end(JSON.stringify({ message: 'error adding document' }));
}
});
2011-11-18 21:51:38 +01:00
});
request.on('error', function(error) {
2011-11-18 22:28:09 +01:00
winston.error('connection error: ' + error.message);
response.writeHead(500, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'connection error' }));
2011-11-18 21:51:38 +01:00
});
};
2011-11-18 22:45:48 +01:00
// Generate a random key
DocumentHandler.prototype.randomKey = function() {
var text = '';
var keyspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < this.keyLength; i++) {
2011-11-18 22:45:48 +01:00
text += keyspace.charAt(Math.floor(Math.random() * keyspace.length));
}
return text;
};
2011-11-18 22:22:00 +01:00
2011-11-18 21:51:38 +01:00
module.exports = DocumentHandler;