2021-11-02 00:01:11 +01:00
|
|
|
import fs from 'fs'
|
|
|
|
import https from 'https'
|
|
|
|
import initLog from '../utils/log.js'
|
|
|
|
import { exec } from 'child_process'
|
|
|
|
|
|
|
|
const log = initLog("Downloader")
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function can download and save data to files, or simply return the data.
|
|
|
|
* @param {string} url The URL of which to download from...
|
|
|
|
* @param {string} target This is the file path you want to save to. Alterntively use "_RETURN_VALUE_" to get value returned instead of saved to a file.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*
|
|
|
|
* @author BurnyLlama
|
|
|
|
*/
|
|
|
|
export function download(url, target) {
|
|
|
|
return new Promise(
|
|
|
|
(resolve, reject) => {
|
|
|
|
log(`Starting a download >> ${url}`)
|
|
|
|
|
|
|
|
https.get(
|
|
|
|
url,
|
|
|
|
data => {
|
|
|
|
if (target === "_RETURN_VALUE_") {
|
|
|
|
let result
|
|
|
|
|
|
|
|
data.on(
|
|
|
|
'data',
|
|
|
|
chunk => result += chunk
|
|
|
|
)
|
|
|
|
|
|
|
|
data.on(
|
|
|
|
'end',
|
|
|
|
() => resolve(result)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = fs.createWriteStream(target)
|
|
|
|
|
|
|
|
data.pipe(file)
|
|
|
|
|
|
|
|
data.on(
|
|
|
|
'end',
|
|
|
|
() => {
|
|
|
|
log(`Done with a download >> ${url}`)
|
|
|
|
file.close()
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function downloadEssentialData() {
|
|
|
|
return new Promise(
|
|
|
|
(resolve, reject) => {
|
|
|
|
log("Downloading 'ddnet.sqlite.zip' and 'players.msgpack'...")
|
|
|
|
Promise.all([
|
|
|
|
download("https://ddnet.tw/stats/ddnet.sqlite.zip", process.env.DDNET_SQLITE_PATH ? `${process.env.DDNET_SQLITE_PATH}.zip` : 'data/ddnet.sqlite.zip'),
|
|
|
|
download("https://ddnet.tw/players.msgpack", process.env.MSGPACK_PATH ?? 'data/players.msgpack')
|
|
|
|
]).then(() => {
|
|
|
|
log("All downloads done! Going to unzip 'ddnet.sqlite.zip'...")
|
|
|
|
|
|
|
|
exec(
|
|
|
|
`unzip -o ${process.env.DDNET_SQLITE_PATH ? `${process.env.DDNET_SQLITE_PATH}.zip` : 'data/ddnet.sqlite.zip'} \
|
2021-11-02 09:58:10 +01:00
|
|
|
-d ${process.env.DDNET_SQLITE_PATH ? process.env.DDNET_SQLITE_PATH.replace(/\/[\s\S]*\.sqlite/, "") : 'data'}`,
|
2021-11-02 00:01:11 +01:00
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
log("Error while unzipping!")
|
|
|
|
reject()
|
|
|
|
}
|
|
|
|
|
|
|
|
log("Done unzipping!")
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|