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
|
!/.vscode
|
||||||
*.log
|
!/.gitignore
|
||||||
|
!/qonq.js
|
||||||
|
!/discord-preloader.js
|
||||||
|
!/package.json
|
||||||
|
!/package-lock.json
|
||||||
|
!/README.md
|
||||||
!.gitkeep
|
!.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
|
||||||
|
}
|
||||||
|
}
|
53
qonq.js
53
qonq.js
@ -1,5 +1,4 @@
|
|||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
process.env.NODE_ENV = "production";
|
|
||||||
var colors = require("colors");
|
var colors = require("colors");
|
||||||
var express = require("express");
|
var express = require("express");
|
||||||
var formidable = require("formidable");
|
var formidable = require("formidable");
|
||||||
@ -14,7 +13,10 @@ if (process.env.HOSTNAMES) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var app = express();
|
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)=>{
|
app.use((req, res, next)=>{
|
||||||
var d = new Date;
|
var d = new Date;
|
||||||
@ -25,36 +27,45 @@ app.use((req, res, next)=>{
|
|||||||
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({
|
var form = new formidable.IncomingForm({
|
||||||
maxFileSize: 2**30, // 1 GiB
|
maxFileSize: 2**30, // 1 GiB
|
||||||
maxFields: 1,
|
maxFields: 1,
|
||||||
uploadDir: process.env.TMP_DIR
|
uploadDir: process.env.TMP_DIR
|
||||||
});
|
});
|
||||||
form.parse(req, function(err, fields, files) {
|
form.parse(req, function(err, fields, files) {
|
||||||
if (err) return next(err);
|
if (err) return void next(err);
|
||||||
var file = files.file;
|
var file = files.file;
|
||||||
if (!file) return res.sendStatus(400);
|
if (!file) return void res.sendStatus(400);
|
||||||
do {
|
(function trymkdir() {
|
||||||
var filecode = Math.random().toString(36).slice(2).substring(0,4);
|
var filecode = Math.random().toString(36).slice(2).substring(0,4);
|
||||||
} while ( fs.existsSync(path.join(FILES_DIR, filecode)) && !console.log("oof") );
|
var webroot = path.join(FILES_DIR, filecode);
|
||||||
try {
|
fs.mkdir(webroot, function (error) {
|
||||||
fs.mkdirSync(path.join(FILES_DIR, filecode));
|
if (error) if (error.code == "EEXIST") {
|
||||||
fs.renameSync(file.path, path.join(FILES_DIR, filecode, file.name));
|
console.log("oof");
|
||||||
} catch(e) {
|
trymkdir();
|
||||||
return next(e);
|
} 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}`;
|
let url = `${req.protocol}://${filecode}.${randomHostname?.() || req.hostname}`;
|
||||||
res.send(url);
|
res.send(url);
|
||||||
require("./discord-preloader.js")(url);
|
require("./discord-preloader.js")(url);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get("*", function(req, res, next){
|
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
|
var filecode = req.hostname.split('.')[0]; // for webroot of main domain i.e. qonq.gq, mkdir files/qonq
|
||||||
let webroot = path.join(FILES_DIR, filecode);
|
var webroot = path.join(FILES_DIR, filecode);
|
||||||
let webrootdirlist = fs.readdirSync(webroot);
|
fs.readdir(webroot, function(error, webrootdirlist) {
|
||||||
|
if (error) return void next(error.code == "ENOENT" ? "route" : error);
|
||||||
if (webrootdirlist.length > 1) {
|
if (webrootdirlist.length > 1) {
|
||||||
req.url = path.join(filecode, req.url);
|
req.url = path.join(filecode, req.url);
|
||||||
next();
|
next();
|
||||||
@ -67,11 +78,5 @@ app.get("*", function(req, res, next){
|
|||||||
res.sendStatus(204);
|
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