@@ -18,6 +18,7 @@ import { Kling, KlingApiTypes, KlingTask } from "@/app/providers/kling";
1818import { renderCode , RenderSubmitter } from "../render" ;
1919import { beforeUpload , safeJsonStringify } from "../utils" ;
2020import { ascetic } from "react-syntax-highlighter/dist/esm/styles/hljs" ;
21+ import { uploadToGetFileUrl } from "../utils/upload-to-server" ;
2122
2223const MODE_OPTIONS = [
2324 { label : "Std-标准模式(高性能)" , value : "std" } ,
@@ -48,6 +49,8 @@ const KlingPage = () => {
4849 const [ klingImage2VideoForm ] = ProForm . useForm ( ) ;
4950 const [ klingQueryTaskForm ] = ProForm . useForm ( ) ;
5051
52+ const [ uploadType , setUploadType ] = useState < "base64" | "url" > ( "base64" ) ;
53+
5154 const [ taskData , setTaskData ] = useState < KlingTask [ ] > ( [ ] ) ;
5255 const [ errorData , setErrorData ] = useState < any > ( null ) ;
5356
@@ -334,7 +337,6 @@ const KlingPage = () => {
334337 updateTask : ( data : KlingTask ) => void ;
335338 updateError : ( error : any ) => void ;
336339 } ) => {
337- const [ uploadType , setUploadType ] = useState < "base64" | "url" > ( "base64" ) ;
338340 const [ abortController , setAbortController ] = useState < AbortController | null > ( null ) ;
339341 const [ submitting , setSubmitting ] = useState ( false ) ;
340342
@@ -390,13 +392,11 @@ const KlingPage = () => {
390392 label = "Upload Type"
391393 options = { [
392394 { label : "Base64" , value : "base64" } ,
393- // TODO: 支持url上传,目前接口维护,后续开放
394- { label : "URL" , value : "url" , disabled : true } ,
395+ { label : "URL" , value : "url" } ,
395396 ] }
396397 fieldProps = { {
397398 value : uploadType ,
398399 onChange : ( e ) => {
399- // 当上传类型改变时,清空图片和图片尾帧当上传类型改变时,清空图片和图片尾帧
400400 props . form . resetFields ( [ "image" , "image_tail" ] ) ;
401401 setUploadType ( e . target . value ) ;
402402 } ,
@@ -417,7 +417,6 @@ const KlingPage = () => {
417417 < p > 图片文件大小不能超过10MB,图片分辨率不小于300*300px</ p >
418418 </ >
419419 }
420- action = { uploadType === "url" ? appConfig . getUploadConfig ( ) . action : undefined }
421420 fieldProps = { {
422421 listType : "picture-card" ,
423422 beforeUpload : async ( file ) =>
@@ -431,23 +430,30 @@ const KlingPage = () => {
431430 ( msg ) => message . error ( msg ) ,
432431 ) ,
433432 ...( uploadType === "url" && {
434- headers : {
435- Authorization : appConfig . getUploadConfig ( ) . auth ,
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+ }
436445 } ,
437446 onChange : ( info ) => {
438- const getValueByPosition = ( obj : any , position : readonly any [ ] ) => {
439- return position . reduce ( ( acc , key ) => acc && acc [ key ] , obj ) ;
440- } ;
441-
442447 if ( info . file . status === "done" ) {
443- try {
444- const response = info . file . response ;
445- if ( response ) {
446- info . file . url = getValueByPosition ( response , appConfig . getUploadConfig ( ) . position ) ;
447- }
448- } catch ( e ) {
449- console . error ( e ) ;
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 ( "上传失败" ) ;
450454 }
455+ } else if ( info . file . status === "error" ) {
456+ message . error ( "上传失败" ) ;
451457 }
452458 } ,
453459 } ) ,
@@ -476,7 +482,6 @@ const KlingPage = () => {
476482 < p > 图片文件大小不能超过10MB,图片分辨率不小于300*300px</ p >
477483 </ >
478484 }
479- action = { uploadType === "url" ? appConfig . getUploadConfig ( ) . action : undefined }
480485 fieldProps = { {
481486 listType : "picture-card" ,
482487 beforeUpload : async ( file ) =>
@@ -490,23 +495,30 @@ const KlingPage = () => {
490495 ( msg ) => message . error ( msg ) ,
491496 ) ,
492497 ...( uploadType === "url" && {
493- headers : {
494- Authorization : appConfig . getUploadConfig ( ) . auth ,
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+ }
495510 } ,
496511 onChange : ( info ) => {
497- const getValueByPosition = ( obj : any , position : readonly any [ ] ) => {
498- return position . reduce ( ( acc , key ) => acc && acc [ key ] , obj ) ;
499- } ;
500-
501512 if ( info . file . status === "done" ) {
502- try {
503- const response = info . file . response ;
504- if ( response ) {
505- info . file . url = getValueByPosition ( response , appConfig . getUploadConfig ( ) . position ) ;
506- }
507- } catch ( e ) {
508- console . error ( e ) ;
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 ( "上传失败" ) ;
509519 }
520+ } else if ( info . file . status === "error" ) {
521+ message . error ( "上传失败" ) ;
510522 }
511523 } ,
512524 } ) ,
0 commit comments