Skip to content

Commit 9d976d0

Browse files
pixlwavebnjbvr
authored andcommitted
sdk: Update get_media_file to take a filename instead of the body.
1 parent 17370a5 commit 9d976d0

File tree

2 files changed

+50
-43
lines changed

2 files changed

+50
-43
lines changed

crates/matrix-sdk/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Breaking changes:
2525
- `Room::event` now takes an optional `RequestConfig` to allow for tweaking the network behavior.
2626
- The `instant` module was removed, use the `ruma::time` module instead.
2727
- Add `ClientBuilder::sqlite_store_with_cache_path` to build a client that stores caches in a different directory to state/crypto.
28+
- The `body` parameter in `get_media_file` has been replaced with a `filename` parameter now that Ruma has a `filename()` method.
2829

2930
Additions:
3031

crates/matrix-sdk/src/media.rs

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,13 @@ impl Media {
176176
///
177177
/// * `request` - The `MediaRequest` of the content.
178178
///
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+
///
179184
/// * `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.
181186
///
182187
/// * `use_cache` - If we should use the media cache for this request.
183188
///
@@ -189,7 +194,7 @@ impl Media {
189194
pub async fn get_media_file(
190195
&self,
191196
request: &MediaRequest,
192-
body: Option<String>,
197+
filename: Option<String>,
193198
content_type: &Mime,
194199
use_cache: bool,
195200
temp_dir: Option<String>,
@@ -198,50 +203,51 @@ impl Media {
198203

199204
let inferred_extension = mime2ext::mime2ext(content_type);
200205

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);
210207

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)
243214
};
244215

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+
245251
let mut file = TokioFile::from_std(temp_file.reopen()?);
246252
file.write_all(&data).await?;
247253
// Make sure the file metadata is flushed to disk.

0 commit comments

Comments
 (0)