Update document_handler.js

This commit is contained in:
wioniqle-q 2021-11-15 14:56:37 +03:00 committed by GitHub
commit 87b83bb438
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);