@@ -176,8 +176,13 @@ impl Media {
176
176
///
177
177
/// * `request` - The `MediaRequest` of the content.
178
178
///
179
+ /// * `filename` - The filename specified in the event. It is suggested to
180
+ /// use the `filename()` method on the event's content instead of using
181
+ /// the `filename` field directly. If not provided, a random name will be
182
+ /// generated.
183
+ ///
179
184
/// * `content_type` - The type of the media, this will be used to set the
180
- /// temporary file's extension.
185
+ /// temporary file's extension when one isn't included in the filename .
181
186
///
182
187
/// * `use_cache` - If we should use the media cache for this request.
183
188
///
@@ -189,7 +194,7 @@ impl Media {
189
194
pub async fn get_media_file (
190
195
& self ,
191
196
request : & MediaRequest ,
192
- body : Option < String > ,
197
+ filename : Option < String > ,
193
198
content_type : & Mime ,
194
199
use_cache : bool ,
195
200
temp_dir : Option < String > ,
@@ -198,50 +203,51 @@ impl Media {
198
203
199
204
let inferred_extension = mime2ext:: mime2ext ( content_type) ;
200
205
201
- let body_path = body. as_ref ( ) . map ( Path :: new) ;
202
- let filename = body_path. and_then ( |f| f. file_name ( ) . and_then ( |f| f. to_str ( ) ) ) ;
203
- let filename_with_extension = body_path. and_then ( |f| {
204
- if f. extension ( ) . is_some ( ) {
205
- f. file_name ( ) . and_then ( |f| f. to_str ( ) )
206
- } else {
207
- None
208
- }
209
- } ) ;
206
+ let filename_as_path = filename. as_ref ( ) . map ( Path :: new) ;
210
207
211
- let ( temp_file, temp_dir) = match ( filename, filename_with_extension, inferred_extension) {
212
- // If the body is a file name and has an extension use that
213
- ( Some ( _) , Some ( filename_with_extension) , Some ( _) ) => {
214
- // Use an intermediary directory to avoid conflicts
215
- let temp_dir = temp_dir. map ( TempDir :: new_in) . unwrap_or_else ( TempDir :: new) ?;
216
- let temp_file = TempFileBuilder :: new ( )
217
- . prefix ( filename_with_extension)
218
- . rand_bytes ( 0 )
219
- . tempfile_in ( & temp_dir) ?;
220
- ( temp_file, Some ( temp_dir) )
221
- }
222
- // If the body is a file name but doesn't have an extension try inferring one for it
223
- ( Some ( filename) , None , Some ( inferred_extension) ) => {
224
- // Use an intermediary directory to avoid conflicts
225
- let temp_dir = temp_dir. map ( TempDir :: new_in) . unwrap_or_else ( TempDir :: new) ?;
226
- let temp_file = TempFileBuilder :: new ( )
227
- . prefix ( filename)
228
- . suffix ( & ( "." . to_owned ( ) + inferred_extension) )
229
- . rand_bytes ( 0 )
230
- . tempfile_in ( & temp_dir) ?;
231
- ( temp_file, Some ( temp_dir) )
232
- }
233
- // If the only thing we have is an inferred extension then use that together with a
234
- // randomly generated file name
235
- ( None , None , Some ( inferred_extension) ) => (
236
- TempFileBuilder :: new ( )
237
- . suffix ( & & ( "." . to_owned ( ) + inferred_extension) )
238
- . tempfile ( ) ?,
239
- None ,
240
- ) ,
241
- // Otherwise just use a completely random file name
242
- _ => ( TempFileBuilder :: new ( ) . tempfile ( ) ?, None ) ,
208
+ let ( sanitized_filename, filename_has_extension) = if let Some ( path) = filename_as_path {
209
+ let sanitized_filename = path. file_name ( ) . and_then ( |f| f. to_str ( ) ) ;
210
+ let filename_has_extension = path. extension ( ) . is_some ( ) ;
211
+ ( sanitized_filename, filename_has_extension)
212
+ } else {
213
+ ( None , false )
243
214
} ;
244
215
216
+ let ( temp_file, temp_dir) =
217
+ match ( sanitized_filename, filename_has_extension, inferred_extension) {
218
+ // If the file name has an extension use that
219
+ ( Some ( filename_with_extension) , true , _) => {
220
+ // Use an intermediary directory to avoid conflicts
221
+ let temp_dir = temp_dir. map ( TempDir :: new_in) . unwrap_or_else ( TempDir :: new) ?;
222
+ let temp_file = TempFileBuilder :: new ( )
223
+ . prefix ( filename_with_extension)
224
+ . rand_bytes ( 0 )
225
+ . tempfile_in ( & temp_dir) ?;
226
+ ( temp_file, Some ( temp_dir) )
227
+ }
228
+ // If the file name doesn't have an extension try inferring one for it
229
+ ( Some ( filename) , false , Some ( inferred_extension) ) => {
230
+ // Use an intermediary directory to avoid conflicts
231
+ let temp_dir = temp_dir. map ( TempDir :: new_in) . unwrap_or_else ( TempDir :: new) ?;
232
+ let temp_file = TempFileBuilder :: new ( )
233
+ . prefix ( filename)
234
+ . suffix ( & ( "." . to_owned ( ) + inferred_extension) )
235
+ . rand_bytes ( 0 )
236
+ . tempfile_in ( & temp_dir) ?;
237
+ ( temp_file, Some ( temp_dir) )
238
+ }
239
+ // If the only thing we have is an inferred extension then use that together with a
240
+ // randomly generated file name
241
+ ( None , _, Some ( inferred_extension) ) => (
242
+ TempFileBuilder :: new ( )
243
+ . suffix ( & & ( "." . to_owned ( ) + inferred_extension) )
244
+ . tempfile ( ) ?,
245
+ None ,
246
+ ) ,
247
+ // Otherwise just use a completely random file name
248
+ _ => ( TempFileBuilder :: new ( ) . tempfile ( ) ?, None ) ,
249
+ } ;
250
+
245
251
let mut file = TokioFile :: from_std ( temp_file. reopen ( ) ?) ;
246
252
file. write_all ( & data) . await ?;
247
253
// Make sure the file metadata is flushed to disk.
0 commit comments