@@ -13,6 +13,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
13
13
use std:: {
14
14
fmt:: { Debug , Display } ,
15
15
ops:: Deref ,
16
+ path:: Path ,
16
17
sync:: Arc
17
18
} ;
18
19
use stickerpicker:: StickerWidget ;
@@ -176,17 +177,29 @@ pub async fn whoami(matrix: &Config) -> Result<Whoami, Error> {
176
177
}
177
178
}
178
179
179
- pub ( crate ) async fn upload ( matrix : & Config , filename : & String , data : Arc < Vec < u8 > > , mimetype : & str ) -> Result < Mxc , Error > {
180
- let mut mxc = upload_ref ( matrix, filename, data. as_slice ( ) , mimetype) . await ?;
180
+ pub ( crate ) async fn upload < P > ( matrix : & Config , path : P , data : Arc < Vec < u8 > > , mimetype : & str ) -> Result < Mxc , Error >
181
+ where
182
+ P : AsRef < Path >
183
+ {
184
+ let mut mxc = upload_ref ( matrix, path, data. as_slice ( ) , mimetype) . await ?;
181
185
mxc. data = Some ( data) ;
182
186
Ok ( mxc)
183
187
}
184
188
185
- pub ( crate ) async fn upload_ref ( matrix : & Config , filename : & String , data : & [ u8 ] , mimetype : & str ) -> Result < Mxc , Error > {
189
+ pub ( crate ) async fn upload_ref < P > ( matrix : & Config , path : P , data : & [ u8 ] , mimetype : & str ) -> Result < Mxc , Error >
190
+ where
191
+ P : AsRef < Path >
192
+ {
193
+ let path = path. as_ref ( ) ;
194
+ let filename = path
195
+ . file_name ( )
196
+ . unwrap_or_else ( || path. as_os_str ( ) )
197
+ . to_str ( )
198
+ . expect ( "Not valid UTF-8" ) ;
186
199
let answer = CLIENT
187
200
. get ( )
188
201
. post ( & format ! ( "{}/_matrix/media/v3/upload" , matrix. homeserver_url) )
189
- . query ( & [ ( "access_token" , & matrix. access_token ) , ( "filename" , filename) ] )
202
+ . query ( & [ ( "access_token" , matrix. access_token . as_str ( ) ) , ( "filename" , filename) ] )
190
203
. header ( "Content-Type" , mimetype)
191
204
. body ( data. to_owned ( ) ) //TODO check for better solution
192
205
. send ( )
0 commit comments