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