Skip to content

Commit 4d21e12

Browse files
committed
fix: 🐛 下载失败时重试3次
#311
1 parent 352adf5 commit 4d21e12

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

src/components/Manga/components/DownloadButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const DownloadButton = () => {
5353
let fileName: string;
5454
const index = `${i}`.padStart(imgIndexNum, '0');
5555
try {
56-
data = await downloadImg(url);
56+
data = await downloadImg(url, undefined, 3);
5757
fileName = img.name || `${index}.${getExtName(data.type)}`;
5858
} catch {
5959
fileName = `${index} - ${t('alert.download_failed')}`;

src/components/Manga/helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const playAnimation = (e?: HTMLElement) => {
2121
export const downloadImg = async (
2222
imgUrl: string,
2323
details?: RequestDetails<Blob>,
24+
retryNum = 0,
2425
) => {
2526
const url = store.imgMap[imgUrl]?.blobUrl ?? imgUrl;
2627

@@ -29,7 +30,7 @@ export const downloadImg = async (
2930
return res.blob();
3031
}
3132

32-
const res = await _downloadImg(url, details);
33+
const res = await _downloadImg(url, details, retryNum);
3334
if (Reflect.has(store.imgMap, imgUrl))
3435
setState('imgMap', imgUrl, 'blobUrl', URL.createObjectURL(res));
3536
return res;

src/request.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,24 @@ export const downloadImgHeaders = {
179179
export const downloadImg = async (
180180
url: string,
181181
details?: RequestDetails<Blob>,
182+
retryNum = 0,
182183
) => {
183184
if (url.startsWith('blob:')) {
184185
const res = await fetch(url);
185186
return res.blob();
186187
}
187188

188-
const res = await request<Blob>(url, {
189-
responseType: 'blob',
190-
errorText: t('translation.tip.download_img_failed'),
191-
headers: downloadImgHeaders,
192-
retryFetch: true,
193-
...details,
194-
});
189+
const res = await request<Blob>(
190+
url,
191+
{
192+
responseType: 'blob',
193+
errorText: t('translation.tip.download_img_failed'),
194+
headers: downloadImgHeaders,
195+
retryFetch: true,
196+
...details,
197+
},
198+
retryNum,
199+
);
195200

196201
return res.response;
197202
};

0 commit comments

Comments
 (0)