소스 검색

fix: 为导入流程的 fetch 请求添加超时控制

为 useImport.ts 中的三个 fetch 请求添加了 1 小时的超时信号,避免请求长时间挂起导致资源浪费。
lsc 2 주 전
부모
커밋
4e37b4c98f
1개의 변경된 파일과 4개의 추가작업 그리고 1개의 파일을 삭제
  1. 4 1
      src/hooks/useImport.ts

+ 4 - 1
src/hooks/useImport.ts

@@ -1675,10 +1675,12 @@ export default () => {
       exporting.value = true
       imgExporting.value = true
       startTimer?.()
+      const oneHour = 60 * 60 * 1000
       // 1. 上传文件到 ConvertAPI
       fetch(`https://v2.convertapi.com/upload?filename=${encodeURIComponent(file.name)}`, {
         method: 'POST',
         body: file,
+        signal: AbortSignal.timeout(oneHour),
       })
         .then(r => r.json())
         .then(uploadRes => {
@@ -1689,13 +1691,14 @@ export default () => {
             method: 'POST',
             headers: { 'Content-Type': 'application/json' },
             body: JSON.stringify(body),
+            signal: AbortSignal.timeout(oneHour),
           }).then(r => r.json())
         })
         .then(convRes => {
           const fileUrl = convRes?.Files?.[0]?.Url
           console.log('转换成功:', fileUrl)
           if (!fileUrl) throw new Error('转换失败:未获取到文件')
-          return fetch(fileUrl)
+          return fetch(fileUrl, { signal: AbortSignal.timeout(oneHour) })
         })
         .then(res => res.arrayBuffer())
         .then(buffer => {