Compare commits
6 Commits
35b07880eb
...
f7971fef0f
Author | SHA1 | Date | |
---|---|---|---|
f7971fef0f | |||
96436fe340 | |||
7f44d5b676 | |||
9eed8a87d5 | |||
312c95adb7 | |||
1b1601fc7c |
13
.gitignore
vendored
13
.gitignore
vendored
@ -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
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.watcherExclude": {
|
||||
"**/files/**": true
|
||||
}
|
||||
}
|
83
qonq.js
83
qonq.js
@ -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,53 +27,56 @@ 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);
|
||||
}
|
||||
let url = `${req.protocol}://${filecode}.${randomHostname?.() || req.hostname}`;
|
||||
res.send(url);
|
||||
require("./discord-preloader.js")(url);
|
||||
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);
|
||||
if (webrootdirlist.length > 1) {
|
||||
req.url = path.join(filecode, req.url);
|
||||
next();
|
||||
} else if (webrootdirlist.length == 1) {
|
||||
res.sendFile(webrootdirlist[0], {
|
||||
root: path.join(process.cwd(), webroot),
|
||||
headers: {"Content-Disposition": `filename=${webrootdirlist[0]}`}
|
||||
});
|
||||
} else {
|
||||
res.sendStatus(204);
|
||||
}
|
||||
});
|
||||
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();
|
||||
} else if (webrootdirlist.length == 1) {
|
||||
res.sendFile(webrootdirlist[0], {
|
||||
root: path.join(process.cwd(), webroot),
|
||||
headers: {"Content-Disposition": `filename=${webrootdirlist[0]}`}
|
||||
});
|
||||
} 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);
|
||||
|
Loading…
Reference in New Issue
Block a user