MCPcopy Create free account
hub / github.com/Shazbot/WH3-Mod-Manager / downloadFile

Function downloadFile

src/index.ts:298–319  ·  view source on GitHub ↗
(url: string, dest: string, fileOut?: fs.WriteStream)

Source from the content-addressed store, hash-verified

296
297 // Download the update file
298 const downloadFile = (url: string, dest: string, fileOut?: fs.WriteStream): Promise<void> => {
299 return new Promise((resolve, reject) => {
300 const file = fileOut || fs.createWriteStream(dest);
301 https
302 .get(url, (response) => {
303 if (response.statusCode === 302 || response.statusCode === 301) {
304 // Handle redirect
305 return downloadFile(response.headers.location!, dest, file).then(resolve).catch(reject);
306 }
307
308 response.on("end", () => {
309 file.close();
310 resolve();
311 });
312 response.pipe(file);
313 })
314 .on("error", (err) => {
315 fs.unlink(dest, () => {}); // Delete the file async
316 reject(err);
317 });
318 });
319 };
320
321 if (fs.existsSync(zipPath)) {
322 fs.rmSync(zipPath);

Callers 1

createWindowFunction · 0.85

Calls 2

closeMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected