-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlooksie.js
70 lines (53 loc) · 1.89 KB
/
looksie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import express from 'express';
import expressUpload from 'express-fileupload';
import expressAuth from 'express-basic-auth';
import yaml from 'yaml';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { exit } from 'process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const __config = path.join(__dirname, 'private', 'looksie.yml');
if (!fs.existsSync(__config)) {
console.error(`No config: ${__config}`);
exit(1);
}
//? get config and express
const cd = yaml.parse(fs.readFileSync(__config, 'utf8'));
const looksie = express();
const port = process.env.PORT || cd.app.port || 3169;
//? setup static content
looksie.use(express.static(path.join(__dirname, cd.app.path.static)));
//? setup express-fileupload
looksie.use(
expressUpload({
limits: {
fileSize: (cd.app.fileSizeLimit * (1024 * 1024)).toFixed(0),
parts: 1
},
abortOnLimit: true,
})
);
//? setup routes
looksie.get(cd.app.route.paste, expressAuth({ users: cd.auth.users, challenge: true, realm: cd.auth.realm }), (req, res) => {
const host = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
console.log(cd.ui.authed + `host(${host})`);
res.sendFile(path.join(__dirname, cd.app.path.paste));
});
looksie.post(cd.app.route.upload, expressAuth({ users: cd.auth.users, challenge: true, realm: cd.auth.realm }), (req, res) => {
if (cd.app.debug) {
console.log(req.files);
}
if (cd.app.requestFile in req.files) {
const { image } = req.files;
console.log(cd.ui.new + `md5(${image.md5}) mime(${image.mimetype}) size(${(image.size / (1024 * 1024)).toFixed(cd.app.fixedLimit)}MB)`);
image.mv(path.join(__dirname, cd.app.path.upload));
res.sendStatus(200);
}
else {
res.sendStatus(400);
}
});
//? start express
looksie.listen(port, () => { console.log(cd.ui.startup + `port(${port})`); });