✨ Finish curl download implementation
This commit is contained in:
parent
c2cccc438c
commit
24515e41a3
2 changed files with 37 additions and 16 deletions
|
@ -4,11 +4,13 @@ import * as childProcess from 'child_process';
|
|||
* Downloads a file using a curl child process, to allow for speed-limiting and timeout if download is too slow.
|
||||
* Takes as input the host domain name, the path and the file size
|
||||
*/
|
||||
export default function downloadWithCurl({ host, path, tempFileName, size, resolve, reject }: {host: string, path: string, tempFileName: string, size: number, resolve: (downloadedFile: string) => void, reject: () => void}): void {
|
||||
export default function downloadWithCurl({ host, path, tempFileName }: {host: string, path: string, tempFileName: string}): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = `http://${host}${(path.substr(0, 1) === '/' ? '' : '/')}${path}`;
|
||||
|
||||
const parameters: string[] = [
|
||||
//...
|
||||
'--silent',
|
||||
'--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',
|
||||
|
@ -18,5 +20,17 @@ export default function downloadWithCurl({ host, path, tempFileName, size, resol
|
|||
|
||||
const spawnedProcess = childProcess.spawn('curl', parameters);
|
||||
|
||||
spawnedProcess.stderr.setEncoding('utf8');
|
||||
spawnedProcess.stderr.on('data', (error) => {
|
||||
reject(`Error in process:\n> curl ${parameters.join(' ')}\n${error}`);
|
||||
});
|
||||
|
||||
spawnedProcess.on('exit', (code) => {
|
||||
if (code === 0) {
|
||||
resolve(tempFileName);
|
||||
} else {
|
||||
reject(`Error in process:\n> curl ${parameters.join(' ')}\nNon-zero exit code ${code}.`);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -23,10 +23,17 @@ export default async function downloadWrapper({ host, path, size, useCurl = fals
|
|||
|
||||
//Download either via curl or natively with Node
|
||||
if (useCurl) {
|
||||
const downloadResult = await new Promise((resolve, reject) => {
|
||||
downloadWithCurl({ host, path, tempFileName, size, resolve, reject });
|
||||
}) as string;
|
||||
//Try up to three times
|
||||
for (let i = 0; i < 3; i += 3) {
|
||||
try {
|
||||
const downloadResult = await downloadWithCurl({ host, path, tempFileName });
|
||||
return downloadResult;
|
||||
} catch {
|
||||
//ignore error and try again
|
||||
}
|
||||
}
|
||||
//Download failed, throw error
|
||||
throw new Error('Could not download with curl');
|
||||
} else {
|
||||
const downloadResult = await new Promise((resolve, reject) => {
|
||||
downloadUrlContents(host, path, tempFileName, resolve, reject);
|
||||
|
|
Loading…
Reference in a new issue