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) ) } else { const file = fs.createWriteStream(target) data.pipe(file) } data.on( 'end', () => { log(`Done with a download >> ${url}`) 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([ 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( `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() } ) }) } ) }