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