Skip to content

Commit beb8829

Browse files
committed
支持使用公开部署的服务上传图片
1 parent 0af209c commit beb8829

5 files changed

Lines changed: 105 additions & 238 deletions

File tree

app/client/doc2X.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { api2ProviderBaseUrl } from "@/app/store";
21
import { Doc2XEndpoint, REQUEST_TIMEOUT_MS } from "@/constant";
32
import { Doc2XFormFields } from "@/app/pages/Doc2X";
43
import { getRequestOptions } from "@/app/client/helper";
4+
import { BASE_URL_B } from "@/app/providers/interfaces";
55

66
// {"uuid":"1ac0240c-2a30-46b6-8b17-a57df2479d34","status":"error","data":"add pages error"}
77

@@ -28,7 +28,7 @@ export class Doc2XAPI {
2828
}
2929

3030
path(): string {
31-
return [api2ProviderBaseUrl.Doc2X, Doc2XEndpoint.Default].join("/");
31+
return [BASE_URL_B, Doc2XEndpoint.Default].join("/");
3232
}
3333

3434
model(response_format: "text" | "json", ocr: boolean = false, progress: boolean = false): string {
@@ -62,7 +62,7 @@ export class Doc2XAPI {
6262
return request
6363
? {
6464
model: this.model(request.response_format, request.ocr, request.progress),
65-
messages: [{ role: "user", content: request?.file?.[0]?.url }],
65+
messages: [{ role: "user", content: request?.file }],
6666
}
6767
: {};
6868
}

app/pages/Doc2X.tsx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// app/pages/Doc2X.tsx
22

3-
import { api2Provider, useAppConfig } from "@/app/store";
3+
import { useAppConfig } from "@/app/store";
44
import { Doc2XAPI } from "@/app/client/doc2X";
55
import { ProForm, ProFormInstance, ProFormRadio, ProFormSwitch, ProFormUploadButton } from "@ant-design/pro-components";
66
import React, { useState } from "react";
@@ -13,7 +13,7 @@ export interface Doc2XFormFields {
1313
response_format: "text" | "json";
1414
ocr: boolean;
1515
progress?: boolean;
16-
file: any;
16+
file: string; // 文件地址
1717
}
1818

1919
const Doc2XForm = (props: {
@@ -94,32 +94,9 @@ const Doc2XForm = (props: {
9494
<ProFormUploadButton
9595
name="file"
9696
label="File"
97-
max={1}
98-
action={appConfig.getUploadConfig().action}
9997
accept={"application/pdf"}
10098
rules={[{ required: true, message: "Please upload a file" }]}
101-
fieldProps={{
102-
listType: "picture",
103-
headers: {
104-
Authorization: appConfig.getUploadConfig().auth,
105-
},
106-
onChange: (info) => {
107-
const getValueByPosition = (obj: any, position: readonly any[]) => {
108-
return position.reduce((acc, key) => acc && acc[key], obj);
109-
};
110-
111-
if (info.file.status === "done") {
112-
try {
113-
const response = info.file.response;
114-
if (response) {
115-
info.file.url = getValueByPosition(response, appConfig.getUploadConfig().position);
116-
}
117-
} catch (e) {
118-
console.error(e);
119-
}
120-
}
121-
},
122-
}}
99+
{...appConfig.getProFormUploadConfig("url", 1, "picture")}
123100
width="sm"
124101
/>
125102
</ProForm>
@@ -128,7 +105,7 @@ const Doc2XForm = (props: {
128105

129106
const Doc2XPage = () => {
130107
const appConfig = useAppConfig();
131-
const doc2XApi = new Doc2XAPI(appConfig.getFirstApiKey(api2Provider.Doc2X));
108+
const doc2XApi = new Doc2XAPI("");
132109

133110
const [doc2XForm] = ProForm.useForm();
134111

app/pages/Kling.tsx

Lines changed: 18 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ const KlingPage = () => {
5454
const [taskData, setTaskData] = useState<KlingTask[]>([]);
5555
const [errorData, setErrorData] = useState<any>(null);
5656

57+
const beforeUploadImage = (file: File) => {
58+
return beforeUpload(
59+
file,
60+
[-1, 10],
61+
[
62+
[300, 300],
63+
[-1, -1],
64+
],
65+
(msg) => message.error(msg),
66+
);
67+
};
68+
5769
// 更新任务数据
5870
const updateTaskData = (data: KlingTask) => {
5971
// 检查本地 id 是否已存在
@@ -408,7 +420,6 @@ const KlingPage = () => {
408420
name="image"
409421
label="Image"
410422
accept=".jpg,.jpeg,.png"
411-
max={1}
412423
rules={[{ required: true }]}
413424
tooltip={
414425
<>
@@ -417,57 +428,9 @@ const KlingPage = () => {
417428
<p>图片文件大小不能超过10MB,图片分辨率不小于300*300px</p>
418429
</>
419430
}
420-
fieldProps={{
421-
listType: "picture-card",
422-
beforeUpload: async (file) =>
423-
await beforeUpload(
424-
file,
425-
[-1, 10],
426-
[
427-
[300, 300],
428-
[-1, -1],
429-
],
430-
(msg) => message.error(msg),
431-
),
432-
...(uploadType === "url" && {
433-
customRequest: async (options) => {
434-
try {
435-
const fileUrl = await uploadToGetFileUrl(options.file as File);
436-
if (fileUrl) {
437-
options.onSuccess?.(fileUrl, options.file);
438-
} else {
439-
options.onError?.(new Error("上传失败"), options.file);
440-
}
441-
} catch (error) {
442-
console.error("Upload error:", error);
443-
options.onError?.(error as Error, options.file);
444-
}
445-
},
446-
onChange: (info) => {
447-
if (info.file.status === "done") {
448-
const fileUrl = info.file.response;
449-
if (fileUrl && typeof fileUrl === "string") {
450-
info.file.url = fileUrl;
451-
} else {
452-
info.file.status = "error";
453-
message.error("上传失败");
454-
}
455-
} else if (info.file.status === "error") {
456-
message.error("上传失败");
457-
}
458-
},
459-
}),
460-
}}
461-
transform={(value: Array<{ thumbUrl: string; url: string }> | undefined) => {
462-
if (!value) {
463-
return undefined;
464-
}
465-
if (uploadType === "base64") {
466-
return value?.[0]?.thumbUrl.replace(/^data:image\/\w+;base64,/, "");
467-
} else {
468-
return value?.[0]?.url;
469-
}
470-
}}
431+
{...((uploadType === "url"
432+
? appConfig.getProFormUploadConfig("url", 1, "picture-card", false, beforeUploadImage)
433+
: appConfig.getProFormUploadConfig("base64", 1, "picture-card", false, beforeUploadImage)) as any)}
471434
/>
472435

473436
<ProFormUploadButton
@@ -482,57 +445,9 @@ const KlingPage = () => {
482445
<p>图片文件大小不能超过10MB,图片分辨率不小于300*300px</p>
483446
</>
484447
}
485-
fieldProps={{
486-
listType: "picture-card",
487-
beforeUpload: async (file) =>
488-
await beforeUpload(
489-
file,
490-
[-1, 10],
491-
[
492-
[300, 300],
493-
[-1, -1],
494-
],
495-
(msg) => message.error(msg),
496-
),
497-
...(uploadType === "url" && {
498-
customRequest: async (options) => {
499-
try {
500-
const fileUrl = await uploadToGetFileUrl(options.file as File);
501-
if (fileUrl) {
502-
options.onSuccess?.(fileUrl, options.file);
503-
} else {
504-
options.onError?.(new Error("上传失败"), options.file);
505-
}
506-
} catch (error) {
507-
console.error("Upload error:", error);
508-
options.onError?.(error as Error, options.file);
509-
}
510-
},
511-
onChange: (info) => {
512-
if (info.file.status === "done") {
513-
const fileUrl = info.file.response;
514-
if (fileUrl && typeof fileUrl === "string") {
515-
info.file.url = fileUrl;
516-
} else {
517-
info.file.status = "error";
518-
message.error("上传失败");
519-
}
520-
} else if (info.file.status === "error") {
521-
message.error("上传失败");
522-
}
523-
},
524-
}),
525-
}}
526-
transform={(value: Array<{ thumbUrl: string; url: string }> | undefined) => {
527-
if (!value) {
528-
return undefined;
529-
}
530-
if (uploadType === "base64") {
531-
return value?.[0]?.thumbUrl.replace(/^data:image\/\w+;base64,/, "");
532-
} else {
533-
return value?.[0]?.url;
534-
}
535-
}}
448+
{...((uploadType === "url"
449+
? appConfig.getProFormUploadConfig("url", 1, "picture-card", false, beforeUploadImage)
450+
: appConfig.getProFormUploadConfig("base64", 1, "picture-card", false, beforeUploadImage)) as any)}
536451
/>
537452
</ProForm.Group>
538453
<Divider />

0 commit comments

Comments
 (0)