Skip to main content

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