修复某些大文件无法正常上传处理问题

This commit is contained in:
Vinlic 2024-04-25 10:47:57 +08:00
parent 77d42d9484
commit a2d5ab9390

View File

@ -609,35 +609,49 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
}); });
checkResult(result, refreshToken); checkResult(result, refreshToken);
// 获取文件上传结果 let fileId, status, startTime = Date.now();
result = await axios.post('https://kimi.moonshot.cn/api/file', { while (status != 'initialized') {
type: 'file', if (Date.now() - startTime > 30000)
name: filename, throw new Error('文件等待处理超时');
object_name: objectName, // 获取文件上传结果
timeout: 15000 result = await axios.post('https://kimi.moonshot.cn/api/file', {
}, { type: 'file',
headers: { name: filename,
Authorization: `Bearer ${accessToken}`, object_name: objectName,
Referer: `https://kimi.moonshot.cn/`, timeout: 15000
'X-Traffic-Id': userId, }, {
...FAKE_HEADERS headers: {
} Authorization: `Bearer ${accessToken}`,
}); Referer: `https://kimi.moonshot.cn/`,
const { id: fileId } = checkResult(result, refreshToken); 'X-Traffic-Id': userId,
...FAKE_HEADERS
}
});
({ id: fileId, status } = checkResult(result, refreshToken));
}
// 处理文件转换 startTime = Date.now();
result = await axios.post('https://kimi.moonshot.cn/api/file/parse_process', { let parseFinish = false;
ids: [fileId], while (!parseFinish) {
timeout: 120000 if (Date.now() - startTime > 30000)
}, { throw new Error('文件等待处理超时');
headers: { // 处理文件转换
Authorization: `Bearer ${accessToken}`, parseFinish = await new Promise(resolve => {
Referer: `https://kimi.moonshot.cn/`, axios.post('https://kimi.moonshot.cn/api/file/parse_process', {
'X-Traffic-Id': userId, ids: [fileId],
...FAKE_HEADERS timeout: 120000
} }, {
}); headers: {
checkResult(result, refreshToken); Authorization: `Bearer ${accessToken}`,
Referer: `https://kimi.moonshot.cn/`,
'X-Traffic-Id': userId,
...FAKE_HEADERS
}
})
.then(() => resolve(true))
.catch(() => resolve(false));
});
}
return fileId; return fileId;
} }