(url: string, localFilePath: string)
| 6 | import { getCommonHeaders } from "../common/network"; |
| 7 | |
| 8 | export 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 | } |
no test coverage detected