-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_app.js
60 lines (42 loc) · 1.88 KB
/
local_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
56
57
58
59
60
const nivel = process.argv.length > 2 ? process.argv.slice(2)[0].split('=')[1] : 1;
const dotdotslash = '../'.repeat(nivel);
const express = require(dotdotslash + 'midia-server/node_modules/express');
const dir = require(dotdotslash + 'midia-server/node_modules/node-dir');
const cors = require(dotdotslash + 'midia-server/node_modules/cors');
const HandleFile = require(dotdotslash + 'midia-server/handle-file');
const app = express();
const fs = require('fs');
const path = require('path');
const basePath = '';
// 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');