-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
294 lines (260 loc) · 9.3 KB
/
server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env node
const myConstClass = require("./constants.js")
const fs = require("fs")
const util = require("util")
const path = require("path")
const qrcode = require("qrcode-terminal")
const archiver = require("archiver")
const fileUpload = require("express-fileupload")
const express = require("express")
const app = express()
module.exports = ({ base_path, compression, port }) => {
app.use(fileUpload({
useTempFiles: true,
tempFileDir: "/tmp/"
}))
// If want to limit file size add this:
// app.use(fileUpload({
// createParentPath: true,
// limits: {
// fileSize: 2 * 1024 * 1024 * 1024 //2MB max file(s) size
// },
// }))
app.listen(port, () => {
// get local ip address from https://gist.github.com/sviatco/9054346#gistcomment-1810845
let addresses = new Set()
let ifaces = require("os").networkInterfaces()
for (let dev in ifaces) {
ifaces[dev].filter((details) => {
if (details.family === 'IPv4' && !details.internal) {
addresses.add(details.address)
}
})
}
addresses = Array.from(addresses)
if (addresses.length == 0) {
return
}
for (address of addresses) {
const full_address = "http://" + address + ":" + port
console.log("Server listening on port: ", port)
console.log("Scan QR code or go to " + full_address)
qrcode.generate(full_address)
console.log("\n")
}
})
const readdir = util.promisify(fs.readdir)
// console.log("base_path: " + base_path)
const base_dir = base_path || process.cwd()
app.get("/api/files", async (req, res) => {
let dir
// console.log("/api/files req.query: ", req.query)
if (req.query.path && req.query.path !== "undefined") {
dir = path.join(base_dir, req.query.path)
} else {
// console.log("base_dir: ", base_dir)
dir = base_dir
}
if (!checkPathOK(dir)) {
console.error("path attempting to go back. dir: ", dir)
res.status(403).send("Can't go back up path")
}
// console.log("dir: ", dir)
listDir(dir)
.then((results) => {
res.send(results)
// console.log("success")
})
.catch((err) => {
console.error("error browsing files", err)
res.sendStatus(501)
})
})
async function listDir(dir) {
let jsonResult = {}
// console.log("listDir")
let rel_dir = ""
let files
try {
files = await readdir(dir)
} catch (err) {
console.error(err)
}
files = files.filter((item) => !/(^|\/)\.[^\/\.]/g.test(item))
files.forEach((file) => {
jsonResult[file] = path.join(rel_dir, file)
let is_dir = false
// Is directory?
let file_path = path.join(dir, file)
// console.log("isDirectory file_path: ", file_path)
if (
fs.existsSync(file_path) &&
fs.lstatSync(file_path).isDirectory()
) {
is_dir = true
// console.log("isDirectory file: ", file)
}
jsonResult[file] = is_dir
})
// console.log("jsonResult: ", jsonResult)
return jsonResult
}
app.get("/api/download", async (req, res) => {
// console.log("/api/download")
const file = req.query.file
downloadFile(file, res)
.then(() => {
// res.send(results)
// console.log("success")
})
.catch((err) => {
console.error("error", err)
res.sendStatus(501)
})
})
async function downloadFile(file, res) {
let file_path = path.join(base_dir, file)
if (fs.existsSync(file_path) && fs.lstatSync(file_path).isDirectory()) {
is_dir = true
// console.log("isDirectory file: ", file)
}
res.download(file_path)
}
app.get("/api/downloadDir", async (req, res) => {
const dir = req.query.dir
if (!checkPathOK(dir)) {
console.error("path attempting to go back. dir: ", dir)
res.status(403).send("Can't go back up path")
}
// console.log("downloadDir. dir: ", dir)
await zipDir(base_dir, dir, res)
.then((output_path) => {
// res.send(results)
// console.log("success downloadDir")
})
.catch(err => {
console.error("error", err)
res.sendStatus(501)
})
})
async function zipDir(rel_directory, dir, res) {
// console.log("rel_directory: ", rel_directory)
// console.log("dir: ", dir)
let archive = archiver("zip", {
zlib: { level: compression }, // Sets the compression level.
})
archive.on("warning", function (err) {
if (err.code === "ENOENT") {
console.error("Zipping error. ENOENT.")
} else {
// throw error
throw err
}
})
archive.on("error", function (err) {
throw err
})
res.writeHead(200, {
"Content-Type": "application/zip",
"Content-Disposition": "attachment filename=" + dir + ".zip",
})
// pipe archive data to the result
archive.pipe(res)
archive.directory(path.join(rel_directory, dir), dir)
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize()
// console.log("finalized archive")
}
app.post("/api/upload", async (req, res) => {
// console.log("/api/upload.")
uploadFile(req, res)
.then(() => {
// res.send(results)
// console.log("upload success")
})
.catch((err) => {
console.error("upload error", err)
res.sendStatus(501)
})
})
async function moveFile(file, rel_dir) {
//move photo to uploads directory
let new_name = file.name
let dir = path.join(base_dir, rel_dir)
// console.log("dir: ", dir)
try {
fs.accessSync(path.join(dir, new_name))
let name = new_name.substring(0, new_name.lastIndexOf("."))
let file_extension = new_name.substring(new_name.lastIndexOf("."))
new_name = await nonExistingName(name, file_extension, 0, dir)
} catch (err) {
// console.log("moveFile. fs.accessSync(new_name)")
}
let full_path = path.join(dir, new_name)
file.mv(full_path)
//push file details
return {
name: file.name,
mimetype: file.mimetype,
size: file.size,
}
}
function checkPathOK(path_check) {
return !path_check.includes("..")
}
async function nonExistingName(name, file_extension, count, dir) {
// console.log("nonExistingName: name, file_extension: ", name, file_extension)
try {
fs.accessSync(path.join(dir, name) + "_" + count + file_extension)
return await nonExistingName(name, file_extension, count + 1, dir)
} catch (err) {
// file does not exist.//
}
return name + "_" + count + file_extension
}
async function uploadFile(req, res) {
// console.log("uploadFile req.files: ", req.files)
try {
if (!req.files) {
// console.log("!req.files")
res.send({
status: false,
message: "No file uploaded",
})
} else {
let data = new Array()
let message
const rel_dir = req.body.rel_dir
// console.log("rel_dir: ", rel_dir)
// Check if single file or group of files
if (req.files.file && !Array.isArray(req.files.file)) {
let file = req.files.file
// console.log("single file")
data = moveFile(file, rel_dir)
message = "File is uploaded"
} else {
for (let i = 0; i < req.files.file.length; i++) {
let file = req.files.file[i]
// console.log("inside before for each. : ")
data.push(moveFile(file, rel_dir))
message = "Files are uploaded"
}
}
//send response
res.send({
status: true,
message: message,
data: data,
})
}
} catch (err) {
res.status(500).send(err)
}
}
app.get("/api/baseDir", async (req, res) => {
// console.log("/api/baseDir: ", base_dir)
res.send(base_dir)
})
app.use(express.static(path.join(__dirname, "client/build")))
}