forked from mirror/MCSM-Daemon
Feat: add download file try catch
This commit is contained in:
parent
9a0a7b0d66
commit
1119e6e1f9
@ -9,14 +9,12 @@ import logger from "./log";
|
|||||||
export function downloadFileToLocalFile(url: string, localFilePath: string): Promise<boolean> {
|
export function downloadFileToLocalFile(url: string, localFilePath: string): Promise<boolean> {
|
||||||
logger.info(`Download File: ${url} --> ${path.normalize(localFilePath)}`);
|
logger.info(`Download File: ${url} --> ${path.normalize(localFilePath)}`);
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
const writeStream = fs.createWriteStream(path.normalize(localFilePath));
|
const writeStream = fs.createWriteStream(path.normalize(localFilePath));
|
||||||
const response = await axios<Readable>({
|
const response = await axios<Readable>({
|
||||||
url,
|
url,
|
||||||
responseType: "stream"
|
responseType: "stream"
|
||||||
});
|
});
|
||||||
if (response.status > 299 || response.status < 200) {
|
|
||||||
reject(new Error(`Download File: Target File response code is ${response.status} NOT 2XX.`));
|
|
||||||
}
|
|
||||||
pipeline(response.data, writeStream, (err) => {
|
pipeline(response.data, writeStream, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
@ -24,5 +22,8 @@ export function downloadFileToLocalFile(url: string, localFilePath: string): Pro
|
|||||||
resolve(true);
|
resolve(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user