-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (78 loc) · 2.16 KB
/
index.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
#!/usr/bin/env node
const bodyParser = require('body-parser')
const express = require('express')
const mkdirp = require('mkdirp')
function normalizePort(val) {
const port = parseInt(val, 10)
if (Number.isNaN(port)) {
// named pipe
return val
}
if (port >= 0) {
// port number
return port
}
return false
}
const app = express()
const port = normalizePort(process.env.AFL_PORT || '3035')
app.use(bodyParser.json()) // to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true,
}),
)
app.post('/', (req, res) => {
const {chunkName, files} = req.body
res.send(`Start loading ${chunkName}`)
loadFiles({chunkName, files}).catch(console.error)
})
// TODO make chunkName optional
async function loadFiles({chunkName, files}) {
let i = 0
console.log(`total files: ${files.length}`)
await createChunkFolder(chunkName)
for (const file of files) {
await loadFile(file, chunkName, ++i)
}
console.log(`${chunkName} loading finished. ${files.length} files loaded`)
}
async function createChunkFolder(chunkName) {
await mkdirp(chunkName)
}
async function loadFile(link, chunkName, i) {
const path = require('path')
const url = require('url')
const parsedUrl = url.parse(link)
const fileName = parsedUrl.pathname.split('/').pop()
const filePath = chunkName + path.sep + fileName
await loadByLink(link, filePath)
console.log(`File: ${i}. ${fileName} loaded`)
}
function loadByLink(link, path) {
return new Promise((resolve, reject) => {
const https = require('https')
const fs = require('fs')
const file = fs.createWriteStream(path)
https
.get(link, response => {
response.pipe(file)
file.on('finish', () => {
file.close()
})
response.on('end', () => {
resolve()
})
})
.on('error', err => {
// Handle errors
fs.unlink(path) // Delete the file async. (But we don't check the result)
console.error(err.message)
resolve() // go to next file
})
})
}
app.listen(port, () =>
console.log(`Ajax file loader listening on port ${port}!`),
)