Add files via upload
This commit is contained in:
7
server/routes/err404.js
Normal file
7
server/routes/err404.js
Normal file
@ -0,0 +1,7 @@
|
||||
async function err404(req, res) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
res.statusCode = 404;
|
||||
res.render('404');
|
||||
res.end();
|
||||
}
|
||||
module.exports = err404;
|
7
server/routes/fileTooBig.js
Normal file
7
server/routes/fileTooBig.js
Normal file
@ -0,0 +1,7 @@
|
||||
async function fileTooBig(req, res) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
res.statusCode = 413;
|
||||
res.render('ERR_FILE_TOO_BIG');
|
||||
res.end();
|
||||
}
|
||||
module.exports = fileTooBig;
|
267
server/routes/files.js
Normal file
267
server/routes/files.js
Normal file
@ -0,0 +1,267 @@
|
||||
/* eslint-disable no-lonely-if */
|
||||
const formidable = require('formidable');
|
||||
const fs = require('fs-extra');
|
||||
const { Remarkable } = require('remarkable');
|
||||
const ejs = require('ejs');
|
||||
const exif = require('exif2');
|
||||
|
||||
const md = new Remarkable('full', {
|
||||
html: false,
|
||||
linkify: true,
|
||||
typographer: true,
|
||||
});
|
||||
async function files(req, res) {
|
||||
res.setHeader('Content-Type', 'text/text');
|
||||
const fileName = this.randomToken(this.c.fileNameLength, false);
|
||||
const form = new formidable.IncomingForm();
|
||||
const protocol = this.protocol();
|
||||
// eslint-disable-next-line no-shadow
|
||||
form.parse(req, (err, fields, files) => {
|
||||
let userIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress.split(",")[0]; userIP = userIP.split(",")[0];
|
||||
const authKey = fields.key;
|
||||
let usingUploader = false;
|
||||
if (files.fdataUploader && !fields.key) {
|
||||
usingUploader = true;
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
files.fdata = files.fdataUploader;
|
||||
}
|
||||
if (files.file) {
|
||||
files.fdata = files.file;
|
||||
}
|
||||
if (!this.auth(this.c.key, fields.key, this.c) && usingUploader === false) {
|
||||
res.statusCode = 401;
|
||||
res.write('Unauthorized');
|
||||
res.end();
|
||||
return this.log.warning(`Unauthorized User | File Upload | ${userIP} | ${authKey}`);
|
||||
} if (!this.auth(this.c.key, fields.password, this.c) && usingUploader === true) {
|
||||
this.log.warning(this.auth(this.c.key, fields.password, this.c));
|
||||
res.statusCode = 401;
|
||||
res.redirect('/?error=Incorrect_Password');
|
||||
res.end();
|
||||
return this.log.warning(`Unauthorized User | File Upload | ${userIP} | ${authKey}`);
|
||||
}
|
||||
const oldpath = files.fdata.path;
|
||||
const fileExt = files.fdata.name.substring(files.fdata.name.lastIndexOf('.') + 1, files.fdata.name.length).toLowerCase();
|
||||
let newpath;
|
||||
if(this.c.dateURLPath === true) {
|
||||
let currentMonth = getDate('month')
|
||||
let currentYear = getDate('year')
|
||||
let currentDay = getDate('day')
|
||||
let baseDir = `${__dirname}/../uploads/`
|
||||
let basePWDir = `${__dirname}/../passwordUploads/`
|
||||
fs.access(`${baseDir}${currentYear}/${currentMonth}/${currentDay}`, err => {
|
||||
if (err && err.code === 'ENOENT') {
|
||||
fs.mkdirSync(`${baseDir}${currentYear}`);
|
||||
fs.mkdirSync(`${baseDir}${currentYear}/${currentMonth}`);
|
||||
fs.mkdirSync(`${baseDir}${currentYear}/${currentMonth}/${currentDay}`)
|
||||
}
|
||||
});
|
||||
fs.access(`${basePWDir}${currentYear}/${currentMonth}/${currentDay}`, err => {
|
||||
if (err && err.code === 'ENOENT') {
|
||||
fs.mkdirSync(`${basePWDir}${currentYear}`);
|
||||
fs.mkdirSync(`${basePWDir}${currentYear}/${currentMonth}`);
|
||||
fs.mkdirSync(`${basePWDir}${currentYear}/${currentMonth}/${currentDay}`)
|
||||
}
|
||||
});
|
||||
}
|
||||
fields.pupload
|
||||
? newpath = `${__dirname}/../passwordUploads/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${fileName}.${fileExt}`
|
||||
: newpath = `${__dirname}/../uploads/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${fileName}.${fileExt}`;
|
||||
let returnedFileName;
|
||||
if (!fileExt.includes('png') && !fileExt.includes('jpg') && !fileExt.includes('jpeg') && !fileExt.includes('md') && !fields.pupload) {
|
||||
returnedFileName = `${fileName}.${fileExt}`;
|
||||
} else {
|
||||
returnedFileName = fileName;
|
||||
}
|
||||
if(fields.showCase) {
|
||||
fields.showCase = true
|
||||
}
|
||||
let showCaseFile;
|
||||
if(fields.showCase !== false) {
|
||||
showCaseFile = this.randomToken(this.c.fileNameLength, false);
|
||||
}
|
||||
this.db.get('files')
|
||||
.push({
|
||||
path: fields.showCase ? `/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${showCaseFile}` : `/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName}`,
|
||||
ip: userIP,
|
||||
views: 0,
|
||||
original: newpath,
|
||||
showCase: fields.showCase ? true : false
|
||||
})
|
||||
.write();
|
||||
let settings;
|
||||
let isAdmin = false;
|
||||
if (!this.c.admin.key.includes(fields.key)) {
|
||||
settings = this.c;
|
||||
} else {
|
||||
settings = this.c.admin;
|
||||
isAdmin = true;
|
||||
}
|
||||
if (Math.round((files.fdata.size / 1024) / 1000) > settings.maxUploadSize && !isAdmin) {
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[FAILED UPLOAD][USER]\n[FILE](${files.fdata.name})\n[SIZE](${Math.round(files.fdata.size / 1024)}KB)\n[TYPE](${files.fdata.type})\n[KEY](${authKey})\n[IP](${userIP})\n\n[ERROR](ERR_FILE_TOO_BIG)\`\`\``);
|
||||
res.statusCode = 413;
|
||||
if (usingUploader === true) {
|
||||
res.redirect('/?error=File_Too_Big');
|
||||
return res.end();
|
||||
}
|
||||
res.write(`${protocol}://${req.headers.host}/ERR_FILE_TOO_BIG`);
|
||||
return res.end();
|
||||
}
|
||||
if (!settings.allowed.some(ext => fileExt.endsWith(ext)) && !settings.allowed.includes("*")) {
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[FAILED UPLOAD][USER]\n[FILE](${files.fdata.name})\n[SIZE](${Math.round(files.fdata.size / 1024)}KB)\n[TYPE](${files.fdata.type})\n[KEY](${authKey})\n[IP](${userIP})\n\n[ERROR](ERR_ILLEGAL_FILE_TYPE)\`\`\``);
|
||||
res.statusCode = 415;
|
||||
if (usingUploader === true) {
|
||||
res.redirect('/?error=Illegal_File_Type');
|
||||
return res.end();
|
||||
}
|
||||
res.write(`${protocol}://${req.headers.host}/ERR_ILLEGAL_FILE_TYPE`);
|
||||
return res.end();
|
||||
}
|
||||
if (fields.pupload) {
|
||||
let altKey = this.randomToken(this.c.puploadKeyGenLength, true);
|
||||
fs.move(oldpath, newpath, () => {
|
||||
let puploadKey
|
||||
if(fields.pupload === '*random*') {
|
||||
puploadKey = altKey;
|
||||
} else {
|
||||
puploadKey = fields.pupload;
|
||||
}
|
||||
this.db.get('passwordUploads')
|
||||
.push({
|
||||
fileName: `${fileName}.${fileExt}`,
|
||||
key: puploadKey,
|
||||
})
|
||||
.write();
|
||||
fs.readFile(newpath, 'utf-8', () => {
|
||||
const stream = fs.createWriteStream(`${__dirname}/../uploads/${fileName}.html`);
|
||||
stream.once('open', () => {
|
||||
ejs.renderFile(`${__dirname}/../views/puploadAuth.ejs`, {
|
||||
fileName: `${fileName}.${fileExt}`,
|
||||
}, {}, (_err, str) => {
|
||||
stream.write(str);
|
||||
});
|
||||
stream.end();
|
||||
});
|
||||
});
|
||||
});
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW UPLOAD][USER]\n[SIZE](${Math.round(files.fdata.size / 1024)}KB)\n[TYPE](${files.fdata.type})\n[KEY](${authKey})\n[IP](${userIP})\n\`\`\`\n${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName}`);
|
||||
if (err) return res.write(err);
|
||||
this.log.verbose(`New File Upload: ${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName} | IP: ${userIP} | KEY: ${authKey}`);
|
||||
if (usingUploader === true) {
|
||||
res.redirect(`/?success=${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName}`);
|
||||
return res.end();
|
||||
}
|
||||
fields.pupload === '*random*' ? res.write(`URL: ${protocol}://${req.headers.host}/${returnedFileName} | KEY: ${altKey}`) : res.write(`${protocol}://${req.headers.host}/${returnedFileName}`);
|
||||
return res.end();
|
||||
}
|
||||
if (fields.showCase === true) {
|
||||
if(fileExt === "png" || fileExt === "jpg" || fileExt === "gif" || fileExt === "jpeg") {
|
||||
returnedFileName = `${showCaseFile}.html`
|
||||
fs.move(oldpath, newpath, () => {
|
||||
fs.readFile(newpath, 'utf-8', (err, data) => {
|
||||
exif(newpath, (err, obj) => {
|
||||
if(!obj['camera model name']) obj['camera model name'] = "N/A";
|
||||
if(!obj['f number']) obj['f number'] = "N/A";
|
||||
if(!obj['exposure time']) obj['exposure time'] = "N/A";
|
||||
if(!obj['iso']) obj['iso'] = "N/A";
|
||||
if(!obj['focal length']) obj['focal length'] = "N/A";
|
||||
if(!obj['image size']) obj['image size'] = "N/A";
|
||||
if(!obj['lens id']) obj['lens id'] = "N/A";
|
||||
let camera = obj['camera model name'].replace(/<|>|<|>/gm, "")
|
||||
let fstop = `f/${obj['f number']}`.replace(/<|>|<|>/gm, "")
|
||||
let shutter = obj['exposure time'].replace(/<|>|<|>/gm, "")
|
||||
let iso = obj['iso'].replace(/<|>|<|>/gm, "")
|
||||
let focal = obj['focal length'].replace(/<|>|<|>/gm, "")
|
||||
let dims = obj['image size'].replace(/<|>|<|>/gm, "")
|
||||
let lens = obj['lens id'].replace(/<|>|<|>/gm, "")
|
||||
let width = parseInt(dims.split('x')[0]);
|
||||
let height = parseInt(dims.split('x')[1]);
|
||||
if(height > 700) {
|
||||
let magicNumber = height / 700;
|
||||
height = height / magicNumber;
|
||||
width = width / magicNumber
|
||||
}
|
||||
let sizing = [width, height]
|
||||
const stream = fs.createWriteStream(`${__dirname}/../uploads/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${showCaseFile}.html`);
|
||||
stream.once('open', () => {
|
||||
ejs.renderFile(`${__dirname}/../views/photoShowCase.ejs`, {
|
||||
camera: camera,
|
||||
fstop, fstop,
|
||||
shutter, shutter,
|
||||
iso: iso,
|
||||
focal: focal,
|
||||
dims: dims,
|
||||
lens: lens,
|
||||
width: sizing[0],
|
||||
height: sizing[1],
|
||||
filename: `${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${fileName}.${fileExt}`
|
||||
}, {}, (_err, str) => {
|
||||
stream.write(str);
|
||||
});
|
||||
stream.end();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW UPLOAD][USER]\n[SIZE](${Math.round(files.fdata.size / 1024)}KB)\n[TYPE](${files.fdata.type})\n[KEY](${authKey})\n[IP](${userIP})\n\`\`\`\n${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${showCaseFile}`);
|
||||
if (err) return res.write(err);
|
||||
this.log.verbose(`New File Upload: ${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${showCaseFile} | IP: ${userIP} | KEY ${authKey}`);
|
||||
if (usingUploader === true) {
|
||||
res.redirect(`/?success=${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${showCaseFile}`);
|
||||
return res.end();
|
||||
}
|
||||
res.write(`${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${showCaseFile}`);
|
||||
return res.end();
|
||||
}
|
||||
}
|
||||
fs.move(oldpath, newpath, () => {
|
||||
if (fileExt.toLowerCase() === 'md' && this.c.markdown) {
|
||||
fs.readFile(newpath, 'utf-8', (_readErr, data) => {
|
||||
const stream = fs.createWriteStream(`${__dirname}/../uploads/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${fileName}.html`);
|
||||
stream.once('open', () => {
|
||||
ejs.renderFile(`${__dirname}/../views/md.ejs`, {
|
||||
ogDesc: data.match(/.{1,297}/g)[0],
|
||||
mdRender: md.render(data),
|
||||
}, {}, (_renderErr, str) => {
|
||||
stream.write(str);
|
||||
});
|
||||
stream.end();
|
||||
fs.unlink(newpath, delErr => {
|
||||
if (delErr) return this.log.warning(delErr);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW UPLOAD][USER]\n[SIZE](${Math.round(files.fdata.size / 1024)}KB)\n[TYPE](${files.fdata.type})\n[IP](${userIP})\n[KEY](${authKey})\n\`\`\`\n${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName}`);
|
||||
if (err) return res.write(err);
|
||||
this.log.verbose(`New File Upload: ${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName} | IP: ${userIP} | KEY: ${authKey}`);
|
||||
if (usingUploader === true) {
|
||||
res.redirect(`/?success=${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName}`);
|
||||
return res.end();
|
||||
}
|
||||
res.write(`${protocol}://${req.headers.host}/${this.c.dateURLPath === true ? `${getDate('year')}/${getDate('month')}/${getDate('day')}/`: ""}${returnedFileName}`);
|
||||
return res.end();
|
||||
});
|
||||
});
|
||||
}
|
||||
//const currentMonth = date.getMonth() + 1;
|
||||
function getDate(type) {
|
||||
if(type.toLowerCase() === 'year') {
|
||||
const date = new Date();
|
||||
const currentYear = date.getFullYear();
|
||||
return currentYear;
|
||||
}
|
||||
if(type.toLowerCase() === 'month') {
|
||||
const date = new Date();
|
||||
let currentMonth = `${date.getMonth() + 1}`;
|
||||
if(currentMonth.length === 1) currentMonth = `0${currentMonth}`
|
||||
return currentMonth;
|
||||
}
|
||||
if(type.toLowerCase() === 'day') {
|
||||
const date = new Date();
|
||||
let currentDay = `${date.getDate()}`;
|
||||
if(currentDay.length === 1) currentDay = `0${currentDay}`;
|
||||
return currentDay;
|
||||
}
|
||||
}
|
||||
module.exports = files;
|
51
server/routes/gallery.js
Normal file
51
server/routes/gallery.js
Normal file
@ -0,0 +1,51 @@
|
||||
const fs = require('fs-extra');
|
||||
|
||||
async function get(_req, res) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
res.render('galleryLogin');
|
||||
res.end();
|
||||
}
|
||||
async function post(req, res) {
|
||||
const userIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
const protocol = this.protocol();
|
||||
var password = this.c.admin.key;
|
||||
// Compatibility with old config
|
||||
if(typeof password == "string"){
|
||||
password = [password];
|
||||
}
|
||||
if (!this.c.admin.key.includes(req.body.password)) {
|
||||
res.statusCode = 401;
|
||||
res.render('unauthorized');
|
||||
res.end();
|
||||
return this.log.warning(`Unauthorized User | Gallery Access | ${userIP} | ${req.body.password}`);
|
||||
}
|
||||
this.log.warning(`IP Address: ${userIP} successfully accessed gallery with key ${req.body.password}`);
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[GALLERY ACCESS][USER]\n[IP](${userIP})\n[KEY](${req.body.password})\n\`\`\``);
|
||||
fs.readdir(`${__dirname}/../uploads`, (err, files) => {
|
||||
let pics = [];
|
||||
files = files.map(fileName => {
|
||||
return {
|
||||
name: fileName,
|
||||
time: fs.statSync(`${__dirname}/../uploads/${fileName}`).mtime.getTime()
|
||||
};
|
||||
})
|
||||
files.sort((a, b) => {
|
||||
return b.time - a.time; });
|
||||
files = files.map(v => {
|
||||
return v.name; });
|
||||
files.forEach((file, idx, array) => {
|
||||
if (file.toString().includes('.jpg') || file.toString().includes('.png') || file.toString().includes('.gif')) {
|
||||
pics.push(`${protocol}://${req.headers.host}/${file.toString()}`);
|
||||
}
|
||||
if (idx === array.length - 1) {
|
||||
res.render('gallery', {
|
||||
pictures: pics,
|
||||
});
|
||||
return res.end();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
}
|
||||
module.exports = { get, post };
|
7
server/routes/illegalFileType.js
Normal file
7
server/routes/illegalFileType.js
Normal file
@ -0,0 +1,7 @@
|
||||
async function illegalFileType(req, res) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
res.statusCode = 413;
|
||||
res.render('ERR_FILE_TOO_BIG');
|
||||
res.end();
|
||||
}
|
||||
module.exports = illegalFileType;
|
7
server/routes/index.js
Normal file
7
server/routes/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
require('fs')
|
||||
.readdirSync(__dirname)
|
||||
.map(filename => {
|
||||
const moduleName = filename.split('.')[0];
|
||||
// eslint-disable-next-line global-require
|
||||
exports[moduleName] = require(`${__dirname}/${filename}`);
|
||||
});
|
59
server/routes/paste.js
Normal file
59
server/routes/paste.js
Normal file
@ -0,0 +1,59 @@
|
||||
/* eslint-disable consistent-return */
|
||||
const formidable = require('formidable');
|
||||
const fs = require('fs-extra');
|
||||
const ejs = require('ejs');
|
||||
|
||||
async function paste(req, res) {
|
||||
res.setHeader('Content-Type', 'text/text');
|
||||
const fileName = this.randomToken(5); // 916,132,832 possible file names
|
||||
const form = new formidable.IncomingForm();
|
||||
const protocol = this.protocol();
|
||||
form.parse(req, (err, fields, files) => {
|
||||
const userIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
|
||||
if (!this.auth(this.c.key, fields.key, this.c)) {
|
||||
res.statusCode = 401;
|
||||
res.write('Unauthorized');
|
||||
res.end();
|
||||
return this.log.warning(`Unauthorized User | File Upload | ${userIP}`);
|
||||
}
|
||||
this.db.get('files')
|
||||
.push({
|
||||
path: `/${fileName}`,
|
||||
ip: userIP,
|
||||
views: 0,
|
||||
})
|
||||
.write();
|
||||
const oldpath = files.fdata.path;
|
||||
const newpath = `${__dirname}/../uploads/${fileName + files.fdata.name.toString().match(/(\.)+([a-zA-Z0-9]+)+/g, '').toString()}`;
|
||||
if (Math.round((files.fdata.size / 1024) / 1000) > this.c.paste.max_upload_size) {
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[FAILED PASTE][USER]\n[FILE](${files.fdata.name})\n[SIZE](${Math.round(files.fdata.size / 1024)}KB)\n[TYPE](${files.fdata.type})\n[IP](${userIP})\n\n[ERROR](ERR_FILE_TOO_BIG)\`\`\``);
|
||||
res.statusCode = 413;
|
||||
res.write(`${protocol}://${req.headers.host}/ERR_FILE_TOO_BIG`);
|
||||
return res.end();
|
||||
}
|
||||
fs.move(oldpath, newpath, () => {
|
||||
fs.readFile(newpath, 'utf-8', (_err, data) => {
|
||||
const stream = fs.createWriteStream(`${__dirname}/../uploads/${fileName}.html`);
|
||||
stream.once('open', () => {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
let cleaned = data.replace(/>/g, '>');
|
||||
cleaned = cleaned.replace(/</g, '<');
|
||||
ejs.renderFile(`${__dirname}/../views/paste.ejs`, {
|
||||
ogDesc: data.match(/.{1,297}/g)[0],
|
||||
pData: data,
|
||||
}, {}, (_renderErr, str) => {
|
||||
stream.write(str);
|
||||
});
|
||||
stream.end();
|
||||
fs.unlink(newpath, delErr => {
|
||||
if (delErr) return this.log.warning(delErr);
|
||||
});
|
||||
res.write(`${protocol}://${req.headers.host}/${fileName}`);
|
||||
return res.end();
|
||||
});
|
||||
});
|
||||
});
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW PASTE][USER]\n[SIZE](${Math.round(files.fdata.size / 1024)}KB)\n[IP](${userIP})\n\`\`\`\n${protocol}://${req.headers.host}/${fileName}`);
|
||||
});
|
||||
}
|
||||
module.exports = paste;
|
24
server/routes/pupload.js
Normal file
24
server/routes/pupload.js
Normal file
@ -0,0 +1,24 @@
|
||||
const fs = require('fs-extra');
|
||||
|
||||
async function pupload(req, res) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
const givenPassword = req.body.password;
|
||||
const givenFileName = req.body.file;
|
||||
const entry = this.db.get('passwordUploads').find({ fileName: givenFileName }).value();
|
||||
if (entry.key !== givenPassword) {
|
||||
res.statusCode = 401;
|
||||
res.render('unauthorized');
|
||||
return res.end();
|
||||
}
|
||||
const filePath = `${__dirname}/../passwordUploads/${entry.fileName}`;
|
||||
const file = fs.readFileSync(filePath);
|
||||
if(entry.fileName.includes('.mp3')) {
|
||||
res.set('Content-Type', 'text/html');
|
||||
let base64Str = new Buffer(file).toString('base64');
|
||||
res.render('mp3', { data: base64Str })
|
||||
} else {
|
||||
res.set('Content-Type', this.mimeType(entry.fileName));
|
||||
res.send(file);
|
||||
}
|
||||
}
|
||||
module.exports = pupload;
|
36
server/routes/short.js
Normal file
36
server/routes/short.js
Normal file
@ -0,0 +1,36 @@
|
||||
const fs = require('fs-extra');
|
||||
|
||||
async function get(req, res) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
res.render('short', { public: this.c.public });
|
||||
res.end();
|
||||
}
|
||||
async function post(req, res) {
|
||||
const userIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
|
||||
res.setHeader('Content-Type', 'text/text');
|
||||
if (!this.auth(this.c.key, req.body.password, this.c) && !this.c.public) {
|
||||
res.statusCode = 401;
|
||||
res.redirect('/short?error=Incorrect_Password');
|
||||
res.end();
|
||||
return this.log.warning(`Unauthorized User | URL Shorten | ${userIP}`);
|
||||
}
|
||||
const protocol = this.protocol();
|
||||
const fileName = this.randomToken(this.c.shortUrlLength);
|
||||
if (req.body.URL === '' || req.body.URL === null) {
|
||||
res.redirect('/short?error=No URL Input');
|
||||
return res.end();
|
||||
}
|
||||
const stream = fs.createWriteStream(`${__dirname}/../uploads/${fileName}.html`);
|
||||
stream.once('open', () => {
|
||||
stream.write(`<meta http-equiv="refresh" content="0;URL='${req.body.URL}'" />`);
|
||||
stream.end();
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW][SHORT URL]\n[URL](${req.body.URL})\n[NEW](${req.headers.host}/${fileName})\n[IP](${userIP})\n\`\`\``);
|
||||
this.log.verbose(`New Short URL: ${protocol}://${req.headers.host}/${fileName} | IP: ${userIP}`);
|
||||
res.redirect(`/short?success=${protocol}://${req.headers.host}/${fileName}`);
|
||||
this.db.get('files')
|
||||
.push({ path: `/${fileName}`, ip: userIP, views: 0 })
|
||||
.write();
|
||||
return res.end();
|
||||
});
|
||||
}
|
||||
module.exports = { get, post };
|
41
server/routes/shortener.js
Normal file
41
server/routes/shortener.js
Normal file
@ -0,0 +1,41 @@
|
||||
/* eslint-disable no-useless-escape */
|
||||
const formidable = require('formidable');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
async function shortener(req, res) {
|
||||
const form = new formidable.IncomingForm();
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
form.parse(req, (_err, fields, _files) => {
|
||||
const userIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
|
||||
const protocol = this.protocol();
|
||||
if (!this.auth(this.c.key, fields.key, this.c)) {
|
||||
res.statusCode = 401;
|
||||
res.write('Unauthorized');
|
||||
res.end();
|
||||
return this.log.warning(`Unauthorized User | File Upload | ${userIP}`);
|
||||
}
|
||||
const fileName = this.randomToken(4); // 14,776,336 possible file names
|
||||
const url = req.headers.url;
|
||||
if (url == null) {
|
||||
res.send('NO_URL_PROVIDED');
|
||||
return res.end();
|
||||
}
|
||||
if (!/([-a-zA-Z0-9^\p{L}\p{C}\u00a1-\uffff@:%_\+.~#?&//=]{2,256}){1}(\.[a-z]{2,4}){1}(\:[0-9]*)?(\/[-a-zA-Z0-9\u00a1-\uffff\(\)@:%,_\+.~#?&//=]*)?([-a-zA-Z0-9\(\)@:%,_\+.~#?&//=]*)?/.test(url.toLowerCase().toString())) {
|
||||
res.send('NOT_A_VALID_URL');
|
||||
return res.end();
|
||||
}
|
||||
const stream = fs.createWriteStream(`${__dirname}/../uploads/${fileName}.html`);
|
||||
stream.once('open', () => {
|
||||
stream.write(`<meta http-equiv="refresh" content="0; url=${url}" />`);
|
||||
stream.end();
|
||||
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW][SHORT URL]\n[URL](${url})\n[NEW](${req.headers.host}/${fileName})\n[IP](${userIP})\n\`\`\``);
|
||||
this.log.verbose(`New Short URL: ${protocol}://${req.headers.host}/${fileName} | IP: ${userIP}`);
|
||||
res.write(`${protocol}://${req.headers.host}/${fileName}`);
|
||||
this.db.get('files')
|
||||
.push({ path: `/${fileName}`, ip: userIP, views: 0 })
|
||||
.write();
|
||||
return res.end();
|
||||
});
|
||||
});
|
||||
}
|
||||
module.exports = shortener;
|
6
server/routes/upload.js
Normal file
6
server/routes/upload.js
Normal file
@ -0,0 +1,6 @@
|
||||
async function upload(_req, res) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
res.statusCode = 200;
|
||||
res.render('upload', { public: this.c.public });
|
||||
}
|
||||
module.exports = upload;
|
Reference in New Issue
Block a user