| | |
| | | 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) { |
| | |
| | | const out = fs.createWriteStream(filepath); |
| | | stream.pipe(out); |
| | | stream.on("end", () => { |
| | | clearTimeout(timeout); |
| | | book.state = `下载完成`; |
| | | book.format = ext; |
| | | book.file = filepath; |
| | |
| | | resolve(true); |
| | | }); |
| | | stream.on("error", (err) => { |
| | | clearTimeout(timeout); |
| | | console.error(err); |
| | | book.state = "下载失败"; |
| | | book.url = url; |
| | |
| | | }); |
| | | }) |
| | | .catch((e) => { |
| | | clearTimeout(timeout); |
| | | console.error(e); |
| | | book.state = "下载失败"; |
| | | book.url = url; |