31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | use std::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | borrow::Cow, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | collections::{HashMap, VecDeque}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | io::Read as _, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | panic::AssertUnwindSafe, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | sync::Arc, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 7 | time::Duration, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | use futures_util::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | FutureExt, StreamExt, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 12 | future::{Shared, WeakShared}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | use matrix_sdk::{ |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 15 | Client, Room, SendOutsideWasm, SyncOutsideWasm, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | room::reply::{EnforceThread, Reply as SdkReply}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | ruma::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | api::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | Metadata, OutgoingRequest, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | auth_scheme::SendAccessToken, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | client::{authenticated_media, media}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | events::room::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | MediaSource, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | message::{AddMentions, ReplyWithinThread}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | use matrix_sdk_crypto::{AttachmentDecryptor, MediaEncryptionInfo}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | use ruma::MxcUri; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | use tokio::sync::Mutex; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 34 | use super::executor::EngineBoxFuture; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | AttachmentUpload, Error, ErrorKind, Media, MediaId, MediaKind, MediaSize, TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | normalise::{MediaRegistration, MediaScope}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | upload::prepare_upload, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 41 | /// a stalled download gives up after this long without a byte |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 42 | const DOWNLOAD_IDLE: Duration = Duration::from_secs(60); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 43 | /// the sdk client gives up on any request after thirty seconds, far too soon for a video |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 44 | const DOWNLOAD_TOTAL: Duration = Duration::from_secs(60 * 60); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 46 | type FlightFuture = EngineBoxFuture<'static, Result<Media, Error>>; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | type Flight = Shared<FlightFuture>; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | /// owns everything needed to turn a media id or an mxc uri into bytes, so the |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | /// handle can answer media queries without a trip through the engine |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | pub(crate) struct MediaService { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | registry: Mutex<MediaRegistry>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | flights: Mutex<HashMap<String, WeakShared<FlightFuture>>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | max_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | impl MediaService { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | pub(crate) fn new(client: Client, max_bytes: usize, max_sources: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | registry: Mutex::new(MediaRegistry::new(max_sources)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | flights: Mutex::new(HashMap::new()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | max_bytes, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | pub(crate) async fn register(&self, media: Vec<MediaRegistration>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | if !media.is_empty() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | self.registry.lock().await.insert_all(media); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | pub(crate) async fn release(&self, scope: &MediaScope) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | self.registry.lock().await.release(scope); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | pub(crate) async fn clear(&self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | self.registry.lock().await.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | pub(crate) async fn timeline_media( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | media_id: &MediaId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | size: MediaSize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | ) -> Result<Media, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | let media = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | .registry |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | .lock() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | .get(media_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | .cloned() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | .ok_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | ErrorKind::MediaUnknown, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | "media is not registered in a retained timeline", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | let (source, format, mimetype, declared_size) = match size { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | MediaSize::Full => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | media.full.source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | MediaFormat::File, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | media.full.mimetype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | media.full.declared_size, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | ), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 106 | MediaSize::Thumbnail { width, height } => match media.thumbnail { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | Some(thumbnail) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | thumbnail.source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | MediaFormat::File, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | thumbnail.mimetype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | thumbnail.declared_size, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | // the server can only resize what it can read |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | None if matches!(media.kind, MediaKind::Image | MediaKind::Video) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | && matches!(media.full.source, MediaSource::Plain(_)) => |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 117 | // the server picks the thumbnail's type, so the original's would be a lie |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 118 | let format = if is_local(&media.full.source) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 119 | MediaFormat::File |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 120 | } else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 121 | thumbnail_format(width, height) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 122 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 123 | (media.full.source, format, None, None) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | None => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | ErrorKind::MediaUnavailable, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | "the requested media size is unavailable", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | if declared_size.is_some_and(|size| size > self.max_bytes as u64) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | return Err(too_large("media exceeds the configured byte limit")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 136 | self.fetch_or_file(MediaRequestParameters { source, format }, mimetype) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | pub(crate) async fn mxc_media(&self, uri: &MxcUri, size: MediaSize) -> Result<Media, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | if !uri.is_valid() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | return Err(Error::new(ErrorKind::Invalid, "mxc uri is malformed")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | let format = match size { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | MediaSize::Full => MediaFormat::File, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | MediaSize::Thumbnail { width, height } => thumbnail_format(width, height), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 148 | self.fetch_or_file( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | MediaRequestParameters { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | source: MediaSource::Plain(uri.to_owned()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | format, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 158 | /// plenty of servers won't thumbnail everything, and the file itself is the next best |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 159 | /// thing. the byte cap still holds either way |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 160 | async fn fetch_or_file( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 161 | &self, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 162 | request: MediaRequestParameters, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 163 | mimetype: Option<String>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 164 | ) -> Result<Media, Error> { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 165 | if !matches!(request.format, MediaFormat::Thumbnail(_)) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 166 | return self.fetch(request, mimetype).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 167 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 168 | match self.fetch(request.clone(), mimetype.clone()).await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 169 | Err(error) if error.kind == ErrorKind::MediaUnavailable => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 170 | let file = MediaRequestParameters { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 171 | source: request.source, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 172 | format: MediaFormat::File, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 173 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 174 | self.fetch(file, mimetype).await |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 175 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 176 | result => result, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 177 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 178 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 179 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | async fn fetch( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | request: MediaRequestParameters, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | mimetype: Option<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | ) -> Result<Media, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 185 | let key = flight_key(&request); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | let flight = { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | let mut flights = self.flights.lock().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | flights.retain(|_, flight| flight.upgrade().is_some()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | match flights.get(&key).and_then(WeakShared::upgrade) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | Some(flight) => flight, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | None => { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 192 | let prepared = AssertUnwindSafe(fetch( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | self.client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | mimetype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | self.max_bytes, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | )) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | .catch_unwind() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | .map(|result| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | result.unwrap_or_else(|_| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | Err(Error::new(ErrorKind::Other, "media fetch panicked")) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | }) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 203 | }); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 204 | let boxed: FlightFuture = Box::pin(prepared); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 205 | let flight: Flight = boxed.shared(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | if let Some(weak) = flight.downgrade() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | flights.insert(key, weak); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | flight |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | flight.await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | fn thumbnail_format(width: u32, height: u32) -> MediaFormat { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | MediaFormat::Thumbnail(MediaThumbnailSettings::new(width.into(), height.into())) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 221 | fn is_local(source: &MediaSource) -> bool { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 222 | let uri = match source { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 223 | MediaSource::Plain(uri) => uri, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 224 | MediaSource::Encrypted(file) => &file.url, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 225 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 226 | uri.server_name() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 227 | .is_ok_and(|server| server.as_str() == "send-queue.localhost") |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 228 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 229 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 230 | /// plain and encrypted sources share a cache key in the sdk, but must never share a flight |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | fn flight_key(request: &MediaRequestParameters) -> String { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | let uri = match &request.source { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 233 | MediaSource::Plain(uri) => format!("plain|{uri}"), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 234 | MediaSource::Encrypted(file) => format!("encrypted|{}", file.url), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | match &request.format { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | MediaFormat::File => uri.to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | MediaFormat::Thumbnail(settings) => format!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | "{uri}|{}|{}x{}|{}", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | settings.method, settings.width, settings.height, settings.animated |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | fn too_large(message: &'static str) -> Error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | Error::new(ErrorKind::MediaTooLarge, message) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | async fn fetch( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | request: MediaRequestParameters, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | mimetype: Option<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | max_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | ) -> Result<Media, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | if let Some(cached) = cached_media(&client, &request).await? { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | if cached.len() > max_bytes { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | if let Ok(store) = client.media_store().lock().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | let _ = store.remove_media_content(&request).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | return Err(too_large("cached media exceeds the configured byte limit")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 262 | // plain and encrypted share a cache key, so a plain fetch of an encrypted file's uri |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 263 | // leaves ciphertext here. its hash gives it away |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 264 | if is_ciphertext(&cached, &request.source) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 265 | let bytes = maybe_decrypt(cached, &request, max_bytes)?; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 266 | cache(&client, &request, &bytes).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 267 | return Ok(media(bytes, mimetype)); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 268 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | return Ok(media(cached, mimetype)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | let (bytes, served_mimetype) = download_bounded(&client, &request, max_bytes).await?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | // the sdk caches plaintext under the encrypted key, so we do too or nobody can read anything |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | let bytes = maybe_decrypt(bytes, &request, max_bytes)?; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 274 | cache(&client, &request, &bytes).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 275 | Ok(media(bytes, mimetype.or(served_mimetype))) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 276 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 277 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 278 | async fn cache(client: &Client, request: &MediaRequestParameters, bytes: &[u8]) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | if let Ok(store) = client.media_store().lock().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | let _ = store |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | .add_media_content( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 282 | request, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 283 | bytes.to_vec(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | matrix_sdk::media::store::IgnoreMediaRetentionPolicy::No, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 288 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 289 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 290 | fn is_ciphertext(bytes: &[u8], source: &MediaSource) -> bool { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 291 | use sha2::{Digest, Sha256}; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 292 | let MediaSource::Encrypted(file) = source else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 293 | return false; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 294 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 295 | file.hashes |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 296 | .get(&ruma::events::room::EncryptedFileHashAlgorithm::Sha256) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 297 | .is_some_and(|hash| hash.as_bytes() == Sha256::digest(bytes).as_slice()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | fn media(bytes: Vec<u8>, mimetype: Option<String>) -> Media { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | let mimetype = mimetype |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | .or_else(|| sniff_image(&bytes).map(str::to_owned)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | .unwrap_or_else(|| "application/octet-stream".to_owned()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | Media { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | bytes: Arc::from(bytes), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | mimetype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | /// the cache forgets content types, and avatars never declare one. images are the common case |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | fn sniff_image(bytes: &[u8]) -> Option<&'static str> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | match bytes { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | [0x89, b'P', b'N', b'G', ..] => Some("image/png"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | [0xff, 0xd8, 0xff, ..] => Some("image/jpeg"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | [b'G', b'I', b'F', b'8', ..] => Some("image/gif"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | b'R', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | b'I', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | b'F', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | b'F', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | _, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | _, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | _, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | _, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | b'W', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | b'E', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | b'B', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | b'P', |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | .., |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | ] => Some("image/webp"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | _ => None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | async fn cached_media( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | request: &MediaRequestParameters, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | ) -> Result<Option<Vec<u8>>, Error> { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 339 | fn cache_failed( |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 340 | source: impl std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 341 | ) -> Error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | Error::with_source(ErrorKind::Store, "reading media cache failed", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | .media_store() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | .lock() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | .map_err(cache_failed)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | .get_media_content(request) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | .map_err(cache_failed) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | fn maybe_decrypt( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | bytes: Vec<u8>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | request: &MediaRequestParameters, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | max_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | ) -> Result<Vec<u8>, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | let MediaSource::Encrypted(file) = &request.source else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | return Ok(bytes); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | }; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 362 | fn decrypt_failed( |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 363 | source: impl std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 364 | ) -> Error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | ErrorKind::MediaUnavailable, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | "media decryption failed", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | let info: MediaEncryptionInfo = (**file).clone().into(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | let mut cursor = std::io::Cursor::new(bytes); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | let mut decryptor = AttachmentDecryptor::new(&mut cursor, info).map_err(decrypt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | let mut decrypted = Vec::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | let mut chunk = [0_u8; 8192]; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | let read = decryptor.read(&mut chunk).map_err(decrypt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | if read == 0 { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | return Ok(decrypted); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | if read > max_bytes.saturating_sub(decrypted.len()) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | return Err(too_large( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | "decrypted media exceeds the configured byte limit", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | decrypted.extend_from_slice(&chunk[..read]); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 390 | /// the sdk's own download has no size cap, so we do it by hand |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | async fn download_bounded( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | request: &MediaRequestParameters, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | max_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | ) -> Result<(Vec<u8>, Option<String>), Error> { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 396 | if is_local(&request.source) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | ErrorKind::MediaUnavailable, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | "local media is not in the media cache", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | let network_failed = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | |source| Error::with_source(ErrorKind::Network, "media download failed", source); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 404 | let mut refreshed = false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | let response = loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | let http_request = media_http_request(client, request).await?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | let (parts, _) = http_request.into_parts(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 408 | let mut builder = client |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 409 | .http_client() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 410 | .get(parts.uri.to_string()) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 411 | .timeout(DOWNLOAD_TOTAL); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | for (name, value) in &parts.headers { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | builder = builder.header(name.as_str(), value.as_bytes()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | let response = builder.send().await.map_err(network_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | if response.status() != matrix_sdk::reqwest::StatusCode::UNAUTHORIZED { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | break response; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 419 | if refreshed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | || client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | .session_tokens() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | .is_none_or(|tokens| tokens.refresh_token.is_none()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | ErrorKind::Unauthenticated, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 426 | "media download was not authorized", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | client.refresh_access_token().await.map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | Error::from_refresh_token("refreshing media access token failed", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 432 | refreshed = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 433 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 434 | let status = response.status(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | if !status.is_success() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 436 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 437 | ErrorKind::MediaUnavailable, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | format!("media download failed with status {status}"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 439 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 440 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 441 | if response |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 442 | .content_length() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 443 | .is_some_and(|length| length > max_bytes as u64) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 444 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 445 | return Err(too_large("media exceeds the configured byte limit")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 446 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 447 | let mimetype = response |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 448 | .headers() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 449 | .get(http::header::CONTENT_TYPE) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 450 | .and_then(|value| value.to_str().ok()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 451 | .map(str::to_owned); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 452 | let mut stream = response.bytes_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 453 | let mut bytes = Vec::new(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 454 | loop { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 455 | let chunk = matrix_sdk::timeout::timeout(stream.next(), DOWNLOAD_IDLE) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 456 | .await |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 457 | .map_err(|_| Error::new(ErrorKind::Network, "media download stalled"))?; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 458 | let Some(chunk) = chunk else { break }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | let chunk = chunk.map_err(network_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 460 | if chunk.len() > max_bytes.saturating_sub(bytes.len()) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 461 | return Err(too_large( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 462 | "downloaded media exceeds the configured byte limit", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 463 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 464 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 465 | bytes.extend_from_slice(&chunk); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 466 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 467 | Ok((bytes, mimetype)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 468 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 469 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 470 | async fn media_http_request( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 471 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 472 | request: &MediaRequestParameters, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 473 | ) -> Result<http::Request<Vec<u8>>, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 474 | let versions = client.supported_versions().await.map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 475 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 476 | ErrorKind::Network, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 477 | "reading supported server versions failed", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 478 | source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 479 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 480 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 481 | let authenticated = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 482 | authenticated_media::get_content::v1::Request::PATH_BUILDER.is_supported(&versions); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 483 | let base = client.homeserver().to_string(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 484 | let token = client.access_token(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 485 | let invalid_uri = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 486 | |source| Error::with_source(ErrorKind::Invalid, "media URI is invalid", source); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 487 | let (uri, thumbnail) = match (&request.source, &request.format) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 488 | (MediaSource::Encrypted(file), _) => (&file.url, None), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 489 | (MediaSource::Plain(uri), MediaFormat::File) => (uri, None), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 490 | (MediaSource::Plain(uri), MediaFormat::Thumbnail(settings)) => (uri, Some(settings)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 491 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 492 | // each endpoint has its own auth input type, so no generic helper can take them all |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 493 | macro_rules! into_http { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 494 | ($endpoint:expr, $token:expr) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 495 | $endpoint |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 496 | .try_into_http_request::<Vec<u8>>(&base, $token, Cow::Borrowed(&versions)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 497 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 498 | Error::with_source(ErrorKind::Other, "media request is invalid", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 499 | })? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 500 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 501 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 502 | let authed = SendAccessToken::IfRequired(token.as_deref().unwrap_or("")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 503 | #[allow(deprecated)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 504 | let mut http_request = match (thumbnail, authenticated) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 505 | (None, true) => into_http!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 506 | authenticated_media::get_content::v1::Request::from_uri(uri).map_err(invalid_uri)?, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 507 | authed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 508 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 509 | (None, false) => into_http!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 510 | media::get_content::v3::Request::from_url(uri).map_err(invalid_uri)?, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 511 | SendAccessToken::None |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | (Some(settings), true) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 514 | let mut endpoint = authenticated_media::get_content_thumbnail::v1::Request::from_uri( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 515 | uri, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 516 | settings.width, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 517 | settings.height, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 518 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 519 | .map_err(invalid_uri)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 520 | endpoint.method = Some(settings.method.clone()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 521 | endpoint.animated = Some(settings.animated); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 522 | into_http!(endpoint, authed) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 523 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 524 | (Some(settings), false) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 525 | let mut endpoint = media::get_content_thumbnail::v3::Request::from_url( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 526 | uri, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 527 | settings.width, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 528 | settings.height, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 529 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 530 | .map_err(invalid_uri)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 531 | endpoint.method = Some(settings.method.clone()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 532 | endpoint.animated = Some(settings.animated); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 533 | into_http!(endpoint, SendAccessToken::None) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 534 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 535 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 536 | // legacy endpoints don't ask for a token but plenty of servers want one anyway |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 537 | if let Some(token) = token.as_deref() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 538 | let value = format!("Bearer {token}").parse().map_err(|_| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 539 | Error::new(ErrorKind::Unauthenticated, "stored access token is invalid") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 540 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 541 | http_request |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 542 | .headers_mut() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 543 | .insert(http::header::AUTHORIZATION, value); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 544 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 545 | Ok(http_request) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 546 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 547 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 548 | pub(crate) async fn queue_upload( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 549 | room: Room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 550 | upload: AttachmentUpload, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 551 | max_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 552 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 553 | let (upload, mime, mut config) = prepare_upload(upload, max_bytes)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 554 | if let TimelineId::Thread { root_event_id, .. } = &upload.timeline_id { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 555 | config.reply = Some(SdkReply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 556 | event_id: root_event_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 557 | enforce_thread: EnforceThread::Threaded(ReplyWithinThread::No), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 558 | add_mentions: AddMentions::No, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 559 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 560 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 561 | room.send_queue() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 562 | .send_attachment(upload.filename, mime, upload.bytes, config) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 563 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 564 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 565 | .map_err(|source| { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 566 | use matrix_sdk::send_queue::RoomSendQueueError; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 567 | let kind = match &source { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 568 | RoomSendQueueError::RoomNotJoined | RoomSendQueueError::RoomDisappeared => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 569 | ErrorKind::RoomUnknown |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 570 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 571 | RoomSendQueueError::StorageError(_) => ErrorKind::Store, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 572 | _ => ErrorKind::MediaUnavailable, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 573 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 574 | Error::with_source(kind, "media upload could not be queued", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 575 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 576 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 577 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 578 | /// least recently seen goes first. stale order slots are skipped by stamp rather than hunted down |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 579 | struct MediaRegistry { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 580 | entries: HashMap<MediaId, (MediaRegistration, u64)>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 581 | order: VecDeque<(MediaId, u64)>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 582 | next_stamp: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 583 | limit: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 584 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 585 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 586 | impl MediaRegistry { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 587 | fn new(limit: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 588 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 589 | entries: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 590 | order: VecDeque::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 591 | next_stamp: 0, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 592 | limit, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 593 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 594 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 595 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 596 | fn get(&self, id: &MediaId) -> Option<&MediaRegistration> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 597 | self.entries.get(id).map(|(media, _)| media) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 598 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 599 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 600 | fn insert_all(&mut self, media: Vec<MediaRegistration>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 601 | for media in media { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 602 | let stamp = self.next_stamp; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 603 | self.next_stamp += 1; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 604 | self.order.push_back((media.id.clone(), stamp)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 605 | self.entries.insert(media.id.clone(), (media, stamp)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 606 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 607 | while self.entries.len() > self.limit { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 608 | let Some((id, stamp)) = self.order.pop_front() else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 609 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 610 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 611 | if self.is_current(&id, stamp) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 612 | self.entries.remove(&id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 613 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 614 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 615 | if self.order.len() > self.limit.saturating_mul(4) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 616 | let entries = &self.entries; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 617 | self.order |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 618 | .retain(|(id, stamp)| entries.get(id).is_some_and(|(_, current)| current == stamp)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 619 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 620 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 621 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 622 | fn is_current(&self, id: &MediaId, stamp: u64) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 623 | self.entries |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 624 | .get(id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 625 | .is_some_and(|(_, current)| *current == stamp) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 626 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 627 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 628 | fn release(&mut self, scope: &MediaScope) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 629 | self.entries.retain(|id, _| !scope.owns(id)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 630 | let entries = &self.entries; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 631 | self.order.retain(|(id, _)| entries.contains_key(id)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 632 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 633 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 634 | fn clear(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 635 | self.entries.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 636 | self.order.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 637 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 638 | } |