forked from indilo53/lightshot-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (42 loc) · 1.37 KB
/
index.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
const fs = require('fs');
const http = require("http");
const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();
const { uid } = require('uid');
const config = require('./config.json');
app.use(fileUpload());
app.use('/i', express.static(config.uploadDir))
app.post('/upload/:a/:b/', function(req, res) {
if (!req.files)
return res.status(500).send('No file uploaded');
let newFileName;
for(let k in req.files) {
((file) => {
if(file.name.indexOf('thumb') == -1) {
const split = file.name.split('.');
const ext = split.pop();
fileName = uid(8) + '.' + ext;
let count = 1;
while(fs.existsSync(config.uploadDir + '/' + fileName))
fileName = `${uid(8)}.${ext})`;
file.mv(config.uploadDir + '/' + fileName, function(err) {
if (err)
return res.status(500).send(err);
});
newFileName = fileName;
}
})(req.files[k]);
}
const url = config.baseUrl.replace('{{filename}}', newFileName);
const body = `<response><status>success</status><share>${url}</share></response>
`
res.send(body);
});
app.g
http
.createServer(app)
.listen(config.listenPort, () => {
console.log('lightshot http server started on port ' + config.listenPort);
})
;