mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-12-23 01:49:19 +08:00
支持解析BASE64文件数据
This commit is contained in:
parent
e193e905f3
commit
84353391cd
@ -53,6 +53,8 @@ Authorization: Bearer [refresh_token]
|
|||||||
|
|
||||||
### 文件上传解读
|
### 文件上传解读
|
||||||
|
|
||||||
|
提供一个可访问的文件URL或者BASE64_URL进行解析。
|
||||||
|
|
||||||
请求数据:
|
请求数据:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@ -105,6 +107,8 @@ Authorization: Bearer [refresh_token]
|
|||||||
|
|
||||||
### 图像解析
|
### 图像解析
|
||||||
|
|
||||||
|
提供一个可访问的图像URL或者BASE64_URL进行解析。
|
||||||
|
|
||||||
此格式兼容 [gpt-4-vision-preview](https://platform.openai.com/docs/guides/vision) API格式,您也可以用这个格式传送文档进行解析。
|
此格式兼容 [gpt-4-vision-preview](https://platform.openai.com/docs/guides/vision) API格式,您也可以用这个格式传送文档进行解析。
|
||||||
|
|
||||||
请求数据:
|
请求数据:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kimi-free-api",
|
"name": "kimi-free-api",
|
||||||
"version": "0.0.7",
|
"version": "0.0.8",
|
||||||
"description": "Kimi Free API Server",
|
"description": "Kimi Free API Server",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -298,6 +298,8 @@ async function preSignUrl(filename: string, refreshToken: string) {
|
|||||||
* @param fileUrl 文件URL
|
* @param fileUrl 文件URL
|
||||||
*/
|
*/
|
||||||
async function checkFileUrl(fileUrl: string) {
|
async function checkFileUrl(fileUrl: string) {
|
||||||
|
if(util.isBASE64Data(fileUrl))
|
||||||
|
return;
|
||||||
const result = await axios.head(fileUrl, {
|
const result = await axios.head(fileUrl, {
|
||||||
timeout: 15000,
|
timeout: 15000,
|
||||||
validateStatus: () => true
|
validateStatus: () => true
|
||||||
@ -322,24 +324,34 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
|
|||||||
// 预检查远程文件URL可用性
|
// 预检查远程文件URL可用性
|
||||||
await checkFileUrl(fileUrl);
|
await checkFileUrl(fileUrl);
|
||||||
|
|
||||||
|
let filename, fileData, mimeType;
|
||||||
|
// 如果是BASE64数据则直接转换为Buffer
|
||||||
|
if(util.isBASE64Data(fileUrl)) {
|
||||||
|
mimeType = util.extractBASE64DataFormat(fileUrl);
|
||||||
|
const ext = mime.getExtension(mimeType);
|
||||||
|
filename = `${util.uuid()}.${ext}`;
|
||||||
|
fileData = Buffer.from(util.removeBASE64DataHeader(fileUrl), 'base64');
|
||||||
|
}
|
||||||
// 下载文件到内存,如果您的服务器内存很小,建议考虑改造为流直传到下一个接口上,避免停留占用内存
|
// 下载文件到内存,如果您的服务器内存很小,建议考虑改造为流直传到下一个接口上,避免停留占用内存
|
||||||
const filename = path.basename(fileUrl);
|
else {
|
||||||
const { data: fileData } = await axios.get(fileUrl, {
|
filename = path.basename(fileUrl);
|
||||||
responseType: 'arraybuffer',
|
({ data: fileData } = await axios.get(fileUrl, {
|
||||||
// 100M限制
|
responseType: 'arraybuffer',
|
||||||
maxContentLength: FILE_MAX_SIZE,
|
// 100M限制
|
||||||
// 60秒超时
|
maxContentLength: FILE_MAX_SIZE,
|
||||||
timeout: 60000
|
// 60秒超时
|
||||||
});
|
timeout: 60000
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
// 获取预签名文件URL
|
// 获取预签名文件URL
|
||||||
const {
|
const {
|
||||||
url: uploadUrl,
|
url: uploadUrl,
|
||||||
object_name: objectName
|
object_name: objectName
|
||||||
} = await preSignUrl(filename, refreshToken);
|
} = await preSignUrl(filename, refreshToken);
|
||||||
|
|
||||||
// 获取文件的MIME类型
|
// 获取文件的MIME类型
|
||||||
const mimeType = mime.getType(filename);
|
mimeType = mimeType || mime.getType(filename);
|
||||||
// 上传文件到目标OSS
|
// 上传文件到目标OSS
|
||||||
const token = await acquireToken(refreshToken);
|
const token = await acquireToken(refreshToken);
|
||||||
let result = await axios.request({
|
let result = await axios.request({
|
||||||
|
@ -23,7 +23,7 @@ const startupTime = performance.now();
|
|||||||
await server.listen();
|
await server.listen();
|
||||||
|
|
||||||
config.service.bindAddress &&
|
config.service.bindAddress &&
|
||||||
logger.success("service bind address:", config.service.bindAddress);
|
logger.success("Service bind address:", config.service.bindAddress);
|
||||||
})()
|
})()
|
||||||
.then(() =>
|
.then(() =>
|
||||||
logger.success(
|
logger.success(
|
||||||
|
@ -136,18 +136,18 @@ const util = {
|
|||||||
return !_.isUndefined(value) && /^[a-zA-Z0-9\/\+]+(=?)+$/.test(value);
|
return !_.isUndefined(value) && /^[a-zA-Z0-9\/\+]+(=?)+$/.test(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
isBASE64Image(value) {
|
isBASE64Data(value) {
|
||||||
return /^data:image/.test(value);
|
return /^data:/.test(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
extractBASE64ImageFormat(value): string | null {
|
extractBASE64DataFormat(value): string | null {
|
||||||
const match = value.trim().match(/^data:image\/(.+);base64,/);
|
const match = value.trim().match(/^data:(.+);base64,/);
|
||||||
if(!match) return null;
|
if(!match) return null;
|
||||||
return match[1];
|
return match[1];
|
||||||
},
|
},
|
||||||
|
|
||||||
removeBASE64ImageHeader(value): string {
|
removeBASE64DataHeader(value): string {
|
||||||
return value.replace(/^data:image\/(.+);base64,/, "");
|
return value.replace(/^data:(.+);base64,/, "");
|
||||||
},
|
},
|
||||||
|
|
||||||
isDataString(value): boolean {
|
isDataString(value): boolean {
|
||||||
|
Loading…
Reference in New Issue
Block a user