From b1e4e03c682ecff03aa6a6045eea234082acbd59 Mon Sep 17 00:00:00 2001
From: lyg <1543117173@qq.com>
Date: 星期二, 11 六月 2024 23:10:02 +0800
Subject: [PATCH] 修改文件下载超时时长为10分钟

---
 src/main.mjs |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/main.mjs b/src/main.mjs
index 0e0a42f..cc3d0e5 100644
--- a/src/main.mjs
+++ b/src/main.mjs
@@ -259,8 +259,13 @@
     return;
   }
   await retry(() => {
+    const timeoutTime = 10 * 60 * 1000;
+    const source = axios.CancelToken.source();
+    const timeout = setTimeout(() => {
+      source.cancel("timeout");
+    }, timeoutTime);
     return new Promise((resolve, reject) => myAxios
-      .get(url, { responseType: "stream" })
+      .get(url, { responseType: "stream", timeout: timeoutTime, cancelToken: source.token })
       .then((response) => {
         const len = response.headers['content-length'];
         if (ext !== "pdf" && ext !== "txt" && len > 200 * 1024 * 1024) {
@@ -275,6 +280,7 @@
         const out = fs.createWriteStream(filepath);
         stream.pipe(out);
         stream.on("end", () => {
+          clearTimeout(timeout);
           book.state = `涓嬭浇瀹屾垚`;
           book.format = ext;
           book.file = filepath;
@@ -283,6 +289,7 @@
           resolve(true);
         });
         stream.on("error", (err) => {
+          clearTimeout(timeout);
           console.error(err);
           book.state = "涓嬭浇澶辫触";
           book.url = url;
@@ -297,6 +304,7 @@
         });
       })
       .catch((e) => {
+        clearTimeout(timeout);
         console.error(e);
         book.state = "涓嬭浇澶辫触";
         book.url = url;

--
Gitblit v1.9.1