From a2d5ab9390f6ad97319fd0a3683d67c6ac0cfabc Mon Sep 17 00:00:00 2001 From: Vinlic Date: Thu, 25 Apr 2024 10:47:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9F=90=E4=BA=9B=E5=A4=A7?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 70 ++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 2af9224..add3e89 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -609,35 +609,49 @@ async function uploadFile(fileUrl: string, refreshToken: string) { }); checkResult(result, refreshToken); - // 获取文件上传结果 - result = await axios.post('https://kimi.moonshot.cn/api/file', { - type: 'file', - name: filename, - object_name: objectName, - timeout: 15000 - }, { - headers: { - Authorization: `Bearer ${accessToken}`, - Referer: `https://kimi.moonshot.cn/`, - 'X-Traffic-Id': userId, - ...FAKE_HEADERS - } - }); - const { id: fileId } = checkResult(result, refreshToken); + let fileId, status, startTime = Date.now(); + while (status != 'initialized') { + if (Date.now() - startTime > 30000) + throw new Error('文件等待处理超时'); + // 获取文件上传结果 + result = await axios.post('https://kimi.moonshot.cn/api/file', { + type: 'file', + name: filename, + object_name: objectName, + timeout: 15000 + }, { + headers: { + Authorization: `Bearer ${accessToken}`, + Referer: `https://kimi.moonshot.cn/`, + 'X-Traffic-Id': userId, + ...FAKE_HEADERS + } + }); + ({ id: fileId, status } = checkResult(result, refreshToken)); + } - // 处理文件转换 - result = await axios.post('https://kimi.moonshot.cn/api/file/parse_process', { - ids: [fileId], - timeout: 120000 - }, { - headers: { - Authorization: `Bearer ${accessToken}`, - Referer: `https://kimi.moonshot.cn/`, - 'X-Traffic-Id': userId, - ...FAKE_HEADERS - } - }); - checkResult(result, refreshToken); + startTime = Date.now(); + let parseFinish = false; + while (!parseFinish) { + if (Date.now() - startTime > 30000) + throw new Error('文件等待处理超时'); + // 处理文件转换 + parseFinish = await new Promise(resolve => { + axios.post('https://kimi.moonshot.cn/api/file/parse_process', { + ids: [fileId], + timeout: 120000 + }, { + headers: { + Authorization: `Bearer ${accessToken}`, + Referer: `https://kimi.moonshot.cn/`, + 'X-Traffic-Id': userId, + ...FAKE_HEADERS + } + }) + .then(() => resolve(true)) + .catch(() => resolve(false)); + }); + } return fileId; }