@@ -156,17 +156,43 @@ where
156
156
}
157
157
}
158
158
159
- /// `IntoFuture` used to send media upload requests. It wraps another
160
- /// [`SendRequest`], checking its size will be accepted by the homeserver before
161
- /// uploading.
159
+ /// `IntoFuture` used to send media upload requests. It works as a builder which
160
+ /// can create a [`SendRequest`] given several configuration options, checking
161
+ /// its size will be accepted by the homeserver before uploading.
162
162
#[ allow( missing_debug_implementations) ]
163
163
pub struct SendMediaUploadRequest {
164
- send_request : SendRequest < media:: create_content:: v3:: Request > ,
164
+ client : Client ,
165
+ request : media:: create_content:: v3:: Request ,
166
+ request_config : Option < RequestConfig > ,
167
+ progress_observable : SharedObservable < TransmissionProgress > ,
165
168
}
166
-
167
169
impl SendMediaUploadRequest {
168
- pub fn new ( request : SendRequest < media:: create_content:: v3:: Request > ) -> Self {
169
- Self { send_request : request }
170
+ /// Creates a new instance.
171
+ pub fn new ( client : Client , file : Vec < u8 > ) -> Self {
172
+ Self {
173
+ client,
174
+ request : media:: create_content:: v3:: Request :: new ( file) ,
175
+ request_config : Default :: default ( ) ,
176
+ progress_observable : SharedObservable :: new ( TransmissionProgress :: default ( ) ) ,
177
+ }
178
+ }
179
+
180
+ /// Sets the content type of the media for the media upload request.
181
+ pub fn with_content_type ( mut self , content_type : impl Into < String > ) -> Self {
182
+ self . request . content_type = Some ( content_type. into ( ) ) ;
183
+ self
184
+ }
185
+
186
+ /// Sets the filename for the media upload request.
187
+ pub fn with_filename ( mut self , filename : impl Into < String > ) -> Self {
188
+ self . request . filename = Some ( filename. into ( ) ) ;
189
+ self
190
+ }
191
+
192
+ /// Applies the provided [`RequestConfig`] to the future [`SendRequest`].
193
+ pub fn with_request_config ( mut self , request_config : Option < RequestConfig > ) -> Self {
194
+ self . request_config = request_config;
195
+ self
170
196
}
171
197
172
198
/// Replace the default `SharedObservable` used for tracking upload
@@ -179,14 +205,24 @@ impl SendMediaUploadRequest {
179
205
mut self ,
180
206
send_progress : SharedObservable < TransmissionProgress > ,
181
207
) -> Self {
182
- self . send_request = self . send_request . with_send_progress_observable ( send_progress) ;
208
+ self . progress_observable = send_progress;
183
209
self
184
210
}
185
211
186
212
/// Get a subscriber to observe the progress of sending the request
187
213
/// body.
188
214
pub fn subscribe_to_send_progress ( & self ) -> Subscriber < TransmissionProgress > {
189
- self . send_request . send_progress . subscribe ( )
215
+ self . progress_observable . subscribe ( )
216
+ }
217
+
218
+ /// Creates the [`SendRequest`] using the provided parameters.
219
+ pub fn build_send_request ( self ) -> SendRequest < media:: create_content:: v3:: Request > {
220
+ SendRequest {
221
+ client : self . client ,
222
+ request : self . request ,
223
+ config : self . request_config ,
224
+ send_progress : self . progress_observable ,
225
+ }
190
226
}
191
227
}
192
228
@@ -195,9 +231,9 @@ impl IntoFuture for SendMediaUploadRequest {
195
231
boxed_into_future ! ( ) ;
196
232
197
233
fn into_future ( self ) -> Self :: IntoFuture {
198
- let request_length = self . send_request . request . file . len ( ) ;
199
- let client = self . send_request . client . clone ( ) ;
200
- let send_request = self . send_request ;
234
+ let send_request = self . build_send_request ( ) ;
235
+ let request_length = send_request. request . file . len ( ) ;
236
+ let client = send_request. client . clone ( ) ;
201
237
202
238
Box :: pin ( async move {
203
239
let max_upload_size = client. load_or_fetch_max_upload_size ( ) . await ?;
0 commit comments