@@ -1056,26 +1056,53 @@ export default class EmbeddedChatApi {
10561056 ) {
10571057 try {
10581058 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
1059+ const headers = {
1060+ "X-Auth-Token" : authToken ,
1061+ "X-User-Id" : userId ,
1062+ } ;
1063+
10591064 const form = new FormData ( ) ;
1065+ form . append ( "file" , file , fileName ) ;
1066+ if ( fileDescription . length !== 0 ) {
1067+ form . append ( "description" , fileDescription ) ;
1068+ }
1069+
1070+ const uploadResponse = await fetch (
1071+ `${ this . host } /api/v1/rooms.media/${ this . rid } ` ,
1072+ {
1073+ method : "POST" ,
1074+ body : form ,
1075+ headers,
1076+ }
1077+ ) ;
1078+ const uploadResult = await uploadResponse . json ( ) ;
1079+
1080+ if ( ! uploadResult . success || ! uploadResult . file ?. _id ) {
1081+ throw new Error ( uploadResult . error || "File upload failed" ) ;
1082+ }
1083+
1084+ const confirmBody : Record < string , any > = { } ;
1085+ if ( fileDescription . length !== 0 ) {
1086+ confirmBody . description = fileDescription ;
1087+ }
10601088 if ( threadId ) {
1061- form . append ( " tmid" , threadId ) ;
1089+ confirmBody . tmid = threadId ;
10621090 }
1063- form . append ( "file" , file , fileName ) ;
1064- form . append (
1065- "description" ,
1066- fileDescription . length !== 0 ? fileDescription : ""
1091+
1092+ const confirmResponse = await fetch (
1093+ `${ this . host } /api/v1/rooms.mediaConfirm/${ this . rid } /${ uploadResult . file . _id } ` ,
1094+ {
1095+ method : "POST" ,
1096+ headers : {
1097+ ...headers ,
1098+ "Content-Type" : "application/json" ,
1099+ } ,
1100+ body : JSON . stringify ( confirmBody ) ,
1101+ }
10671102 ) ;
1068- const response = fetch ( `${ this . host } /api/v1/rooms.upload/${ this . rid } ` , {
1069- method : "POST" ,
1070- body : form ,
1071- headers : {
1072- "X-Auth-Token" : authToken ,
1073- "X-User-Id" : userId ,
1074- } ,
1075- } ) . then ( ( r ) => r . json ( ) ) ;
1076- return response ;
1103+ return await confirmResponse . json ( ) ;
10771104 } catch ( err ) {
1078- console . log ( err ) ;
1105+ console . error ( err ) ;
10791106 }
10801107 }
10811108
0 commit comments