Skip to content

Commit

Permalink
修复某些大文件无法正常上传处理问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Apr 25, 2024
1 parent 77d42d9 commit a2d5ab9
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions src/api/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a2d5ab9

Please sign in to comment.