96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | use std::time::Duration; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | attachment::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | AttachmentConfig, AttachmentInfo, BaseAudioInfo, BaseFileInfo, BaseImageInfo, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | BaseVideoInfo, Thumbnail, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | ruma::events::room::message::TextMessageEventContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | use crate::{AttachmentUpload, Error, ErrorKind, UploadDetails}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | pub(crate) fn prepare_upload( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | mut upload: AttachmentUpload, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | max_media_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | ) -> Result<(AttachmentUpload, mime::Mime, AttachmentConfig), Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | if upload.bytes.len() > max_media_bytes { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | ErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | "attachment exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | let mimetype: mime::Mime = upload |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | .mimetype |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | .parse() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | .map_err(|_| Error::new(ErrorKind::Invalid, "media MIME type is invalid"))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | let size = ruma::UInt::try_from(upload.bytes.len()).map_err(|_| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | ErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | "attachment size is not representable", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | let info = match upload.details.as_ref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | Some(UploadDetails::Image { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | width, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | height, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | blurhash, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | }) if mimetype.type_() == mime::IMAGE => AttachmentInfo::Image(BaseImageInfo { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | width: width.map(Into::into), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | height: height.map(Into::into), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | size: Some(size), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | blurhash: blurhash.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | ..Default::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | Some(UploadDetails::Video { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | width, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | height, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | duration_ms, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | blurhash, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | }) if mimetype.type_() == mime::VIDEO => AttachmentInfo::Video(BaseVideoInfo { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | width: width.map(Into::into), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | height: height.map(Into::into), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | duration: duration_ms.map(Duration::from_millis), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | size: Some(size), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | blurhash: blurhash.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | Some(UploadDetails::Audio { duration_ms }) if mimetype.type_() == mime::AUDIO => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | AttachmentInfo::Audio(BaseAudioInfo { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | duration: duration_ms.map(Duration::from_millis), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | size: Some(size), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | ..Default::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | Some(UploadDetails::Voice { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | duration_ms, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | waveform, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | }) if mimetype.type_() == mime::AUDIO => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | if waveform.iter().any(|sample| *sample > 1024) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | ErrorKind::Invalid, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | "voice waveform samples must be at most 1024", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | AttachmentInfo::Voice(BaseAudioInfo { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | duration: Some(Duration::from_millis(*duration_ms)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | size: Some(size), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | waveform: Some( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | waveform |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | .map(|sample| f32::from(*sample) / 1024.0) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | .collect(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | Some(UploadDetails::File) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | if mimetype.type_() != mime::IMAGE |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | && mimetype.type_() != mime::VIDEO |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | && mimetype.type_() != mime::AUDIO => |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | AttachmentInfo::File(BaseFileInfo { size: Some(size) }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | None => match mimetype.type_() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | mime::IMAGE => AttachmentInfo::Image(BaseImageInfo { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | size: Some(size), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | ..Default::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | mime::VIDEO => AttachmentInfo::Video(BaseVideoInfo { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | size: Some(size), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | ..Default::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | mime::AUDIO => AttachmentInfo::Audio(BaseAudioInfo { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | size: Some(size), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | ..Default::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | _ => AttachmentInfo::File(BaseFileInfo { size: Some(size) }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | _ => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | ErrorKind::Invalid, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | "attachment metadata does not match its MIME type", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | if upload.thumbnail.is_some() && mimetype.type_() == mime::AUDIO { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | ErrorKind::Invalid, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | "audio attachments do not support thumbnails", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | let thumbnail = upload |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | .thumbnail |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | .map(|thumbnail| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | if thumbnail.bytes.len() > max_media_bytes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | || thumbnail.width == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | || thumbnail.height == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | return Err(Error::new( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 130 | if thumbnail.bytes.len() > max_media_bytes { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 131 | ErrorKind::MediaTooLarge |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 132 | } else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 133 | ErrorKind::Invalid |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 134 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | "thumbnail size or dimensions are invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | let content_type: mime::Mime = thumbnail |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | .mimetype |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | .parse() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | .map_err(|_| Error::new(ErrorKind::Invalid, "thumbnail MIME type is invalid"))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | if content_type.type_() != mime::IMAGE { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | ErrorKind::Invalid, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | "thumbnail MIME type must be an image", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | let size = ruma::UInt::try_from(thumbnail.bytes.len()).map_err(|_| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | ErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | "thumbnail size is not representable", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | Ok(Thumbnail { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | data: thumbnail.bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | content_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | width: thumbnail.width.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | height: thumbnail.height.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | .transpose()?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | let config = AttachmentConfig::new() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | .info(info) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | .thumbnail(thumbnail) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | .caption(upload.caption.clone().map(TextMessageEventContent::plain)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | Ok((upload, mimetype, config)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | } |