mirror of
https://github.com/MrUnknownDE/unknownbin.git
synced 2026-04-27 01:53:48 +02:00
first push
This commit is contained in:
118
lib/document_handler.js
Normal file
118
lib/document_handler.js
Normal file
@@ -0,0 +1,118 @@
|
||||
var logger = require('winston');
|
||||
|
||||
var 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();
|
||||
|
||||
if (this.createKey !== '') {
|
||||
logger.info("Creation-Key:", this.createKey);
|
||||
}
|
||||
};
|
||||
|
||||
// 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 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 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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
_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.'}));
|
||||
});
|
||||
};
|
||||
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// creates a new key using the key-generator
|
||||
DocumentHandler.prototype.acceptableKey = function() {
|
||||
return this.keyGenerator.createKey(this.keyLength);
|
||||
};
|
||||
|
||||
module.exports = DocumentHandler;
|
||||
38
lib/file_storage.js
Normal file
38
lib/file_storage.js
Normal file
@@ -0,0 +1,38 @@
|
||||
var fs = require('fs');
|
||||
var crypto = require('crypto');
|
||||
|
||||
var logger = require('winston');
|
||||
|
||||
// handles saving and retrieving all documents
|
||||
var FileDocumentStore = function(options) {
|
||||
this.basePath = options.path || './data';
|
||||
logger.info('Path to data: ' + this.basePath);
|
||||
};
|
||||
|
||||
// saves a new file to the filesystem
|
||||
FileDocumentStore.prototype.set = function(key, data, callback) {
|
||||
var _this = this;
|
||||
fs.mkdir(this.basePath, '700', function(err) {
|
||||
fs.writeFile(_this.basePath + '/' + key, data, 'utf8', function(err) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
} else {
|
||||
callback(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// gets an exisiting file from the filesystem
|
||||
FileDocumentStore.prototype.get = function(key, callback) {
|
||||
var _this = this;
|
||||
fs.readFile(this.basePath + '/' + key, 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
} else {
|
||||
callback(data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = FileDocumentStore;
|
||||
15
lib/key_generator.js
Normal file
15
lib/key_generator.js
Normal file
@@ -0,0 +1,15 @@
|
||||
var KeyGenerator = function() {
|
||||
this.keyspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
};
|
||||
|
||||
KeyGenerator.prototype.createKey = function(keyLength) {
|
||||
var key = '';
|
||||
var index;
|
||||
for (var i = 0; i < keyLength; i++) {
|
||||
index = Math.floor(Math.random() * this.keyspace.length);
|
||||
key += this.keyspace.charAt(index);
|
||||
}
|
||||
return key;
|
||||
};
|
||||
|
||||
module.exports = KeyGenerator;
|
||||
Reference in New Issue
Block a user