-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
218 lines (177 loc) · 5.68 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
import { fileURLToPath } from 'node:url'
import fs from 'node:fs'
import path from 'node:path'
import { fork } from 'node:child_process'
import express from 'express'
import busboy from 'busboy' // multipart/form-data parser
import * as task from './task.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const tmpDir = path.join(process.cwd(), '.tmp')
const createRoot = (id) => `${id}.root`
const getRoot = (id) => path.resolve(tmpDir, createRoot(id))
const app = express()
// UPLOAD
app.post('/upload', (req, res, next) => {
const bb = busboy({ headers: req.headers })
bb.on('error', (err) => {
return res.end(err.message)
})
bb.on('close', () => {
res.redirect(req.triggerUrl)
})
bb.on('file', (name, file, info) => {
req.triggerUrl = `/start/${info.filename}`
const rootDir = `${tmpDir}/${createRoot(info.filename)}`
fs.mkdirSync(rootDir, { recursive: true })
file.pipe(fs.createWriteStream(`${rootDir}/${info.filename}`))
})
req.pipe(bb)
})
// HOMEPAGE
app.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/html')
res.sendFile(path.join(__dirname, '/index.html'))
})
// MIDDLEWARE
function ensureRootDirCreated(req, res, next) {
const id = req.params.id
const rootDir = getRoot(id)
if (!fs.existsSync(rootDir)) {
return res.end(`${id} not valid`)
}
req.rootDir = rootDir
next()
}
// ROUTES
app.get('/status/:id', ensureRootDirCreated, (req, res, next) => {
const { id } = req.params
const rootDir = req.rootDir
res.setHeader('Content-Type', 'text/html; charset=utf-8')
fs.createReadStream(`${rootDir}/${id}.status`)
.on('error', (err) => res.end(`${id} not ready yet`))
.pipe(res)
})
app.get('/json/:id', ensureRootDirCreated, (req, res, next) => {
const { id } = req.params
const rootDir = req.rootDir
res.setHeader('Content-Type', 'application/json')
fs.createReadStream(`${rootDir}/${id}.json`)
.on('error', (err) => res.end(`${id} not ready yet`))
.pipe(res)
})
app.get('/zips/:id', ensureRootDirCreated, (req, res, next) => {
const { id } = req.params
const rootDir = req.rootDir
return fs
.createReadStream(`${rootDir}/${id}.ziplist`)
.on('error', (err) => res.end(`${id} not ready yet`))
.pipe(res)
})
app.get('/customization/:id', ensureRootDirCreated, (req, res, next) => {
const { id } = req.params
const rootDir = req.rootDir
res.setHeader('Content-Type', 'application/json')
return fs
.createReadStream(`${rootDir}/${id}.customization.json`)
.on('error', (err) => res.end(`${id} not ready yet`))
.pipe(res)
})
app.get('/csv/:id', ensureRootDirCreated, (req, res, next) => {
const { id } = req.params
const rootDir = req.rootDir
fs.readdir(`${rootDir}/csv/`, (err, files) => {
if (err) return res.end(`${id} not ready yet`)
const ENDPOINT = `http://amz-tool.jqwerty.com`
const createUrls = (files) => {
const createUrl = (file) => `${ENDPOINT}/csv/${id}/download/${file}`
return files
.sort(() => -1)
.map((file) => `<a href="${createUrl(file)}">${file}</a>`)
.join('\n')
}
const html = `
<div style="display:flex; flex-direction: column; gap: 1rem;">
${createUrls(files)}
</div>
`
return res.send(html)
})
})
app.get('/csv/:id/download/:name', ensureRootDirCreated, (req, res, next) => {
const { id, name } = req.params
const rootDir = req.rootDir
res.setHeader('Content-Type', 'text/csv')
res.setHeader('Content-Disposition', `attachment; filename="${name}"`)
return fs
.createReadStream(`${rootDir}/csv/${name}`)
.on('error', (err) => res.end(`${id} ${name} not found`))
.pipe(res)
})
// COMPOSE STEPS
app.get('/start/:id', ensureRootDirCreated, async (req, res, next) => {
const id = req.params.id
const rootDir = req.rootDir
async function updateStatus(msg) {
await fs.promises.writeFile(`${rootDir}/${id}.status`, msg)
}
try {
updateStatus(`${id} created`).then(() => {
res.redirect(`/status/${id}`)
})
updateStatus(`parsing tsv file`)
await task.parseTSV(id, rootDir)
updateStatus(`downloading zip`)
await task.download(id, rootDir)
updateStatus(`unziping`)
await task.decompress(id, rootDir)
updateStatus(`parsing customize info`)
await task.parseCustomizationData(id, rootDir)
updateStatus(`generating csv`)
await task.generateCSV(id, rootDir)
updateStatus(`[DONE] csv generated: <a href="/csv/${id}">view</a>`)
} catch (err) {
console.log(err)
}
})
// STEPS
app.get('/parse-tsv/:id', ensureRootDirCreated, (req, res, next) => {
const id = req.params.id
const rootDir = req.rootDir
task
.parseTSV(id, rootDir)
.then((msg) => res.end(msg))
.catch((err) => res.end(err))
})
app.get('/download/:id', ensureRootDirCreated, (req, res, next) => {
const id = req.params.id
const rootDir = req.rootDir
task
.download(id, rootDir)
.then((msg) => res.end(msg))
.catch((err) => res.end(err))
})
app.get('/decompress/:id', ensureRootDirCreated, (req, res, next) => {
const id = req.params.id
const rootDir = req.rootDir
task
.decompress(id, rootDir)
.then((msg) => res.end(msg))
.catch((err) => res.end(err))
})
app.get('/parse-customization/:id', ensureRootDirCreated, (req, res, next) => {
const id = req.params.id
const rootDir = req.rootDir
task
.parseCustomizationData(id, rootDir)
.then((msg) => res.end(msg))
.catch((err) => res.end(err))
})
app.get('/generate-csv/:id', ensureRootDirCreated, (req, res, next) => {
const id = req.params.id
const rootDir = req.rootDir
task
.generateCSV(id, rootDir)
.then((msg) => res.end(msg))
.catch((err) => res.end(err))
})
app.listen(3000, () => console.log('running at :3000'))