♻ Extract file download from preparation steps
This commit is contained in:
parent
facf0d12c9
commit
6394de57f4
5 changed files with 74 additions and 38 deletions
|
@ -1,26 +1,14 @@
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import * as os from 'os';
|
|
||||||
import * as nodePath from 'path';
|
|
||||||
import checkLocalCache from './funcs/checkLocalCache';
|
|
||||||
import createDirRecursively from './funcs/createDirRecursively';
|
|
||||||
import saveResponse from './funcs/saveResponse';
|
import saveResponse from './funcs/saveResponse';
|
||||||
|
|
||||||
/** Downloads the given URL and saves it to disk. Throws error if download fails. */
|
/** 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 downloadUrlContents({ host, path, size }: {host: string, path: string, size: number}): Promise<string> {
|
export default function downloadUrlContents(
|
||||||
return new Promise((resolve, reject) => {
|
host: string,
|
||||||
//Generate file name we want to save it under
|
path: string,
|
||||||
//e.g. on Linux: /tmp/patcher/patch/assets_swtor_main/assets_swtor_main_-1to0/assets_swtor_main_-1to0.zip
|
tempFileName: string,
|
||||||
const tempFileName = nodePath.join(os.tmpdir(), 'patcher', host, path);
|
resolve: (fileName: string) => void,
|
||||||
|
reject: (reason: Error | string) => void,
|
||||||
//Create parent directory recursively
|
): void {
|
||||||
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 HTTP request
|
//Create HTTP request
|
||||||
const request = http.request({
|
const request = http.request({
|
||||||
family: 4,
|
family: 4,
|
||||||
|
@ -35,7 +23,4 @@ export default function downloadUrlContents({ host, path, size }: {host: string,
|
||||||
});
|
});
|
||||||
|
|
||||||
request.end();
|
request.end();
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
19
src/cdn/downloadWithCurl.ts
Normal file
19
src/cdn/downloadWithCurl.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
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 {
|
||||||
|
const url = `http://${host}${(path.substr(0, 1) === '/' ? '' : '/')}${path}`;
|
||||||
|
|
||||||
|
const parameters: string[] = [
|
||||||
|
//...
|
||||||
|
'--output', tempFileName,
|
||||||
|
url,
|
||||||
|
];
|
||||||
|
|
||||||
|
const spawnedProcess = childProcess.spawn('curl', parameters);
|
||||||
|
|
||||||
|
resolve(tempFileName);
|
||||||
|
}
|
32
src/cdn/downloadWrapper.ts
Normal file
32
src/cdn/downloadWrapper.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import * as os from 'os';
|
||||||
|
import * as nodePath from 'path';
|
||||||
|
import downloadUrlContents from './downloadUrlContents';
|
||||||
|
import downloadWithCurl from './downloadWithCurl';
|
||||||
|
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<string> {
|
||||||
|
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);
|
||||||
|
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (useCurl) {
|
||||||
|
downloadWithCurl({ host, path, tempFileName, size, resolve, reject });
|
||||||
|
} else {
|
||||||
|
downloadUrlContents(host, path, tempFileName, resolve, reject);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import downloadUrlContents from '../cdn/downloadUrlContents';
|
import downloadWrapper from '../cdn/downloadWrapper';
|
||||||
import createDirRecursively from '../cdn/funcs/createDirRecursively';
|
import createDirRecursively from '../cdn/funcs/createDirRecursively';
|
||||||
import getUrlContents from '../cdn/getUrlContents';
|
import getUrlContents from '../cdn/getUrlContents';
|
||||||
import { Product } from '../interfaces/ISettings';
|
import { Product } from '../interfaces/ISettings';
|
||||||
|
@ -62,7 +62,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
|
||||||
//start download, making sure that .zip file downloads first
|
//start download, making sure that .zip file downloads first
|
||||||
const indexOfLastFile = solidpkg.files.length - 1;
|
const indexOfLastFile = solidpkg.files.length - 1;
|
||||||
const zipFile = getUrlContents(createUrlObject(solidpkg.files[indexOfLastFile]));
|
const zipFile = getUrlContents(createUrlObject(solidpkg.files[indexOfLastFile]));
|
||||||
const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadUrlContents(createUrlObject(file)));
|
const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadWrapper({ ...createUrlObject(file), useCurl: false }));
|
||||||
|
|
||||||
//we can parse the file entries as soon as the .zip file is downloaded
|
//we can parse the file entries as soon as the .zip file is downloaded
|
||||||
const fileEntries = readSsnFile(await zipFile);
|
const fileEntries = readSsnFile(await zipFile);
|
||||||
|
|
|
@ -28,7 +28,7 @@ export default function launchProcess(
|
||||||
parameters.push('--target', output);
|
parameters.push('--target', output);
|
||||||
}
|
}
|
||||||
|
|
||||||
const spawnedProcess = childProcess.spawn(processPath, parameters.map((value) => value.toString(), { cwd: '.' }));
|
const spawnedProcess = childProcess.spawn(processPath, parameters.map((value) => value.toString()));
|
||||||
|
|
||||||
if (typeof output === 'string') {
|
if (typeof output === 'string') {
|
||||||
spawnedProcess.stdout.setEncoding('utf8');
|
spawnedProcess.stdout.setEncoding('utf8');
|
||||||
|
|
Loading…
Reference in a new issue