Compare commits

...

6 Commits

Author SHA1 Message Date
f7971fef0f Merge branch 'master' of gitea.moe:lamp/qonq 2021-10-22 15:10:24 -05:00
96436fe340 async refactor 2021-10-22 15:10:18 -05:00
7f44d5b676 some refactor
also fixed trust proxy bug
2021-10-22 14:40:07 -05:00
9eed8a87d5 we don't need to care about the post url 2021-10-22 14:06:53 -05:00
312c95adb7 update gitignore 2021-10-22 14:04:54 -05:00
1b1601fc7c vscode dont track files 2021-10-22 14:00:56 -05:00
3 changed files with 57 additions and 44 deletions

13
.gitignore vendored
View File

@ -1,6 +1,9 @@
files/*
node_modules
*.log
*
!/.vscode
!/.gitignore
!/qonq.js
!/discord-preloader.js
!/package.json
!/package-lock.json
!/README.md
!.gitkeep
ecosystem.config.js
.env

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.watcherExclude": {
"**/files/**": true
}
}

55
qonq.js
View File

@ -1,5 +1,4 @@
require("dotenv").config();
process.env.NODE_ENV = "production";
var colors = require("colors");
var express = require("express");
var formidable = require("formidable");
@ -14,7 +13,10 @@ if (process.env.HOSTNAMES) {
}
var app = express();
app.enable('trust proxy', '127.0.0.1');
app.set("env", "production");
app.set('trust proxy', '127.0.0.1');
app.listen(process.env.PORT || 8568, process.env.ADDRESS);
app.use((req, res, next)=>{
var d = new Date;
@ -25,36 +27,45 @@ app.use((req, res, next)=>{
next();
});
app.post("/upload", (req, res, next) => {
if (req.headers.authentication != process.env.AUTH_TOKEN) return res.status(403).send("Unauthorized");
app.post("*", (req, res, next) => {
if (req.headers.authentication != process.env.AUTH_TOKEN) return void res.status(403).send("Unauthorized");
var form = new formidable.IncomingForm({
maxFileSize: 2**30, // 1 GiB
maxFields: 1,
uploadDir: process.env.TMP_DIR
});
form.parse(req, function(err, fields, files) {
if (err) return next(err);
if (err) return void next(err);
var file = files.file;
if (!file) return res.sendStatus(400);
do {
if (!file) return void res.sendStatus(400);
(function trymkdir() {
var filecode = Math.random().toString(36).slice(2).substring(0,4);
} while ( fs.existsSync(path.join(FILES_DIR, filecode)) && !console.log("oof") );
try {
fs.mkdirSync(path.join(FILES_DIR, filecode));
fs.renameSync(file.path, path.join(FILES_DIR, filecode, file.name));
} catch(e) {
return next(e);
}
var webroot = path.join(FILES_DIR, filecode);
fs.mkdir(webroot, function (error) {
if (error) if (error.code == "EEXIST") {
console.log("oof");
trymkdir();
} else next(error);
else {
fs.rename(file.path, path.join(webroot, file.name), function (error) {
if (error) return void next(error);
let url = `${req.protocol}://${filecode}.${randomHostname?.() || req.hostname}`;
res.send(url);
require("./discord-preloader.js")(url);
});
}
});
})();
});
});
app.get("*", function(req, res, next){
let filecode = req.hostname.split('.')[0]; // for home page just create file in directory named as the last level of base hostname
let webroot = path.join(FILES_DIR, filecode);
let webrootdirlist = fs.readdirSync(webroot);
var filecode = req.hostname.split('.')[0]; // for webroot of main domain i.e. qonq.gq, mkdir files/qonq
var webroot = path.join(FILES_DIR, filecode);
fs.readdir(webroot, function(error, webrootdirlist) {
if (error) return void next(error.code == "ENOENT" ? "route" : error);
if (webrootdirlist.length > 1) {
req.url = path.join(filecode, req.url);
next();
@ -66,12 +77,6 @@ app.get("*", function(req, res, next){
} else {
res.sendStatus(204);
}
});
});
}, express.static(FILES_DIR), serveIndex(FILES_DIR, {icons: true}));
app.get("*", express.static(FILES_DIR), serveIndex(FILES_DIR, {icons: true}));
app.use(function (error, req, res, next) {
res.status(error.code == "ENOENT" ? 404 : console.error(error.stack) || 500).type("text/plain").send(error.toString());
});
app.listen(process.env.PORT || 8568, process.env.ADDRESS);