quad-j/index.js

157 lines
4.5 KiB
JavaScript
Raw Normal View History

2020-10-01 23:10:41 +03:00
const config = require('./config');
const express = require('express');
const fileUpload = require('express-fileupload');
const path = require('path');
const fs = require('fs');
const mime = require('mime-types')
2020-10-05 00:15:13 +03:00
const cors = require('cors');
2020-10-01 23:10:41 +03:00
const app = express();
app.use(fileUpload());
app.set('view engine', 'ejs')
2020-10-02 22:46:23 +03:00
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
2020-10-01 23:10:41 +03:00
const isPathValid = (filename, filePath) => {
if (!filename) {
return false;
}
if (filename.indexOf('\0') !== -1 || filename.indexOf('%') !== -1 || filename.indexOf('/') !== -1 || filename.indexOf('..') !== -1) {
return false;
}
if (!/[A-Za-z1-9.]+/g.test(filename)) {
return false;
}
2020-10-01 23:10:41 +03:00
if (filePath.indexOf(config.storagePath) !== 0) {
return false;
}
return true;
};
const isFilenameValid = (filename) => {
if (!filename) {
return false;
}
if (filename.indexOf('\0') !== -1 || filename.indexOf('%') !== -1 || filename.indexOf('..') !== -1 || filename.indexOf('&') !== -1) {
return false;
}
if (!/[A-Za-z1-9.]+/g.test(filename)) {
return false;
}
return true;
};
2020-10-01 23:10:41 +03:00
const getFileType = (filename) => {
const extension = path.extname(filename).substring(1);
let type = config.files.embed[extension];
if (!type) {
type = config.files.other;
}
return type;
};
2020-10-05 00:23:56 +03:00
const allowlist = ['https://files.hippoz.xyz/', 'https://hippoz.xyz/', 'https://files.hippoz.xyz', 'https://hippoz.xyz']
2020-10-05 00:15:13 +03:00
const corsOptions = {
origin: function (origin, callback) {
2020-10-05 00:20:28 +03:00
if (allowlist.indexOf(origin) !== -1) {
2020-10-05 00:23:56 +03:00
callback(null, true)
2020-10-05 00:15:13 +03:00
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
app.get('/', (req, res) => {
2020-10-05 00:15:13 +03:00
// res.render('upload');
2020-10-05 00:33:49 +03:00
// TODO: this is only for hippoz. remove the line below and uncomment the line above if you are not hippoz man
2020-10-07 19:10:20 +03:00
res.redirect(301, 'https://hippoz.xyz/');
});
2020-10-01 23:10:41 +03:00
app.get('/file/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(config.storagePath, filename);
const isValid = isPathValid(filename, filePath);
if (!isValid) {
res.status(400).send('invalid input.');
2020-10-01 23:10:41 +03:00
return;
}
fs.access(filePath, fs.F_OK, (err) => {
if (err) {
res.status(404).send('file not found or is invalid.');
2020-10-01 23:10:41 +03:00
return;
}
const type = getFileType(filePath);
const mimeType = mime.lookup(filePath);
if (type === config.files.other) {
res.contentType('text/plain');
} else {
res.contentType(mimeType);
}
res.sendFile(filePath);
});
});
2020-10-05 00:31:23 +03:00
// TODO: add cors for all endpoints
app.post('/api/upload', [ cors(corsOptions) ], (req, res) => {
2020-10-02 22:46:23 +03:00
const password = req.body.password;
const chosenFileName = req.body.filename;
2020-10-02 22:46:23 +03:00
if (config.passwords.indexOf(password) === -1) {
return res.status(401).render('uploadfailed', { message: 'the password you entered is not correct.' });
2020-10-02 22:46:23 +03:00
}
2020-10-01 23:10:41 +03:00
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).render('uploadfailed', { message: 'you forgot to upload a file.' });
2020-10-01 23:10:41 +03:00
}
const file = req.files.file;
if (!isFilenameValid(chosenFileName)) {
return res.status(400).render('uploadfailed', { message: 'invalid name.' });
}
const filepath = path.join(config.storagePath, chosenFileName);
if (!isFilenameValid(file.name) || !isPathValid(chosenFileName, filepath)) {
return res.status(400).render('uploadfailed', { message: 'invalid name.' });
}
2020-10-02 23:03:59 +03:00
2020-10-02 23:08:12 +03:00
fs.stat(filepath, (err) => {
if(err == null) {
return res.status(400).render('uploadfailed', { message: 'a file with that name already exists.' });
2020-10-02 23:08:12 +03:00
} else if(err.code === 'ENOENT') {
2020-10-02 23:03:59 +03:00
file.mv(filepath, (err) => {
if (err) return res.status(500).render('uploadfailed', { message: 'something went wrong while uploading the file.' });
2020-10-18 19:40:30 +03:00
res.render('uploaded', { file: { name: chosenFileName }, baseurl: config.url });
2020-10-02 23:03:59 +03:00
});
} else {
return res.status(500).render('uploadfailed', { message: 'something went wrong.' });;
2020-10-02 23:03:59 +03:00
}
2020-10-01 23:10:41 +03:00
});
});
2020-10-05 00:37:01 +03:00
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('internal server error: something went wrong');
2020-10-05 00:37:01 +03:00
});
app.listen(config.server.port, () => {
console.log(`started server on port ${config.server.port}`);
});