mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-22 13:13:28 -07:00
Update document_handler.js
This commit is contained in:
parent
18445795b4
commit
87b83bb438
1 changed files with 140 additions and 130 deletions
|
@ -1,9 +1,5 @@
|
||||||
var winston = require('winston');
|
|
||||||
var Busboy = require('busboy');
|
|
||||||
|
|
||||||
// For handling serving stored documents
|
// For handling serving stored documents
|
||||||
|
const winston = require('winston'), Busboy = require('busboy'), DocumentHandler = function (options) {
|
||||||
var DocumentHandler = function(options) {
|
|
||||||
if (!options) {
|
if (!options) {
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
@ -13,6 +9,7 @@ var DocumentHandler = function(options) {
|
||||||
this.keyGenerator = options.keyGenerator;
|
this.keyGenerator = options.keyGenerator;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
DocumentHandler.defaultKeyLength = 10;
|
DocumentHandler.defaultKeyLength = 10;
|
||||||
|
|
||||||
// Handle retrieving a document
|
// Handle retrieving a document
|
||||||
|
@ -29,8 +26,7 @@ DocumentHandler.prototype.handleGet = function(request, response, config) {
|
||||||
} else {
|
} else {
|
||||||
response.end(JSON.stringify({data: ret, key: key}));
|
response.end(JSON.stringify({data: ret, key: key}));
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
winston.warn('document not found', {key: key});
|
winston.warn('document not found', {key: key});
|
||||||
response.writeHead(404, {'content-type': 'application/json'});
|
response.writeHead(404, {'content-type': 'application/json'});
|
||||||
if (request.method === 'HEAD') {
|
if (request.method === 'HEAD') {
|
||||||
|
@ -56,8 +52,7 @@ DocumentHandler.prototype.handleRawGet = function(request, response, config) {
|
||||||
} else {
|
} else {
|
||||||
response.end(ret);
|
response.end(ret);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
winston.warn('raw document not found', {key: key});
|
winston.warn('raw document not found', {key: key});
|
||||||
response.writeHead(404, {'content-type': 'application/json'});
|
response.writeHead(404, {'content-type': 'application/json'});
|
||||||
if (request.method === 'HEAD') {
|
if (request.method === 'HEAD') {
|
||||||
|
@ -71,12 +66,8 @@ DocumentHandler.prototype.handleRawGet = function(request, response, config) {
|
||||||
|
|
||||||
// Handle adding a new Document
|
// Handle adding a new Document
|
||||||
DocumentHandler.prototype.handlePost = function (request, response) {
|
DocumentHandler.prototype.handlePost = function (request, response) {
|
||||||
var _this = this;
|
|
||||||
var buffer = '';
|
|
||||||
var cancelled = false;
|
|
||||||
|
|
||||||
// What to do when done
|
// What to do when done
|
||||||
var onSuccess = function () {
|
let _this = this, buffer = '', cancelled = false, onSuccess = function () {
|
||||||
// Check length
|
// Check length
|
||||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
|
@ -94,8 +85,7 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
||||||
winston.verbose('added document', {key: key});
|
winston.verbose('added document', {key: key});
|
||||||
response.writeHead(200, {'content-type': 'application/json'});
|
response.writeHead(200, {'content-type': 'application/json'});
|
||||||
response.end(JSON.stringify({key: key}));
|
response.end(JSON.stringify({key: key}));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
winston.verbose('error adding document');
|
winston.verbose('error adding document');
|
||||||
response.writeHead(500, {'content-type': 'application/json'});
|
response.writeHead(500, {'content-type': 'application/json'});
|
||||||
response.end(JSON.stringify({message: 'Error adding document.'}));
|
response.end(JSON.stringify({message: 'Error adding document.'}));
|
||||||
|
@ -105,8 +95,9 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// If we should, parse a form to grab the data
|
// If we should, parse a form to grab the data
|
||||||
var ct = request.headers['content-type'];
|
const ct = request.headers['content-type'];
|
||||||
if (ct && ct.split(';')[0] === 'multipart/form-data') {
|
if (ct) {
|
||||||
|
if (ct.split(';')[0] === 'multipart/form-data') {
|
||||||
var busboy = new Busboy({headers: request.headers});
|
var busboy = new Busboy({headers: request.headers});
|
||||||
busboy.on('field', function (fieldname, val) {
|
busboy.on('field', function (fieldname, val) {
|
||||||
if (fieldname === 'data') {
|
if (fieldname === 'data') {
|
||||||
|
@ -123,7 +114,26 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
||||||
buffer += data.toString();
|
buffer += data.toString();
|
||||||
});
|
});
|
||||||
request.on('end', function () {
|
request.on('end', function () {
|
||||||
if (cancelled) { return; }
|
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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
request.on('data', function (data) {
|
||||||
|
buffer += data.toString();
|
||||||
|
});
|
||||||
|
request.on('end', function () {
|
||||||
|
if (cancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
onSuccess();
|
onSuccess();
|
||||||
});
|
});
|
||||||
request.on('error', function (error) {
|
request.on('error', function (error) {
|
||||||
|
@ -137,8 +147,8 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
||||||
|
|
||||||
// Keep choosing keys until one isn't taken
|
// Keep choosing keys until one isn't taken
|
||||||
DocumentHandler.prototype.chooseKey = function (callback) {
|
DocumentHandler.prototype.chooseKey = function (callback) {
|
||||||
var key = this.acceptableKey();
|
const key = this.acceptableKey();
|
||||||
var _this = this;
|
const _this = this;
|
||||||
this.store.get(key, function (ret) {
|
this.store.get(key, function (ret) {
|
||||||
if (ret) {
|
if (ret) {
|
||||||
_this.chooseKey(callback);
|
_this.chooseKey(callback);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue