-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (40 loc) · 1.61 KB
/
app.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
const express = require('express'),
dir = require('node-dir'),
HandleFile = require('./handle-file'),
cors = require('cors'),
app = express(),
basePath = '../../';
const fs = require('fs');
const path = require('path');
// CORS ALL DOMAIN
app.use(cors());
/***************************************/
app.get('/getdir/:dir*', (req, res) => {
const handleFile = new HandleFile(req.query);
const directory = basePath + req.params.dir + req.params['0'],
replace = directory.split('/').slice(0, -1).join('/');
console.log(`getdir: ${directory}`);
dir.files(directory, 'dir', (err, files) =>
res.send(files ? handleFile.dealPaths(files, replace) : err));
});
/***************************************/
app.get('/getfile/:dir*', (req, res) => {
const handleFile = new HandleFile(req.query);
const directory = basePath + req.params.dir + req.params['0'];
const bufdir = Buffer.from(directory);
const list = fs.readdirSync(directory, { encoding: 'buffer' });
const files = [];
console.log(`getfile: ${directory}`);
for (let i = 0, l = list.length; i < l; i++) {
const fname = list[i].toString();
const buffile = Buffer.concat([bufdir, Buffer.from(path.sep), list[i]]);
const info = fs.statSync(buffile);
if (!info.isDirectory() && handleFile.checkExt(path.extname(fname)))
files.push({ fname, ...info });
}
files.sort((a, b) => b.birthtime - a.birthtime);
res.send(files.map((item) => item.fname));
});
/***************************************/
app.listen(3000);
console.log('server started at port 3000');