-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvoice.js
132 lines (123 loc) · 3.49 KB
/
voice.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
const express = require('express')
const fs = require('fs')
const multer = require('multer')
const util = require('./util.js')
require('dotenv').config()
const SECPATH = process.env.SECPATH || '/uploaded/'
const PORT = process.env.PORT || 8005
const app = express()
const getIP = function(req) {
if (req.headers['x-forwarded-for']) {
return req.headers['x-forwarded-for']
}
if (req.connection && req.connection.remoteAddress) {
return req.connection.remoteAddress
}
if (req.connection.socket && req.connection.socket.remoteAddress) {
return req.connection.socket.remoteAddress
}
if (req.socket && req.socket.remoteAddress) {
return req.socket.remoteAddress
}
return '0.0.0.0'
}
const encodeIP = function(ip) {
return ip.replace(/\.|:/g, '_')
}
const fnid = 'data/id.txt'
const getID = function() {
let id = 0
try {
id = parseInt(fs.readFileSync(fnid, 'utf-8'))
} catch (e) {
}
id++
fs.writeFileSync(fnid, id, 'utf-8')
return id
}
app.post('/', multer({ dest: 'tmp/' }).single('file'), (req, res) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Content-Type', 'application/json; charset=utf-8')
try {
const data = fs.readFileSync(req.file.path)
//console.log(req.file.path)
//console.log(req)
let fn = req.body.filename
fn = fn.replace(/\.\./g, "__")
//const fn = 'data/' + id + "-" + util.getYMDHMSM() + "-" + encodeIP(getIP(req)) + ".wav"
util.writeFileSync('data/' + fn, data)
console.log('uploaded: ' + 'data/' + fn)
const id = getID()
res.send(JSON.stringify({ res: 'ok', id: id }))
fs.appendFileSync('data/log.txt', util.getYMDHMSM() + "," + id + "," + getIP(req) + "," + fn + "\n", "utf-8")
} catch (e) {
res.send(JSON.stringify({ res: 'err' }))
}
})
app.get(SECPATH + '*', (req, res) => {
let fn = req.url.substring(SECPATH.length)
const qn = fn.lastIndexOf('?')
if (qn >= 0) {
fn = fn.substring(0, qn)
}
console.log(req.url, fn)
if (fn.indexOf('..') >= 0) {
res.header('Content-Type', 'text/html; charset=utf-8')
res.send('err')
return
}
if (fn.length == 0) {
const s = []
s.push(`<h2>COVID-19</h2>`)
try {
const list = fs.readdirSync('data/')
console.log(list)
for (const f of list) {
if (f.endsWith('.wav'))
s.push(`<a href=${f}>${f}</a>`)
}
} catch (e) {
}
s.push('')
res.header('Content-Type', 'text/html; charset=utf-8')
res.send(s.join('<br>'))
} else {
res.header('Content-Type', 'audio/wav')
const data = fs.readFileSync('data/' + fn)
res.send(data)
}
})
app.get('/*', (req, res) => {
let url = req.url
if (url == '/' || url.indexOf('..') >= 0) {
url = '/index.html'
}
let ctype = 'text/plain'
if (url.endsWith('.html')) {
ctype = 'text/html; charset=utf-8'
} else if (url.endsWith('.js')) {
ctype = 'application/javascript'
} else if (url.endsWith('.mjs')) {
ctype = 'application/javascript'
} else if (url.endsWith('.css')) {
ctype = 'text/css'
}
let data = null
try {
data = fs.readFileSync('static' + url)
} catch (e) {
}
res.header('Content-Type', ctype)
res.send(data)
})
app.listen(PORT, () => {
console.log(`to access the top`)
console.log(`http://localhost:${PORT}/`)
console.log()
console.log(`to download voice files uploaded`)
console.log(`http://localhost:${PORT}${SECPATH}`)
console.log()
console.log(`edit .env if you want to change`)
console.log()
console.log('https://github.com/code4sabae/coughgathering/')
})