-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnvr-browser.js
78 lines (64 loc) · 2.12 KB
/
nvr-browser.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
71
72
73
74
75
76
77
78
const path = require('path');
const fsAsync = require('fs').promises;
const express = require('express');
const app = express();
const storage = require('./storage.json');
const port = 3000;
// set view engine
app.set('view engine', 'ejs');
app.set('views', 'views');
app.use(require('./middleware/middleware.basic-auth'));
// initialise the video-serving code
app.use(require('./video-file-server'));
app.use(express.static('public'));
app.get('*.:ext', async (req, res, next) => {
const filetype = req.params.ext;
const route = `${req.params['0']}.${filetype}`.split('/').filter(x => x.length > 0);
const breadcrumbs = [];
const currentParts = [];
for (let i = 0; i < route.length; i++) {
const folder = route[i];
breadcrumbs.push({
name: folder,
route: '/' + [...currentParts, folder].join('/')
})
currentParts.push(folder);
}
const filename = route[route.length - 1];
res.render('video', {
pageTitle: `${filename}`,
videoUrl: `/api/${req.params['0']}.${filetype}`,
route: breadcrumbs
})
})
app.get('*', async (req, res, next) => {
const route = req.params['0'].split('/').filter(x => x.length > 0);
const breadcrumbs = [];
const currentParts = [];
for (let i = 0; i < route.length; i++) {
const folder = route[i];
breadcrumbs.push({
name: folder,
route: '/' + [...currentParts, folder].join('/')
})
currentParts.push(folder);
}
const directory = path.join(storage.rootpath, ...route);
const folderItems = (await fsAsync.readdir(directory, { withFileTypes: true })).map(dirent => dirent.name);
const locations = [];
for (let i = 0; i < folderItems.length; i++) {
const folderItem = folderItems[i];
locations.push({
name: folderItem,
route: `/${[...route, folderItem].join('/')}`
})
}
res.render('folder', {
pageTitle: 'Cameras',
route: breadcrumbs,
locations: locations
})
})
app.listen(port, () => {
console.log(`*** Server listening on port ${port} ***`);
})