mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-22 06:43:10 -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
|
||||
|
||||
var DocumentHandler = function(options) {
|
||||
const winston = require('winston'), Busboy = require('busboy'), DocumentHandler = function (options) {
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
@ -13,6 +9,7 @@ var DocumentHandler = function(options) {
|
|||
this.keyGenerator = options.keyGenerator;
|
||||
};
|
||||
|
||||
|
||||
DocumentHandler.defaultKeyLength = 10;
|
||||
|
||||
// Handle retrieving a document
|
||||
|
@ -29,8 +26,7 @@ DocumentHandler.prototype.handleGet = function(request, response, config) {
|
|||
} else {
|
||||
response.end(JSON.stringify({data: ret, key: key}));
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
winston.warn('document not found', {key: key});
|
||||
response.writeHead(404, {'content-type': 'application/json'});
|
||||
if (request.method === 'HEAD') {
|
||||
|
@ -56,8 +52,7 @@ DocumentHandler.prototype.handleRawGet = function(request, response, config) {
|
|||
} else {
|
||||
response.end(ret);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
winston.warn('raw document not found', {key: key});
|
||||
response.writeHead(404, {'content-type': 'application/json'});
|
||||
if (request.method === 'HEAD') {
|
||||
|
@ -71,12 +66,8 @@ DocumentHandler.prototype.handleRawGet = function(request, response, config) {
|
|||
|
||||
// Handle adding a new Document
|
||||
DocumentHandler.prototype.handlePost = function (request, response) {
|
||||
var _this = this;
|
||||
var buffer = '';
|
||||
var cancelled = false;
|
||||
|
||||
// What to do when done
|
||||
var onSuccess = function () {
|
||||
let _this = this, buffer = '', cancelled = false, onSuccess = function () {
|
||||
// Check length
|
||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||
cancelled = true;
|
||||
|
@ -94,8 +85,7 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
|||
winston.verbose('added document', {key: key});
|
||||
response.writeHead(200, {'content-type': 'application/json'});
|
||||
response.end(JSON.stringify({key: key}));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
winston.verbose('error adding document');
|
||||
response.writeHead(500, {'content-type': 'application/json'});
|
||||
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
|
||||
var ct = request.headers['content-type'];
|
||||
if (ct && ct.split(';')[0] === 'multipart/form-data') {
|
||||
const ct = request.headers['content-type'];
|
||||
if (ct) {
|
||||
if (ct.split(';')[0] === 'multipart/form-data') {
|
||||
var busboy = new Busboy({headers: request.headers});
|
||||
busboy.on('field', function (fieldname, val) {
|
||||
if (fieldname === 'data') {
|
||||
|
@ -123,7 +114,26 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
|||
buffer += data.toString();
|
||||
});
|
||||
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();
|
||||
});
|
||||
request.on('error', function (error) {
|
||||
|
@ -137,8 +147,8 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
|||
|
||||
// Keep choosing keys until one isn't taken
|
||||
DocumentHandler.prototype.chooseKey = function (callback) {
|
||||
var key = this.acceptableKey();
|
||||
var _this = this;
|
||||
const key = this.acceptableKey();
|
||||
const _this = this;
|
||||
this.store.get(key, function (ret) {
|
||||
if (ret) {
|
||||
_this.chooseKey(callback);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue