Skip to content

Commit a98f02d

Browse files
committed
feat: add retry
1 parent b456136 commit a98f02d

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

index.js

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function ensureDirectoryExists(filePath) {
3636
}
3737

3838
// 下载文件的函数
39-
function downloadFile(url, filePath) {
39+
function downloadFile(url, filePath, retryCount = 3) {
4040
ensureDirectoryExists(filePath);
4141

4242
// 如果文件已存在,跳过下载
@@ -46,16 +46,81 @@ function downloadFile(url, filePath) {
4646
}
4747

4848
const file = fs.createWriteStream(filePath);
49-
https.get(url, response => {
50-
response.pipe(file);
51-
file.on('finish', () => {
49+
let fileSize = 0;
50+
let currentRetry = 0;
51+
52+
const startDownload = () => {
53+
fileSize = 0; // 重置文件大小计数
54+
55+
https.get(url, response => {
56+
// 检查响应状态码
57+
if (response.statusCode !== 200) {
58+
file.close();
59+
fs.unlink(filePath, () => {});
60+
if (currentRetry < retryCount) {
61+
currentRetry++;
62+
console.log(`Retrying download (${currentRetry}/${retryCount}) for ${filePath} due to HTTP status ${response.statusCode}`);
63+
startDownload();
64+
} else {
65+
console.error(`Failed to download ${filePath}: HTTP Status ${response.statusCode} after ${retryCount} retries`);
66+
}
67+
return;
68+
}
69+
70+
response.on('data', (chunk) => {
71+
fileSize += chunk.length;
72+
});
73+
74+
response.pipe(file);
75+
76+
file.on('finish', () => {
77+
file.close(() => {
78+
// 检查文件大小
79+
if (fileSize === 0) {
80+
fs.unlink(filePath, () => {
81+
if (currentRetry < retryCount) {
82+
currentRetry++;
83+
console.log(`Retrying download (${currentRetry}/${retryCount}) for ${filePath} due to empty file`);
84+
startDownload();
85+
} else {
86+
console.error(`Failed to download ${filePath}: File is empty after ${retryCount} retries`);
87+
}
88+
});
89+
} else {
90+
console.log(`Downloaded: ${filePath} (${fileSize} bytes)`);
91+
}
92+
});
93+
});
94+
}).on('error', err => {
5295
file.close();
53-
console.log(`Downloaded: ${filePath}`);
96+
fs.unlink(filePath, () => {
97+
if (currentRetry < retryCount) {
98+
currentRetry++;
99+
console.log(`Retrying download (${currentRetry}/${retryCount}) for ${filePath} due to error: ${err.message}`);
100+
startDownload();
101+
} else {
102+
console.error(`Download failed for ${filePath} after ${retryCount} retries:`, err.message);
103+
}
104+
});
105+
});
106+
};
107+
108+
// 添加文件写入错误处理
109+
file.on('error', err => {
110+
file.close();
111+
fs.unlink(filePath, () => {
112+
if (currentRetry < retryCount) {
113+
currentRetry++;
114+
console.log(`Retrying download (${currentRetry}/${retryCount}) for ${filePath} due to file write error: ${err.message}`);
115+
startDownload();
116+
} else {
117+
console.error(`File write error for ${filePath} after ${retryCount} retries:`, err.message);
118+
}
54119
});
55-
}).on('error', err => {
56-
fs.unlink(filePath, () => { }); // 删除未完成的文件
57-
console.error(`Download failed for ${filePath}:`, err.message);
58120
});
121+
122+
// 开始首次下载
123+
startDownload();
59124
}
60125

61126
// 获取标准输出

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "resource-save-script",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"main": "index.js",
55
"author": "",
66
"license": "ISC",

0 commit comments

Comments
 (0)