From 35dfc7483d4a29eace34b7720e9f6c766e8f0abb Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 5 Oct 2018 14:56:43 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Refactor=20download=20wrapper=20int?= =?UTF-8?q?o=20async=20func?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cdn/downloadWithCurl.ts | 3 +++ src/cdn/downloadWrapper.ts | 42 +++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/cdn/downloadWithCurl.ts b/src/cdn/downloadWithCurl.ts index 8af60f3..12dabd3 100644 --- a/src/cdn/downloadWithCurl.ts +++ b/src/cdn/downloadWithCurl.ts @@ -9,6 +9,9 @@ export default function downloadWithCurl({ host, path, tempFileName, size, resol const parameters: string[] = [ //... + '--limit-rate', '30m', //maximum speed of 30 MB/s = 240 MBit/s + '--speed-limit', String(100 * 1024 * 1024), //abort if less than 100 MB in 15 seconds + '--speed-time', '15', '--output', tempFileName, url, ]; diff --git a/src/cdn/downloadWrapper.ts b/src/cdn/downloadWrapper.ts index 43c23b9..09774a7 100644 --- a/src/cdn/downloadWrapper.ts +++ b/src/cdn/downloadWrapper.ts @@ -6,27 +6,29 @@ import checkLocalCache from './funcs/checkLocalCache'; import createDirRecursively from './funcs/createDirRecursively'; /** Downloads the given URL and saves it to disk. Returns the location where file is saved under. Throws error if download fails. */ -export default function downloadWrapper({ host, path, size, useCurl = false }: {host: string, path: string, size: number, useCurl: boolean}): Promise { - return new Promise((resolve, reject) => { - //Generate file name we want to save it under - //e.g. on Linux: /tmp/patcher/patch/assets_swtor_main/assets_swtor_main_-1to0/assets_swtor_main_-1to0.zip - const tempFileName = nodePath.join(os.tmpdir(), 'patcher', host, path); +export default async function downloadWrapper({ host, path, size, useCurl = false }: {host: string, path: string, size: number, useCurl: boolean}): string { + //Generate file name we want to save it under + //e.g. on Linux: /tmp/patcher/patch/assets_swtor_main/assets_swtor_main_-1to0/assets_swtor_main_-1to0.zip + const tempFileName = nodePath.join(os.tmpdir(), 'patcher', host, path); - //Create parent directory recursively - const folderName = nodePath.dirname(tempFileName); - createDirRecursively(folderName).then(() => { - //Check if file already exists locally - checkLocalCache(tempFileName, size).then((cacheStatus) => { - if (cacheStatus) { - return resolve(tempFileName); - } + //Create parent directory recursively + const folderName = nodePath.dirname(tempFileName); + await createDirRecursively(folderName); - if (useCurl) { - downloadWithCurl({ host, path, tempFileName, size, resolve, reject }); - } else { - downloadUrlContents(host, path, tempFileName, resolve, reject); - } - }); + //Check if file already exists locally + const cacheStatus = await checkLocalCache(tempFileName, size); + if (cacheStatus) { + return tempFileName; + } + + //Download either via curl or natively with Node + if (useCurl) { + await new Promise((resolve, reject) => { + downloadWithCurl({ host, path, tempFileName, size, resolve, reject }); }); - }); + } else { + await new Promise((resolve, reject) => { + downloadUrlContents(host, path, tempFileName, resolve, reject); + }); + } }