add some fancy magic :kekw:

This commit is contained in:
2025-10-13 18:21:23 +02:00
parent 2e59c1f5e7
commit 22badaf535
15 changed files with 1328 additions and 786 deletions

View File

@@ -1,118 +1,120 @@
var logger = require('winston');
var KeyGenerator = require('./key_generator.js');
const KeyGenerator = require('./key_generator.js');
// handles creating new and requesting existing documents
var DocumentHandler = function(options) {
if (!options) {
options = {};
}
this.store = options.store;
this.maxLength = options.maxLength || 50000;
this.keyLength = options.keyLength || 10;
this.createKey = options.createKey || '';
this.keyGenerator = new KeyGenerator();
class DocumentHandler {
constructor(options) {
if (!options) {
options = {};
}
this.store = options.store;
this.logger = options.logger;
this.maxLength = options.maxLength || 50000;
this.keyLength = options.keyLength || 10;
this.createKey = options.createKey || '';
this.keyGenerator = new KeyGenerator();
if (this.createKey !== '') {
logger.info("Creation-Key:", this.createKey);
}
};
if (this.createKey !== '') {
this.logger.info("Creation-Key is configured.");
}
}
// handles existing documents
DocumentHandler.prototype.handleGet = function(key, res) {
this.store.get(key, function(ret) {
if (ret) {
logger.verbose('Open paste:', key);
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify({key: key, data: ret.replace(/\t/g, ' ')}));
} else {
logger.verbose('Paste not found:', key);
res.writeHead(404, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Paste not found.'}));
}
});
};
// handles existing documents
handleGet(key, res) {
this.store.get(key, (ret) => {
if (ret) {
this.logger.verbose(`Open paste: ${key}`);
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ key: key, data: ret.replace(/\t/g, ' ') }));
} else {
this.logger.verbose(`Paste not found: ${key}`);
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Paste not found.' }));
}
});
}
// handles exisiting documents (raw)
DocumentHandler.prototype.handleRawGet = function(key, res) {
this.store.get(key, function(ret) {
if (ret) {
logger.verbose('Open paste:', key);
res.writeHead(200, {'content-type': 'text/plain'});
res.end(ret);
} else {
logger.verbose('Paste not found:', key);
res.writeHead(404, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Paste not found.'}));
}
});
};
// handles existing documents (raw)
handleRawGet(key, res) {
this.store.get(key, (ret) => {
if (ret) {
this.logger.verbose(`Open raw paste: ${key}`);
res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' });
res.end(ret);
} else {
this.logger.verbose(`Paste not found: ${key}`);
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Paste not found.' }));
}
});
}
// handles creating new documents
DocumentHandler.prototype.handlePost = function(req, res) {
var _this = this;
var buffer = '';
var cancelled = false;
req.on('data', function(data) {
if (cancelled) return;
buffer += data.toString();
if (_this.maxLength && buffer.length > _this.maxLength) {
cancelled = true;
logger.warn('Paste exeeds maximum length.');
res.writeHead(400, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Paste exceeds maximum length.'}));
}
});
req.on('end', function() {
if (cancelled) return;
// handles creating new documents
handlePost(req, res) {
let buffer = '';
let cancelled = false;
if (_this.createKey !== '') {
if (!buffer.startsWith(_this.createKey)) {
logger.warn('Error adding new paste: wrong key');
res.writeHead(400, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Error adding new paste: wrong key'}));
return;
}
buffer = buffer.substring(_this.createKey.length);
}
req.on('data', (data) => {
if (cancelled) return;
buffer += data.toString();
if (this.maxLength && buffer.length > this.maxLength) {
cancelled = true;
this.logger.warn('Paste exceeds maximum length.');
res.writeHead(400, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Paste exceeds maximum length.' }));
}
});
_this.chooseKey(function(key) {
_this.store.set(key, buffer, function(success) {
if (success) {
logger.verbose('New paste:', key);
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify({key: key}));
} else {
logger.warn('Error adding new paste.');
res.writeHead(500, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Error adding new paste.'}));
}
});
});
});
req.on('error', function(error) {
logger.error('Connection error: ' + error.message);
res.writeHead(500, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Connection error.'}));
});
};
req.on('end', () => {
if (cancelled) return;
// creates new keys until one is not taken
DocumentHandler.prototype.chooseKey = function(callback) {
var key = this.acceptableKey();
var _this = this;
this.store.get(key, function(success) {
if (success) {
_this.chooseKey(callback);
} else {
callback(key);
}
});
};
if (this.createKey !== '') {
if (!buffer.startsWith(this.createKey)) {
this.logger.warn('Error adding new paste: wrong key');
res.writeHead(403, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Error adding new paste: wrong key' }));
return;
}
buffer = buffer.substring(this.createKey.length);
}
// creates a new key using the key-generator
DocumentHandler.prototype.acceptableKey = function() {
return this.keyGenerator.createKey(this.keyLength);
};
this.chooseKey((key) => {
this.store.set(key, buffer, (success) => {
if (success) {
this.logger.verbose(`New paste: ${key}`);
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ key: key }));
} else {
this.logger.warn('Error adding new paste.');
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Error adding new paste.' }));
}
});
});
});
module.exports = DocumentHandler;
req.on('error', (error) => {
this.logger.error(`Connection error: ${error.message}`);
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Connection error.' }));
});
}
// creates new keys until one is not taken
chooseKey(callback) {
const key = this.acceptableKey();
this.store.get(key, (success) => {
if (success) {
this.chooseKey(callback);
} else {
callback(key);
}
});
}
// creates a new key using the key-generator
acceptableKey() {
return this.keyGenerator.createKey(this.keyLength);
}
}
module.exports = DocumentHandler;