MCPcopy Create free account
hub / github.com/MCSManager/MCSManager / downloadFileToLocalFile

Function downloadFileToLocalFile

daemon/src/service/download.ts:8–34  ·  view source on GitHub ↗
(url: string, localFilePath: string)

Source from the content-addressed store, hash-verified

6import { getCommonHeaders } from "../common/network";
7
8export function downloadFileToLocalFile(url: string, localFilePath: string): Promise<boolean> {
9 logger.info(`Download File: ${url} --> ${path.normalize(localFilePath)}`);
10 return new Promise(async (resolve, reject) => {
11 try {
12 if (fs.existsSync(localFilePath)) fs.removeSync(localFilePath);
13 const response = await axios<Readable>({
14 url,
15 responseType: "stream",
16 timeout: 1000 * 20,
17 headers: getCommonHeaders(url),
18 maxRedirects: 10
19 });
20 const writeStream = fs.createWriteStream(path.normalize(localFilePath));
21 pipeline(response.data, writeStream, (err) => {
22 if (err) {
23 fs.remove(localFilePath, () => {});
24 reject(err);
25 } else {
26 fs.chmodSync(localFilePath, 0o777);
27 resolve(true);
28 }
29 });
30 } catch (error: any) {
31 reject(error.message);
32 }
33 });
34}

Callers 1

startMethod · 0.90

Calls 2

getCommonHeadersFunction · 0.90
infoMethod · 0.80

Tested by

no test coverage detected