ddstats-server/libs/download/dowload.js

86 lines
2.9 KiB
JavaScript
Raw Normal View History

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_") {
2021-11-06 21:59:49 +01:00
let result = ""
data.on(
'data',
chunk => result += chunk
)
2021-11-06 21:59:49 +01:00
data.on(
'end',
() => resolve(result)
)
}
2021-11-06 21:59:49 +01:00
else {
const file = fs.createWriteStream(target)
data.pipe(file)
}
data.on(
'end',
() => {
log(`Done with a download >> ${url}`)
2021-11-06 21:59:49 +01:00
if (!target === "_RETURN_VALUE_")
file.close()
resolve()
}
)
}
)
}
)
}
export function downloadEssentialData() {
return new Promise(
(resolve, reject) => {
log("Downloading 'ddnet.sqlite.zip' and 'players.msgpack'...")
Promise.all([
2021-11-04 23:12:51 +01:00
download("https://ddnet.tw/stats/ddnet.sqlite.zip", process.env.DATA_DIR ? `${process.env.DATA_DIR}/ddnet.sqlite.zip` : 'data/ddnet.sqlite.zip'),
download("https://ddnet.tw/players.msgpack", process.env.DATA_DIR ? `${process.env.DATA_DIR}/players.msgpack` : 'data/players.msgpack')
]).then(() => {
log("All downloads done! Going to unzip 'ddnet.sqlite.zip'...")
exec(
2021-11-04 23:12:51 +01:00
`unzip -o ${process.env.DATA_DIR ? `${process.env.DATA_DIR}/ddnet.sqlite.zip` : 'data/ddnet.sqlite.zip'} \
-d ${process.env.DATA_DIR ? process.env.DATA_DIR : 'data'}`,
err => {
if (err) {
log("Error while unzipping!")
reject()
}
log("Done unzipping!")
resolve()
}
)
})
}
)
}