Added support for haste maximum size
This commit is contained in:
@@ -5,6 +5,7 @@ var winston = require('winston');
|
||||
var DocumentHandler = function(options) {
|
||||
if (options) {
|
||||
this.keyLength = options.keyLength || 10;
|
||||
this.maxLength = options.maxLength; // none by default
|
||||
this.store = options.store;
|
||||
}
|
||||
};
|
||||
@@ -35,8 +36,15 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
||||
response.writeHead(200, { 'content-type': 'application/json' });
|
||||
}
|
||||
buffer += data.toString();
|
||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||
_this.cancelled = true;
|
||||
winston.warn('attempted to upload a document >maxLength', { maxLength: _this.maxLength });
|
||||
response.writeHead(400, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ message: 'document exceeds maximum length' }));
|
||||
}
|
||||
});
|
||||
request.on('end', function(end) {
|
||||
if (_this.cancelled) return;
|
||||
_this.store.set(key, buffer, function(res) {
|
||||
if (res) {
|
||||
winston.verbose('added document', { key: key });
|
||||
@@ -44,6 +52,7 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
||||
}
|
||||
else {
|
||||
winston.verbose('error adding document');
|
||||
response.writeHead(500, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ message: 'error adding document' }));
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user