-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.js
67 lines (63 loc) · 2.37 KB
/
http.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
const { app } = require('electron')
const express = require('express')
const { networkInterfaces } = require('os')
const path = require('path')
class http {
get ipaddr() {
return this.app.locals.ip
}
get port() {
return this.app.locals.port
}
constructor(filename,code,name) {
this.app = express()
this.app.locals.filename = filename
this.app.locals.code = code
this.app.locals.name = name
this.app.get('/:filename', function(req,res) {
if (req.params.filename != path.basename(req.app.locals.filename)) {
res.sendStatus(404)
} else {
res.setHeader('Content-Disposition',`attachment; filename=${encodeURIComponent(path.basename(req.app.locals.filename))}`)
.sendFile(filename,()=>{})
}
})
this.app.get('/', function(req,res) {
res.contentType('application/json')
.send(JSON.stringify(
{
"id": code,
"name": name,
"type": "module",
"version": app.getVersion(),
"description": `EncounterLog Export of ${name}`,
"download": `http://${req.app.locals.ip}:${req.app.locals.port}/${path.basename(req.app.locals.filename)}`
}
)
)
})
this.server = new Promise((resolve,reject)=>{
try{
let server = this.app.listen(0,'::',()=>{
console.log("Server started.")
const nets = networkInterfaces()
for (const net of Object.keys(nets)) {
for(const ip of nets[net]) {
if (ip.family == 'IPv4' && !ip.internal) {
this.app.locals.ip = ip.address
break;
}
}
if (this.app.locals.ip) break;
}
console.log(server.address())
this.app.locals.port = server.address()?.port
resolve(server)
})
} catch(e) {
reject(e)
}
})
}
}
module.exports = http;