matrix_sdk/media.rs
1// Copyright 2021 Kévin Commaille
2// Copyright 2022 The Matrix.org Foundation C.I.C.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! High-level media API.
17
18#[cfg(feature = "e2e-encryption")]
19use std::io::Read;
20use std::{fmt, time::Duration};
21#[cfg(not(target_family = "wasm"))]
22use std::{fs::File, path::Path};
23
24use eyeball::SharedObservable;
25use futures_util::future::try_join;
26use matrix_sdk_base::media::store::IgnoreMediaRetentionPolicy;
27pub use matrix_sdk_base::media::{store::MediaRetentionPolicy, *};
28use matrix_sdk_common::{BoxFuture, SendOutsideWasm, SyncOutsideWasm};
29use mime::Mime;
30use ruma::{
31 MilliSecondsSinceUnixEpoch, MxcUri, OwnedMxcUri, TransactionId, UInt,
32 api::{
33 Metadata,
34 client::{authenticated_media, media},
35 error::ErrorKind,
36 },
37 assign,
38 events::room::{MediaSource, ThumbnailInfo},
39};
40use serde_json::value::RawValue as RawJsonValue;
41#[cfg(not(target_family = "wasm"))]
42use tempfile::{Builder as TempFileBuilder, NamedTempFile, TempDir};
43#[cfg(not(target_family = "wasm"))]
44use tokio::{
45 fs::File as TokioFile,
46 io::{AsyncSeekExt, AsyncWriteExt},
47};
48
49use crate::{
50 Client, Error, Result, TransmissionProgress, attachment::Thumbnail,
51 client::futures::SendMediaUploadRequest, config::RequestConfig,
52};
53
54/// A conservative upload speed of 1Mbps
55const DEFAULT_UPLOAD_SPEED: u64 = 125_000;
56/// 5 min minimal upload request timeout, used to clamp the request timeout.
57const MIN_UPLOAD_REQUEST_TIMEOUT: Duration = Duration::from_secs(60 * 5);
58/// The server name used to generate local MXC URIs.
59// This mustn't represent a potentially valid media server, otherwise it'd be
60// possible for an attacker to return malicious content under some preconditions
61// (e.g. the cache store has been cleared before the upload took place). To
62// mitigate against this, we use the .localhost TLD, which is guaranteed to be
63// on the local machine. As a result, the only attack possible would be coming
64// from the user themselves, which we consider a non-threat.
65const LOCAL_MXC_SERVER_NAME: &str = "send-queue.localhost";
66
67/// A high-level API to interact with the media API.
68#[derive(Debug, Clone)]
69pub struct Media {
70 /// The underlying HTTP client.
71 client: Client,
72}
73
74/// A file handle that takes ownership of a media file on disk. When the handle
75/// is dropped, the file will be removed from the disk.
76#[derive(Debug)]
77#[cfg(not(target_family = "wasm"))]
78pub struct MediaFileHandle {
79 /// The temporary file that contains the media.
80 file: NamedTempFile,
81 /// An intermediary temporary directory used in certain cases.
82 ///
83 /// Only stored for its `Drop` semantics.
84 _directory: Option<TempDir>,
85}
86
87#[cfg(not(target_family = "wasm"))]
88impl MediaFileHandle {
89 /// Get the media file's path.
90 pub fn path(&self) -> &Path {
91 self.file.path()
92 }
93
94 /// Persist the media file to the given path.
95 pub fn persist(self, path: &Path) -> Result<File, PersistError> {
96 self.file.persist(path).map_err(|e| PersistError {
97 error: e.error,
98 file: Self { file: e.file, _directory: self._directory },
99 })
100 }
101}
102
103/// Error returned when [`MediaFileHandle::persist`] fails.
104#[cfg(not(target_family = "wasm"))]
105pub struct PersistError {
106 /// The underlying IO error.
107 pub error: std::io::Error,
108 /// The temporary file that couldn't be persisted.
109 pub file: MediaFileHandle,
110}
111
112#[cfg(not(any(target_family = "wasm", tarpaulin_include)))]
113impl fmt::Debug for PersistError {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 write!(f, "PersistError({:?})", self.error)
116 }
117}
118
119#[cfg(not(any(target_family = "wasm", tarpaulin_include)))]
120impl fmt::Display for PersistError {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "failed to persist temporary file: {}", self.error)
123 }
124}
125
126/// A preallocated MXC URI created by [`Media::create_content_uri()`], and to be
127/// used with [`Media::upload_preallocated()`].
128#[derive(Debug)]
129pub struct PreallocatedMxcUri {
130 /// The URI for the media URI.
131 pub uri: OwnedMxcUri,
132 /// The expiration date for the media URI.
133 expire_date: Option<MilliSecondsSinceUnixEpoch>,
134}
135
136/// An error that happened in the realm of media.
137#[derive(Debug, thiserror::Error)]
138pub enum MediaError {
139 /// A preallocated MXC URI has expired.
140 #[error("a preallocated MXC URI has expired")]
141 ExpiredPreallocatedMxcUri,
142
143 /// Preallocated media already had content, cannot overwrite.
144 #[error("preallocated media already had content, cannot overwrite")]
145 CannotOverwriteMedia,
146
147 /// Local-only media content was not found.
148 #[error("local-only media content was not found")]
149 LocalMediaNotFound,
150
151 /// The provided media is too large to upload.
152 #[error(
153 "The provided media is too large to upload. \
154 Maximum upload length is {max} bytes, tried to upload {current} bytes"
155 )]
156 MediaTooLargeToUpload {
157 /// The `max_upload_size` value for this homeserver.
158 max: UInt,
159 /// The size of the current media to upload.
160 current: UInt,
161 },
162
163 /// Fetching the `max_upload_size` value from the homeserver failed.
164 #[error("Fetching the `max_upload_size` value from the homeserver failed: {0}")]
165 FetchMaxUploadSizeFailed(String),
166}
167
168/// A generic trait for fetching media content.
169pub trait MediaFetcher: SendOutsideWasm + SyncOutsideWasm + fmt::Debug {
170 /// Fetches the media content for the given [`MediaRequestParameters`].
171 /// Returns either a byte array or an [`crate::Error`].
172 fn fetch_media_content<'a>(
173 &'a self,
174 client: &'a Client,
175 request: &'a MediaRequestParameters,
176 ) -> BoxFuture<'a, Result<Vec<u8>, Error>>;
177
178 /// Fetches the media content for the given [`MediaRequestParameters`],
179 /// using the provided [RequestConfig]. Returns either a byte array or an
180 /// [`crate::Error`].
181 fn fetch_media_content_with_config<'a>(
182 &'a self,
183 client: &'a Client,
184 request: &'a MediaRequestParameters,
185 request_config: RequestConfig,
186 ) -> BoxFuture<'a, Result<Vec<u8>, Error>>;
187}
188
189impl Media {
190 pub(crate) fn new(client: Client) -> Self {
191 Self { client }
192 }
193
194 /// Upload some media to the server.
195 ///
196 /// # Arguments
197 ///
198 /// - `content_type` - The type of the media, this will be used as the
199 /// content-type header.
200 ///
201 /// - `data` - Vector of bytes to be uploaded to the server.
202 /// - `request_config` - Optional request configuration for the HTTP client,
203 /// overriding the default. If not provided, a reasonable timeout value is
204 /// inferred.
205 ///
206 /// # Examples
207 ///
208 /// ```no_run
209 /// # use std::fs;
210 /// # use matrix_sdk::{Client, ruma::room_id};
211 /// # use url::Url;
212 /// # use mime;
213 /// # async {
214 /// # let homeserver = Url::parse("http://localhost:8080")?;
215 /// # let mut client = Client::new(homeserver).await?;
216 /// let image = fs::read("/home/example/my-cat.jpg")?;
217 ///
218 /// let response =
219 /// client.media().upload(&mime::IMAGE_JPEG, image, None).await?;
220 ///
221 /// println!("Cat URI: {}", response.content_uri);
222 /// # anyhow::Ok(()) };
223 /// ```
224 pub fn upload(
225 &self,
226 content_type: &Mime,
227 data: Vec<u8>,
228 request_config: Option<RequestConfig>,
229 ) -> SendMediaUploadRequest {
230 let request_config = request_config.unwrap_or_else(|| {
231 self.client.request_config().timeout(Self::reasonable_upload_timeout(&data))
232 });
233
234 let request = assign!(media::create_content::v3::Request::new(data), {
235 content_type: Some(content_type.essence_str().to_owned()),
236 });
237
238 let request = self.client.send(request).with_request_config(request_config);
239 SendMediaUploadRequest::new(request)
240 }
241
242 /// Returns a reasonable upload timeout for an upload, based on the size of
243 /// the data to be uploaded.
244 pub(crate) fn reasonable_upload_timeout(data: &[u8]) -> Duration {
245 std::cmp::max(
246 Duration::from_secs(data.len() as u64 / DEFAULT_UPLOAD_SPEED),
247 MIN_UPLOAD_REQUEST_TIMEOUT,
248 )
249 }
250
251 /// Preallocates an MXC URI for a media that will be uploaded soon.
252 ///
253 /// This preallocates an URI _before_ any content is uploaded to the server.
254 /// The resulting preallocated MXC URI can then be consumed with
255 /// [`Media::upload_preallocated`].
256 ///
257 /// # Examples
258 ///
259 /// ```no_run
260 /// # use std::fs;
261 /// # use matrix_sdk::{Client, ruma::room_id};
262 /// # use url::Url;
263 /// # use mime;
264 /// # async {
265 /// # let homeserver = Url::parse("http://localhost:8080")?;
266 /// # let mut client = Client::new(homeserver).await?;
267 ///
268 /// let preallocated = client.media().create_content_uri().await?;
269 /// println!("Cat URI: {}", preallocated.uri);
270 ///
271 /// let image = fs::read("/home/example/my-cat.jpg")?;
272 /// client
273 /// .media()
274 /// .upload_preallocated(preallocated, &mime::IMAGE_JPEG, image)
275 /// .await?;
276 ///
277 /// # anyhow::Ok(()) };
278 /// ```
279 pub async fn create_content_uri(&self) -> Result<PreallocatedMxcUri> {
280 // Note: this request doesn't have any parameters.
281 let request = media::create_mxc_uri::v1::Request::default();
282
283 let response = self.client.send(request).await?;
284
285 Ok(PreallocatedMxcUri {
286 uri: response.content_uri,
287 expire_date: response.unused_expires_at,
288 })
289 }
290
291 /// Fills the content of a preallocated MXC URI with the given content type
292 /// and data.
293 ///
294 /// The URI must have been preallocated with [`Self::create_content_uri`].
295 /// See this method's documentation for a full example.
296 pub async fn upload_preallocated(
297 &self,
298 uri: PreallocatedMxcUri,
299 content_type: &Mime,
300 data: Vec<u8>,
301 ) -> Result<()> {
302 // Do a best-effort at reporting an expired MXC URI here; otherwise the
303 // server may complain about it later.
304 if let Some(expire_date) = uri.expire_date
305 && MilliSecondsSinceUnixEpoch::now() >= expire_date
306 {
307 return Err(Error::Media(MediaError::ExpiredPreallocatedMxcUri));
308 }
309
310 let timeout = std::cmp::max(
311 Duration::from_secs(data.len() as u64 / DEFAULT_UPLOAD_SPEED),
312 MIN_UPLOAD_REQUEST_TIMEOUT,
313 );
314
315 let request = assign!(media::create_content_async::v3::Request::from_url(&uri.uri, data)?, {
316 content_type: Some(content_type.as_ref().to_owned()),
317 });
318
319 let request_config = self.client.request_config().timeout(timeout);
320
321 if let Err(err) = self.client.send(request).with_request_config(request_config).await {
322 match err.client_api_error_kind() {
323 Some(ErrorKind::CannotOverwriteMedia) => {
324 Err(Error::Media(MediaError::CannotOverwriteMedia))
325 }
326
327 // Unfortunately, the spec says a server will return 404 for
328 // either an expired MXC ID or a non-existing MXC ID. Do a
329 // best-effort guess to recognize an expired MXC ID based on the
330 // error string, which will work with Synapse (as of
331 // 2024-10-23).
332 Some(ErrorKind::Unknown) if err.to_string().contains("expired") => {
333 Err(Error::Media(MediaError::ExpiredPreallocatedMxcUri))
334 }
335
336 _ => Err(err.into()),
337 }
338 } else {
339 Ok(())
340 }
341 }
342
343 /// Gets a media file by copying it to a temporary location on disk.
344 ///
345 /// The file won't be encrypted even if it is encrypted on the server.
346 ///
347 /// Returns a `MediaFileHandle` which takes ownership of the file. When the
348 /// handle is dropped, the file will be deleted from the temporary location.
349 ///
350 /// # Arguments
351 ///
352 /// - `request` - The `MediaRequest` of the content.
353 /// - `filename` - The filename specified in the event. It is suggested to
354 /// use the `filename()` method on the event's content instead of using
355 /// the `filename` field directly. If not provided, a random name will be
356 /// generated.
357 ///
358 /// - `content_type` - The type of the media, this will be used to set the
359 /// temporary file's extension when one isn't included in the filename.
360 ///
361 /// - `use_cache` - If we should use the media cache for this request.
362 /// - `temp_dir` - Path to a directory where temporary directories can be
363 /// created. If not provided, a default, global temporary directory will
364 /// be used; this may not work properly on Android, where the default
365 /// location may require root access on some older Android versions.
366 #[cfg(not(target_family = "wasm"))]
367 pub async fn get_media_file(
368 &self,
369 request: &MediaRequestParameters,
370 filename: Option<String>,
371 content_type: &Mime,
372 use_cache: bool,
373 temp_dir: Option<String>,
374 ) -> Result<MediaFileHandle> {
375 let data = self.get_media_content(request, use_cache).await?;
376
377 let inferred_extension = mime2ext::mime2ext(content_type);
378
379 let filename_as_path = filename.as_ref().map(Path::new);
380
381 let (sanitized_filename, filename_has_extension) = if let Some(path) = filename_as_path {
382 let sanitized_filename = path.file_name().and_then(|f| f.to_str());
383 let filename_has_extension = path.extension().is_some();
384 (sanitized_filename, filename_has_extension)
385 } else {
386 (None, false)
387 };
388
389 let (temp_file, temp_dir) =
390 match (sanitized_filename, filename_has_extension, inferred_extension) {
391 // If the file name has an extension use that
392 (Some(filename_with_extension), true, _) => {
393 // Use an intermediary directory to avoid conflicts
394 let temp_dir = temp_dir.map(TempDir::new_in).unwrap_or_else(TempDir::new)?;
395 let temp_file = TempFileBuilder::new()
396 .prefix(filename_with_extension)
397 .rand_bytes(0)
398 .tempfile_in(&temp_dir)?;
399 (temp_file, Some(temp_dir))
400 }
401 // If the file name doesn't have an extension try inferring one for it
402 (Some(filename), false, Some(inferred_extension)) => {
403 // Use an intermediary directory to avoid conflicts
404 let temp_dir = temp_dir.map(TempDir::new_in).unwrap_or_else(TempDir::new)?;
405 let temp_file = TempFileBuilder::new()
406 .prefix(filename)
407 .suffix(&(".".to_owned() + inferred_extension))
408 .rand_bytes(0)
409 .tempfile_in(&temp_dir)?;
410 (temp_file, Some(temp_dir))
411 }
412 // If the only thing we have is an inferred extension then use
413 // that together with a randomly generated file name
414 (None, _, Some(inferred_extension)) => (
415 TempFileBuilder::new()
416 .suffix(&&(".".to_owned() + inferred_extension))
417 .tempfile()?,
418 None,
419 ),
420 // Otherwise just use a completely random file name
421 _ => (TempFileBuilder::new().tempfile()?, None),
422 };
423
424 // Reuse the same temp file reference by cloning it instead of reopening
425 // it.
426 let mut file = TokioFile::from_std(temp_file.as_file().try_clone()?);
427 file.write_all(&data).await?;
428 // Make sure the file metadata is flushed to disk.
429 file.sync_all().await?;
430 // Both handles share the same cursor, rewind it so the file returned by
431 // `MediaFileHandle::persist()` is read from the start.
432 file.rewind().await?;
433
434 Ok(MediaFileHandle { file: temp_file, _directory: temp_dir })
435 }
436
437 /// Get a media file's content.
438 ///
439 /// If the content is encrypted and encryption is enabled, the content will
440 /// be decrypted.
441 ///
442 /// # Arguments
443 ///
444 /// - `request` - The `MediaRequest` of the content.
445 /// - `use_cache` - If we should use the media cache for this request.
446 pub async fn get_media_content(
447 &self,
448 request: &MediaRequestParameters,
449 use_cache: bool,
450 ) -> Result<Vec<u8>> {
451 // This is a local media. Force to read the media's content from the
452 // store: it cannot exist somewhere else!
453 if Self::is_local_uri(&request.source) {
454 return self.get_cached_media_content(request).await?.ok_or_else(|| {
455 Error::MediaStore(Box::new(store::MediaStoreError::InvalidData {
456 details: format!("Media does not exist: `{request:?}`"),
457 }))
458 });
459 }
460
461 // Read from the cache: if it doesn't exist, the execution continues by
462 // reading the media from the network.
463 if use_cache && let Some(content) = self.get_cached_media_content(request).await? {
464 return Ok(content);
465 }
466
467 let content = self
468 .client
469 .inner
470 .media_fetcher
471 .read()
472 .await
473 .fetch_media_content(&self.client, request)
474 .await?;
475
476 if use_cache {
477 self.client
478 .media_store()
479 .lock()
480 .await?
481 .add_media_content(request, content.clone(), IgnoreMediaRetentionPolicy::No)
482 .await?;
483 }
484
485 Ok(content)
486 }
487
488 /// Get a media file's content from the cache only, without fetching it
489 /// from the network.
490 ///
491 /// Returns `None` if the content isn't in the media store.
492 ///
493 /// # Arguments
494 ///
495 /// - `request` - The `MediaRequest` of the content.
496 pub async fn get_cached_media_content(
497 &self,
498 request: &MediaRequestParameters,
499 ) -> Result<Option<Vec<u8>>> {
500 // Local medias are always cached with `MediaFormat::File`, be it the
501 // file itself or its thumbnail (see `RoomSendQueue::cache_media`), so
502 // ignore the requested format.
503 let request = if Self::is_local_uri(&request.source) {
504 &MediaRequestParameters { source: request.source.clone(), format: MediaFormat::File }
505 } else {
506 request
507 };
508
509 Ok(self.client.media_store().lock().await?.get_media_content(request).await?)
510 }
511
512 /// Remove a media file's content from the store.
513 ///
514 /// # Arguments
515 ///
516 /// * `request` - The `MediaRequest` of the content.
517 pub async fn remove_media_content(&self, request: &MediaRequestParameters) -> Result<()> {
518 Ok(self.client.media_store().lock().await?.remove_media_content(request).await?)
519 }
520
521 /// Delete all the media content corresponding to the given uri from the
522 /// store.
523 ///
524 /// # Arguments
525 ///
526 /// - `uri` - The `MxcUri` of the files.
527 pub async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<()> {
528 Ok(self.client.media_store().lock().await?.remove_media_content_for_uri(uri).await?)
529 }
530
531 /// Get the file of the given media event content.
532 ///
533 /// If the content is encrypted and encryption is enabled, the content will
534 /// be decrypted.
535 ///
536 /// Returns `Ok(None)` if the event content has no file.
537 ///
538 /// This is a convenience method that calls the
539 /// [`get_media_content`](#method.get_media_content) method.
540 ///
541 /// # Arguments
542 ///
543 /// - `event_content` - The media event content.
544 /// - `use_cache` - If we should use the media cache for this file.
545 pub async fn get_file(
546 &self,
547 event_content: &impl MediaEventContent,
548 use_cache: bool,
549 ) -> Result<Option<Vec<u8>>> {
550 let Some(source) = event_content.source() else { return Ok(None) };
551 let file = self
552 .get_media_content(
553 &MediaRequestParameters { source, format: MediaFormat::File },
554 use_cache,
555 )
556 .await?;
557 Ok(Some(file))
558 }
559
560 /// Remove the file of the given media event content from the cache.
561 ///
562 /// This is a convenience method that calls the
563 /// [`remove_media_content`](#method.remove_media_content) method.
564 ///
565 /// # Arguments
566 ///
567 /// - `event_content` - The media event content.
568 pub async fn remove_file(&self, event_content: &impl MediaEventContent) -> Result<()> {
569 if let Some(source) = event_content.source() {
570 self.remove_media_content(&MediaRequestParameters {
571 source,
572 format: MediaFormat::File,
573 })
574 .await?;
575 }
576
577 Ok(())
578 }
579
580 /// Get a thumbnail of the given media event content.
581 ///
582 /// If the content is encrypted and encryption is enabled, the content will
583 /// be decrypted.
584 ///
585 /// Returns `Ok(None)` if the event content has no thumbnail.
586 ///
587 /// This is a convenience method that calls the
588 /// [`get_media_content`](#method.get_media_content) method.
589 ///
590 /// # Arguments
591 ///
592 /// - `event_content` - The media event content.
593 /// - `settings` - The _desired_ settings of the thumbnail. The actual
594 /// thumbnail may not match the settings specified.
595 ///
596 /// - `use_cache` - If we should use the media cache for this thumbnail.
597 pub async fn get_thumbnail(
598 &self,
599 event_content: &impl MediaEventContent,
600 settings: MediaThumbnailSettings,
601 use_cache: bool,
602 ) -> Result<Option<Vec<u8>>> {
603 let Some(source) = event_content.thumbnail_source() else { return Ok(None) };
604 let thumbnail = self
605 .get_media_content(
606 &MediaRequestParameters { source, format: MediaFormat::Thumbnail(settings) },
607 use_cache,
608 )
609 .await?;
610 Ok(Some(thumbnail))
611 }
612
613 /// Remove the thumbnail of the given media event content from the cache.
614 ///
615 /// This is a convenience method that calls the
616 /// [`remove_media_content`](#method.remove_media_content) method.
617 ///
618 /// # Arguments
619 ///
620 /// - `event_content` - The media event content.
621 /// - `size` - The _desired_ settings of the thumbnail. Must match the
622 /// settings requested with [`get_thumbnail`](#method.get_thumbnail).
623 pub async fn remove_thumbnail(
624 &self,
625 event_content: &impl MediaEventContent,
626 settings: MediaThumbnailSettings,
627 ) -> Result<()> {
628 if let Some(source) = event_content.source() {
629 self.remove_media_content(&MediaRequestParameters {
630 source,
631 format: MediaFormat::Thumbnail(settings),
632 })
633 .await?
634 }
635
636 Ok(())
637 }
638
639 /// Get a preview for a URL, as OpenGraph-like data.
640 ///
641 /// This is generated by the homeserver, which fetches the URL itself. Note
642 /// that servers may disable this endpoint entirely, in which case this
643 /// returns an error, and that using it in an encrypted room discloses the
644 /// URL to the homeserver.
645 ///
646 /// Uses the authenticated endpoint when the homeserver supports it, falling
647 /// back to the deprecated unauthenticated one otherwise.
648 ///
649 /// # Arguments
650 ///
651 /// - `url` - The URL to get a preview of.
652 /// - `ts` - The preferred point in time to return a preview for, if the
653 /// homeserver supports returning previews for a given point in time.
654 ///
655 /// # Returns
656 ///
657 /// The OpenGraph-like data for the URL, if the homeserver returned any. It
658 /// is returned as raw JSON, since the fields are not a fixed set: they
659 /// mirror OpenGraph, with the addition of `matrix:image:size` for the image
660 /// size in bytes, and `og:image` holding an MXC URI rather than an HTTP
661 /// URL.
662 ///
663 /// # Examples
664 ///
665 /// ```no_run
666 /// # use matrix_sdk::Client;
667 /// # use url::Url;
668 /// # async {
669 /// # let homeserver = Url::parse("http://localhost:8080")?;
670 /// # let client = Client::new(homeserver).await?;
671 /// if let Some(data) =
672 /// client.media().get_media_preview("https://matrix.org", None).await?
673 /// {
674 /// println!("Preview data: {}", data.get());
675 /// }
676 /// # anyhow::Ok(()) };
677 /// ```
678 pub async fn get_media_preview(
679 &self,
680 url: &str,
681 ts: Option<MilliSecondsSinceUnixEpoch>,
682 ) -> Result<Option<Box<RawJsonValue>>> {
683 // Use the authenticated endpoint when the server supports it.
684 let supported_versions = self.client.supported_versions().await?;
685
686 let use_auth = authenticated_media::get_media_preview::v1::Request::PATH_BUILDER
687 .is_supported(&supported_versions);
688
689 if use_auth {
690 let mut request =
691 authenticated_media::get_media_preview::v1::Request::new(url.to_owned());
692 request.ts = ts;
693
694 Ok(self.client.send(request).await?.data)
695 } else {
696 // The whole block is `allow(deprecated)`: both the endpoint and its
697 // `ts` field are deprecated since Matrix 1.11, and we only reach
698 // this branch when the homeserver is too old for the authenticated
699 // endpoint above.
700 #[allow(deprecated)]
701 {
702 let mut request = media::get_media_preview::v3::Request::new(url.to_owned());
703 request.ts = ts;
704
705 Ok(self.client.send(request).await?.data)
706 }
707 }
708 }
709
710 /// Set the [`MediaRetentionPolicy`] to use for deciding whether to store or
711 /// keep media content.
712 ///
713 /// It is used:
714 ///
715 /// - When a media needs to be cached, to check that it does not exceed the
716 /// max file size.
717 ///
718 /// - When [`Media::clean()`], to check that all media content in the store
719 /// fits those criteria.
720 ///
721 /// To apply the new policy to the media cache right away,
722 /// [`Media::clean()`] should be called after this.
723 ///
724 /// By default, an empty `MediaRetentionPolicy` is used, which means that no
725 /// criteria are applied.
726 ///
727 /// # Arguments
728 ///
729 /// - `policy` - The `MediaRetentionPolicy` to use.
730 pub async fn set_media_retention_policy(&self, policy: MediaRetentionPolicy) -> Result<()> {
731 self.client.media_store().lock().await?.set_media_retention_policy(policy).await?;
732 Ok(())
733 }
734
735 /// Get the current `MediaRetentionPolicy`.
736 pub async fn media_retention_policy(&self) -> Result<MediaRetentionPolicy> {
737 Ok(self.client.media_store().lock().await?.media_retention_policy())
738 }
739
740 /// Clean up the media cache with the current [`MediaRetentionPolicy`].
741 ///
742 /// If there is already an ongoing cleanup, this is a noop.
743 pub async fn clean(&self) -> Result<()> {
744 self.client.media_store().lock().await?.clean().await?;
745 Ok(())
746 }
747
748 /// Upload the file bytes in `data` and return the source information.
749 pub(crate) async fn upload_plain_media_and_thumbnail(
750 &self,
751 content_type: &Mime,
752 data: Vec<u8>,
753 thumbnail: Option<Thumbnail>,
754 send_progress: SharedObservable<TransmissionProgress>,
755 ) -> Result<(MediaSource, Option<(MediaSource, Box<ThumbnailInfo>)>)> {
756 let upload_thumbnail = self.upload_thumbnail(thumbnail, send_progress.clone());
757
758 let upload_attachment = async move {
759 self.upload(content_type, data, None).with_send_progress_observable(send_progress).await
760 };
761
762 let (thumbnail, response) = try_join(upload_thumbnail, upload_attachment).await?;
763
764 Ok((MediaSource::Plain(response.content_uri), thumbnail))
765 }
766
767 /// Uploads an unencrypted thumbnail to the media repository, and returns
768 /// its source and extra information.
769 async fn upload_thumbnail(
770 &self,
771 thumbnail: Option<Thumbnail>,
772 send_progress: SharedObservable<TransmissionProgress>,
773 ) -> Result<Option<(MediaSource, Box<ThumbnailInfo>)>> {
774 let Some(thumbnail) = thumbnail else {
775 return Ok(None);
776 };
777
778 let (data, content_type, thumbnail_info) = thumbnail.into_parts();
779
780 let response = self
781 .upload(&content_type, data, None)
782 .with_send_progress_observable(send_progress)
783 .await?;
784 let url = response.content_uri;
785
786 Ok(Some((MediaSource::Plain(url), thumbnail_info)))
787 }
788
789 /// Create an [`OwnedMxcUri`] for a file or thumbnail we want to store
790 /// locally before sending it.
791 ///
792 /// This uses a MXC ID that is only locally valid.
793 pub(crate) fn make_local_uri(txn_id: &TransactionId) -> OwnedMxcUri {
794 OwnedMxcUri::from(format!("mxc://{LOCAL_MXC_SERVER_NAME}/{txn_id}"))
795 }
796
797 /// Create a [`MediaRequest`] for a file we want to store locally before
798 /// sending it.
799 ///
800 /// This uses a MXC ID that is only locally valid.
801 pub(crate) fn make_local_file_media_request(txn_id: &TransactionId) -> MediaRequestParameters {
802 MediaRequestParameters {
803 source: MediaSource::Plain(Self::make_local_uri(txn_id)),
804 format: MediaFormat::File,
805 }
806 }
807
808 /// Checks whether the MXC represents a local URI.
809 ///
810 /// A local MXC URI is a URI that was generated with
811 /// [`Self::make_local_uri`].
812 fn is_local_uri(source: &MediaSource) -> bool {
813 let uri = match source {
814 MediaSource::Plain(uri) => uri,
815 MediaSource::Encrypted(file) => &file.url,
816 };
817
818 uri.server_name().is_ok_and(|server_name| server_name == LOCAL_MXC_SERVER_NAME)
819 }
820}
821
822/// A [`MediaFetcher`] that uses the default media/authenticated media endpoints
823/// to fetch new media.
824#[derive(Debug, Clone)]
825pub struct DefaultMediaFetcher;
826
827impl MediaFetcher for DefaultMediaFetcher {
828 fn fetch_media_content<'a>(
829 &'a self,
830 client: &'a Client,
831 request: &'a MediaRequestParameters,
832 ) -> BoxFuture<'a, Result<Vec<u8>, Error>> {
833 let request_config = client
834 .request_config()
835 // Downloading a file should have no timeout as we don't know the
836 // network connectivity available for the user or the file size
837 .timeout(Some(Duration::MAX));
838
839 self.fetch_media_content_with_config(client, request, request_config)
840 }
841
842 fn fetch_media_content_with_config<'a>(
843 &'a self,
844 client: &'a Client,
845 request: &'a MediaRequestParameters,
846 request_config: RequestConfig,
847 ) -> BoxFuture<'a, Result<Vec<u8>, Error>> {
848 Box::pin(async move {
849 // Use the authenticated endpoints when the server supports it.
850 let supported_versions = client.supported_versions().await?;
851
852 let use_auth = authenticated_media::get_content::v1::Request::PATH_BUILDER
853 .is_supported(&supported_versions);
854
855 match &request.source {
856 MediaSource::Encrypted(file) => {
857 let content = if use_auth {
858 let request =
859 authenticated_media::get_content::v1::Request::from_uri(&file.url)?;
860 client.send(request).with_request_config(request_config).await?.file
861 } else {
862 #[allow(deprecated)]
863 let request = media::get_content::v3::Request::from_url(&file.url)?;
864 client.send(request).with_request_config(request_config).await?.file
865 };
866
867 #[cfg(feature = "e2e-encryption")]
868 let content = {
869 let content_len = content.len();
870 let mut cursor = std::io::Cursor::new(content);
871 let mut reader = matrix_sdk_base::crypto::AttachmentDecryptor::new(
872 &mut cursor,
873 file.as_ref().clone().into(),
874 )?;
875
876 // Encrypted size should be the same as the decrypted
877 // size, rounded up to a cipher block.
878 let mut decrypted = Vec::with_capacity(content_len);
879
880 reader.read_to_end(&mut decrypted)?;
881
882 decrypted
883 };
884
885 Ok(content)
886 }
887
888 MediaSource::Plain(uri) => {
889 if let MediaFormat::Thumbnail(settings) = &request.format {
890 if use_auth {
891 let mut request =
892 authenticated_media::get_content_thumbnail::v1::Request::from_uri(
893 uri,
894 settings.width,
895 settings.height,
896 )?;
897 request.method = Some(settings.method.clone());
898 request.animated = Some(settings.animated);
899
900 Ok(client.send(request).with_request_config(request_config).await?.file)
901 } else {
902 #[allow(deprecated)]
903 let request = {
904 let mut request =
905 media::get_content_thumbnail::v3::Request::from_url(
906 uri,
907 settings.width,
908 settings.height,
909 )?;
910 request.method = Some(settings.method.clone());
911 request.animated = Some(settings.animated);
912 request
913 };
914
915 Ok(client.send(request).with_request_config(request_config).await?.file)
916 }
917 } else if use_auth {
918 let request = authenticated_media::get_content::v1::Request::from_uri(uri)?;
919 Ok(client.send(request).with_request_config(request_config).await?.file)
920 } else {
921 #[allow(deprecated)]
922 let request = media::get_content::v3::Request::from_url(uri)?;
923 Ok(client.send(request).with_request_config(request_config).await?.file)
924 }
925 }
926 }
927 })
928 }
929}
930
931#[cfg(test)]
932mod tests {
933 use std::ops::Not;
934
935 use ruma::{
936 MxcUri,
937 events::room::{EncryptedFile, MediaSource},
938 mxc_uri, owned_mxc_uri,
939 };
940 use serde_json::json;
941
942 use super::Media;
943
944 /// Create an `EncryptedFile` with the given MXC URI.
945 fn encrypted_file(mxc_uri: &MxcUri) -> Box<EncryptedFile> {
946 Box::new(
947 serde_json::from_value(json!({
948 "url": mxc_uri,
949 "key": {
950 "kty": "oct",
951 "key_ops": ["encrypt", "decrypt"],
952 "alg": "A256CTR",
953 "k": "b50ACIv6LMn9AfMCFD1POJI_UAFWIclxAN1kWrEO2X8",
954 "ext": true,
955 },
956 "iv": "AK1wyzigZtQAAAABAAAAKK",
957 "hashes": {
958 "sha256": "/NogKqW5bz/m8xHgFiH5haFGjCNVmUIPLzfvOhHdrxY",
959 },
960 "v": "v2",
961 }))
962 .unwrap(),
963 )
964 }
965
966 #[test]
967 fn test_make_local_uri() {
968 let txn_id = "abcdef";
969
970 let uri = Media::make_local_uri(txn_id.into());
971 assert_eq!(uri.media_id().unwrap(), txn_id);
972 }
973
974 #[test]
975 fn test_is_local_uri() {
976 let txn_id = "abcdef";
977
978 // Request generated with `make_local_file_media_request`.
979 let request = Media::make_local_file_media_request(txn_id.into());
980 assert!(Media::is_local_uri(&request.source));
981
982 // Local plain source.
983 let source = MediaSource::Plain(Media::make_local_uri(txn_id.into()));
984 assert!(Media::is_local_uri(&source));
985
986 // Local encrypted source.
987 let source = MediaSource::Encrypted(encrypted_file(&Media::make_local_uri(txn_id.into())));
988 assert!(Media::is_local_uri(&source));
989
990 // Test non-local plain source.
991 let source = MediaSource::Plain(owned_mxc_uri!("mxc://server.local/poiuyt"));
992 assert!(Media::is_local_uri(&source).not());
993
994 // Test non-local encrypted source.
995 let source = MediaSource::Encrypted(encrypted_file(mxc_uri!("mxc://server.local/mlkjhg")));
996 assert!(Media::is_local_uri(&source).not());
997
998 // Test invalid MXC URI.
999 let source = MediaSource::Plain("https://server.local/nbvcxw".into());
1000 assert!(Media::is_local_uri(&source).not());
1001 }
1002}