@@ -242,33 +242,32 @@ where
242242 IT : IntoIterator < Item = I > ,
243243 I : Into < Cow < ' static , [ u8 ] > > ,
244244 {
245- let mut form = Form :: new ( ) ;
246- let mut cnt = 0 ;
247- for ( idx, data) in files. into_iter ( ) . enumerate ( ) {
248- let part = Part :: bytes ( data) . file_name ( idx. to_string ( ) ) ;
249- form = form. part ( idx. to_string ( ) , part) ;
250- cnt += 1 ;
251- }
252-
253- let r: Result < Vec < MediaInfo > , TelegraphError > = self
254- . client
255- . post_builder ( "https://telegra.ph/upload" )
256- . multipart ( form)
257- . send ( )
258- . await
259- . and_then ( Response :: error_for_status) ?
260- . json :: < UploadResult > ( )
261- . await ?
262- . into ( ) ;
263-
264- // Here we check if server returns the same amount as files posted
265- r. and_then ( |x| {
266- if x. len ( ) != cnt {
267- Err ( TelegraphError :: Server )
245+ let mut results = Vec :: new ( ) ;
246+
247+ for data in files. into_iter ( ) {
248+ let form = Form :: new ( )
249+ . text ( "reqtype" , "fileupload" )
250+ . text ( "userhash" , "" ) // Empty string for anonymous upload
251+ . part ( "fileToUpload" , Part :: bytes ( data) . file_name ( "image.jpg" ) ) ;
252+
253+ let response = self . client
254+ . post_builder ( "https://catbox.moe/user/api.php" )
255+ . multipart ( form)
256+ . send ( )
257+ . await
258+ . and_then ( Response :: error_for_status) ?;
259+
260+ let url = response. text ( ) . await ?;
261+
262+ // catbox.moe returns just the URL as plain text
263+ if url. starts_with ( "https://files.catbox.moe/" ) {
264+ results. push ( MediaInfo { src : url } ) ;
268265 } else {
269- Ok ( x )
266+ return Err ( TelegraphError :: Server ) ;
270267 }
271- } )
268+ }
269+
270+ Ok ( results)
272271 }
273272}
274273
0 commit comments