2011-11-19 04:51:38 +08:00
|
|
|
var winston = require('winston');
|
2014-04-22 02:16:23 +08:00
|
|
|
var Busboy = require('busboy');
|
2011-11-19 04:51:38 +08:00
|
|
|
|
|
|
|
// For handling serving stored documents
|
|
|
|
|
2011-11-19 05:54:16 +08:00
|
|
|
var DocumentHandler = function(options) {
|
2011-11-29 05:46:59 +08:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
2011-11-19 05:57:23 +08:00
|
|
|
}
|
2011-11-29 05:46:59 +08:00
|
|
|
this.keyLength = options.keyLength || DocumentHandler.defaultKeyLength;
|
|
|
|
this.maxLength = options.maxLength; // none by default
|
|
|
|
this.store = options.store;
|
2012-01-08 00:35:11 +08:00
|
|
|
this.keyGenerator = options.keyGenerator;
|
2011-11-19 04:51:38 +08:00
|
|
|
};
|
|
|
|
|
2011-11-29 05:46:59 +08:00
|
|
|
DocumentHandler.defaultKeyLength = 10;
|
|
|
|
|
2011-11-19 05:45:48 +08:00
|
|
|
// Handle retrieving a document
|
2011-11-28 14:13:14 +08:00
|
|
|
DocumentHandler.prototype.handleGet = function(key, response, skipExpire) {
|
2011-11-19 06:54:57 +08:00
|
|
|
this.store.get(key, function(ret) {
|
2011-11-19 06:12:28 +08:00
|
|
|
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' });
|
2011-12-20 01:44:12 +08:00
|
|
|
response.end(JSON.stringify({ message: 'Document not found.' }));
|
2011-11-19 06:12:28 +08:00
|
|
|
}
|
2011-11-28 14:13:14 +08:00
|
|
|
}, skipExpire);
|
2011-11-19 04:51:38 +08:00
|
|
|
};
|
|
|
|
|
2011-12-16 20:57:09 +08:00
|
|
|
// Handle retrieving the raw version of a document
|
|
|
|
DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) {
|
|
|
|
this.store.get(key, function(ret) {
|
|
|
|
if (ret) {
|
|
|
|
winston.verbose('retrieved raw document', { key: key });
|
2018-07-13 02:25:27 +08:00
|
|
|
response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
|
2011-12-16 20:57:09 +08:00
|
|
|
response.end(ret);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
winston.warn('raw document not found', { key: key });
|
|
|
|
response.writeHead(404, { 'content-type': 'application/json' });
|
2011-12-20 01:44:12 +08:00
|
|
|
response.end(JSON.stringify({ message: 'Document not found.' }));
|
2011-12-16 20:57:09 +08:00
|
|
|
}
|
|
|
|
}, skipExpire);
|
|
|
|
};
|
|
|
|
|
2011-11-19 05:45:48 +08:00
|
|
|
// Handle adding a new Document
|
2014-04-22 02:16:23 +08:00
|
|
|
DocumentHandler.prototype.handlePost = function (request, response) {
|
2011-11-19 06:54:57 +08:00
|
|
|
var _this = this;
|
2011-11-19 06:12:28 +08:00
|
|
|
var buffer = '';
|
2011-12-20 04:20:22 +08:00
|
|
|
var cancelled = false;
|
2014-04-22 02:16:23 +08:00
|
|
|
|
|
|
|
// What to do when done
|
|
|
|
var onSuccess = function () {
|
|
|
|
// Check length
|
2011-11-21 23:17:23 +08:00
|
|
|
if (_this.maxLength && buffer.length > _this.maxLength) {
|
2011-12-20 04:20:22 +08:00
|
|
|
cancelled = true;
|
2011-11-22 10:48:09 +08:00
|
|
|
winston.warn('document >maxLength', { maxLength: _this.maxLength });
|
2011-11-21 23:17:23 +08:00
|
|
|
response.writeHead(400, { 'content-type': 'application/json' });
|
2012-01-22 04:19:55 +08:00
|
|
|
response.end(
|
|
|
|
JSON.stringify({ message: 'Document exceeds maximum length.' })
|
|
|
|
);
|
2014-04-22 02:16:23 +08:00
|
|
|
return;
|
2011-11-21 23:17:23 +08:00
|
|
|
}
|
2014-04-22 02:16:23 +08:00
|
|
|
// And then save if we should
|
|
|
|
_this.chooseKey(function (key) {
|
|
|
|
_this.store.set(key, buffer, function (res) {
|
2011-11-22 10:48:09 +08:00
|
|
|
if (res) {
|
|
|
|
winston.verbose('added document', { key: key });
|
2014-04-22 02:16:23 +08:00
|
|
|
response.writeHead(200, { 'content-type': 'application/json' });
|
2011-11-22 10:48:09 +08:00
|
|
|
response.end(JSON.stringify({ key: key }));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
winston.verbose('error adding document');
|
|
|
|
response.writeHead(500, { 'content-type': 'application/json' });
|
2011-12-20 01:44:12 +08:00
|
|
|
response.end(JSON.stringify({ message: 'Error adding document.' }));
|
2011-11-22 10:48:09 +08:00
|
|
|
}
|
|
|
|
});
|
2011-11-19 06:12:28 +08:00
|
|
|
});
|
2014-04-22 02:16:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// If we should, parse a form to grab the data
|
|
|
|
var ct = request.headers['content-type'];
|
|
|
|
if (ct && ct.split(';')[0] === 'multipart/form-data') {
|
|
|
|
var busboy = new Busboy({ headers: request.headers });
|
|
|
|
busboy.on('field', function (fieldname, val) {
|
|
|
|
if (fieldname === 'data') {
|
|
|
|
buffer = val;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
busboy.on('finish', function () {
|
|
|
|
onSuccess();
|
|
|
|
});
|
|
|
|
request.pipe(busboy);
|
|
|
|
// Otherwise, use our own and just grab flat data from POST body
|
|
|
|
} else {
|
|
|
|
request.on('data', function (data) {
|
|
|
|
buffer += data.toString();
|
|
|
|
});
|
|
|
|
request.on('end', function () {
|
|
|
|
if (cancelled) { return; }
|
|
|
|
onSuccess();
|
|
|
|
});
|
|
|
|
request.on('error', function (error) {
|
|
|
|
winston.error('connection error: ' + error.message);
|
|
|
|
response.writeHead(500, { 'content-type': 'application/json' });
|
|
|
|
response.end(JSON.stringify({ message: 'Connection error.' }));
|
|
|
|
cancelled = true;
|
|
|
|
});
|
|
|
|
}
|
2011-11-19 04:51:38 +08:00
|
|
|
};
|
|
|
|
|
2012-01-14 00:16:42 +08:00
|
|
|
// Keep choosing keys until one isn't taken
|
2011-11-22 10:48:09 +08:00
|
|
|
DocumentHandler.prototype.chooseKey = function(callback) {
|
2012-01-14 00:16:42 +08:00
|
|
|
var key = this.acceptableKey();
|
2011-11-22 10:48:09 +08:00
|
|
|
var _this = this;
|
|
|
|
this.store.get(key, function(ret) {
|
|
|
|
if (ret) {
|
|
|
|
_this.chooseKey(callback);
|
|
|
|
} else {
|
|
|
|
callback(key);
|
|
|
|
}
|
2018-02-16 22:52:44 +08:00
|
|
|
}, true); // Don't bump expirations when key searching
|
2011-11-22 10:48:09 +08:00
|
|
|
};
|
|
|
|
|
2012-01-14 00:16:42 +08:00
|
|
|
DocumentHandler.prototype.acceptableKey = function() {
|
|
|
|
return this.keyGenerator.createKey(this.keyLength);
|
|
|
|
};
|
|
|
|
|
2011-11-19 04:51:38 +08:00
|
|
|
module.exports = DocumentHandler;
|