-
Notifications
You must be signed in to change notification settings - Fork 4
/
directories.js
43 lines (33 loc) · 1.02 KB
/
directories.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
const fs = require('fs')
const path = require('path')
function _get (req, res, next) {
const relativePath = req.query.path || ""
const absolutePath = path.resolve('/home/' + req.user.username + '/' + relativePath)
const regex = new RegExp('^\/home\/' + req.user.username)
if (!absolutePath.match(regex)) {
const err = new Error('Unauthorized access')
err.status = 403
next(err);
}
fs.readdir(absolutePath, (err, list) => {
if (err) {
err.status = 404
return next(err)
}
list.forEach((item, i) => {
_getItemProperties({ path: absolutePath, item: item }, result => list[i] = result)
})
res.send({ path: relativePath, items: list })
})
}
function _getItemProperties (local, callback) {
const itemPath = local.path + '/' + local.item
let itemProperties = { name: local.item, isFile: fs.statSync(itemPath).isFile() }
if (itemProperties.isFile) {
itemProperties.extension = path.extname(local.item)
}
callback(itemProperties)
}
module.exports = {
get: _get
}