You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ass/ass.js

110 lines
3.5 KiB

try {
// Check if config.json exists
require('./config.json');
} catch (err) {
console.error('No config.json found! Please run \'npm run setup\'');
process.exit(1);
}
// Load the config
const { host, port, domain, useSsl, resourceIdSize, resourceIdType } = require('./config.json');
//#region Imports
3 years ago
const fs = require('fs-extra');
const uuid = require('uuid').v4;
const express = require('express');
3 years ago
const useragent = require('express-useragent');
3 years ago
const multer = require('multer');
const zws = require('./idgen/zws');
const { path, saveData, log, verify, generateId } = require('./utils');
//#endregion
3 years ago
//#region Variables, module setup
3 years ago
const app = express();
const upload = multer({ dest: 'uploads/' });
var tokens = [];
var data = {};
//#endregion
3 years ago
preStartup();
startup();
function preStartup() {
// Make sure data.json exists
if (!fs.existsSync(path('data.json'))) {
fs.writeJsonSync(path('data.json'), data, { spaces: 4 });
log('File [data.json] created');
} else log('File [data.json] exists');
3 years ago
// Make sure auth.json exists and generate the first key
if (!fs.existsSync(path('auth.json'))) {
3 years ago
tokens.push(uuid().replace(/-/g, ''));
fs.writeJsonSync(path('auth.json'), { tokens }, { spaces: 4 });
log(`File [auth.json] created\n!! Important: save this token in a secure spot: ${tokens[0]}\n`);
} else log('File [auth.json] exists');
3 years ago
// Read tokens and data
tokens = fs.readJsonSync(path('auth.json')).tokens;
data = fs.readJsonSync(path('data.json'));
log('Tokens & data read from filesystem');
3 years ago
}
function startup() {
3 years ago
app.use(useragent.express());
// Upload file
app.post('/', upload.single('file'), (req, res) => {
if (!verify(req, tokens)) return res.sendStatus(401);
log(`Uploaded: ${req.file.originalname} (${req.file.mimetype})`);
3 years ago
// Save the file information
let resourceId = generateId(resourceIdType, resourceIdSize, req.file.originalname);
data[resourceId.split('.')[0]] = req.file;
saveData(data);
3 years ago
let http = ('http').concat(useSsl ? 's' : '').concat('://');
let trueDomain = domain.concat((port != 80 || port != 443) ? `:${port}` : '');
res.type('json').send({
resource: `${http}${trueDomain}/${resourceId}`,
delete: `${http}${trueDomain}/delete/${req.file.filename}`
});
3 years ago
});
// View file
3 years ago
app.get('/:resourceId', (req, res) => {
// Don't process favicon requests
if (req.url.includes('favicon.ico')) return;
let resourceId = req.params.resourceId.split('.')[0];
3 years ago
let fileData = fs.readFileSync(path(data[resourceId].path));
if (!resourceId || !data[resourceId]) return res.sendStatus(404);
fs.readFile(path(data[resourceId].path))
.then((fileData) => res
.header('Accept-Ranges', 'bytes')
.header('Content-Length', fileData.byteLength)
.type(data[resourceId].mimetype).send(fileData))
.catch(console.error);
3 years ago
});
// Delete file
app.get('/delete/:filename', (req, res) => {
let filename = req.params.filename;
let resourceId = Object.keys(data)[Object.values(data).indexOf(Object.values(data).find((d) => d.filename == filename))];
if (!resourceId || !data[resourceId]) return res.sendStatus(400);
log(`Deleted: ${data[resourceId].originalname} (${data[resourceId].mimetype})`);
// Save the file information
fs.rmSync(path(data[resourceId].path));
delete data[resourceId];
saveData(data);
res.type('text').send('File has been deleted!');
})
app.listen(port, host, () => log(`Server started on [${host}:${port}]\nAuthorized tokens: ${tokens.length}\nAvailable files: ${Object.keys(data).length}`));
}