forked from ShawonAshraf/LocalFileServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-listing.js
46 lines (40 loc) · 1.12 KB
/
file-listing.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
import fs from "fs"
export const gatherFiles = (path) => {
return new Promise((resolve, reject) => {
fs.readdir(path, (err, files) => {
let fileList = []
if (err) {
reject(err)
}
// add complete path
for (let i = 0; i < files.length; i++) {
let fileObj = {
fileName: `${path}/${files[i]}`
}
fileList.push(fileObj)
}
// resolve
resolve(fileList)
})
})
}
// lists file urls in hbs view table
export const hbsTableListing = (files) => {
let out = ""
if (files.length === 0) {
out = "Server has no files at the moment!"
return out
}
files.forEach(file => {
let fileInfo = file["fileName"].split("/")
let path = fileInfo[0]
let name = fileInfo[1]
out += `<tr class="row">
<td class="col">${name}</td>
<td class="col">
<a class="btn btn-success" href="${path}/${name}">Download</a>
</td>
</tr>`
})
return out
}