Skip to content

Commit d4d5f45

Browse files
zecakehbnjbvr
authored andcommitted
feat(media)!: Make all fields of Thumbnail required
It seems sensible to assume that if a client is able to generate a thumbnail, it should be able to get all this information for it too. A thumbnail with no information is not really useful, as we don't know when it could be used instead of the original image. Removes `BaseThumbnailInfo`. Signed-off-by: Kévin Commaille <[email protected]>
1 parent d0257d1 commit d4d5f45

File tree

7 files changed

+62
-273
lines changed

7 files changed

+62
-273
lines changed

bindings/matrix-sdk-ffi/src/ruma.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
use std::{collections::BTreeSet, sync::Arc, time::Duration};
1616

1717
use extension_trait::extension_trait;
18-
use matrix_sdk::attachment::{
19-
BaseAudioInfo, BaseFileInfo, BaseImageInfo, BaseThumbnailInfo, BaseVideoInfo,
20-
};
18+
use matrix_sdk::attachment::{BaseAudioInfo, BaseFileInfo, BaseImageInfo, BaseVideoInfo};
2119
use ruma::{
2220
assign,
2321
events::{
@@ -747,21 +745,6 @@ impl From<ThumbnailInfo> for RumaThumbnailInfo {
747745
}
748746
}
749747

750-
impl TryFrom<&ThumbnailInfo> for BaseThumbnailInfo {
751-
type Error = MediaInfoError;
752-
753-
fn try_from(value: &ThumbnailInfo) -> Result<Self, MediaInfoError> {
754-
let height = UInt::try_from(value.height.ok_or(MediaInfoError::MissingField)?)
755-
.map_err(|_| MediaInfoError::InvalidField)?;
756-
let width = UInt::try_from(value.width.ok_or(MediaInfoError::MissingField)?)
757-
.map_err(|_| MediaInfoError::InvalidField)?;
758-
let size = UInt::try_from(value.size.ok_or(MediaInfoError::MissingField)?)
759-
.map_err(|_| MediaInfoError::InvalidField)?;
760-
761-
Ok(BaseThumbnailInfo { height: Some(height), width: Some(width), size: Some(size) })
762-
}
763-
}
764-
765748
#[derive(Clone, uniffi::Record)]
766749
pub struct NoticeMessageContent {
767750
pub body: String,

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use matrix_sdk::crypto::CollectStrategy;
2424
use matrix_sdk::{
2525
attachment::{
2626
AttachmentConfig, AttachmentInfo, BaseAudioInfo, BaseFileInfo, BaseImageInfo,
27-
BaseThumbnailInfo, BaseVideoInfo, Thumbnail,
27+
BaseVideoInfo, Thumbnail,
2828
},
2929
deserialized_responses::{ShieldState as SdkShieldState, ShieldStateCode},
3030
room::edit::EditedContent as SdkEditedContent,
@@ -53,7 +53,7 @@ use ruma::{
5353
},
5454
AnyMessageLikeEventContent,
5555
},
56-
EventId,
56+
EventId, UInt,
5757
};
5858
use tokio::{
5959
sync::Mutex,
@@ -144,19 +144,26 @@ fn build_thumbnail_info(
144144
let thumbnail_data =
145145
fs::read(thumbnail_url).map_err(|_| RoomError::InvalidThumbnailData)?;
146146

147-
let base_thumbnail_info = BaseThumbnailInfo::try_from(&thumbnail_info)
148-
.map_err(|_| RoomError::InvalidAttachmentData)?;
147+
let height = thumbnail_info
148+
.height
149+
.and_then(|u| UInt::try_from(u).ok())
150+
.ok_or(RoomError::InvalidAttachmentData)?;
151+
let width = thumbnail_info
152+
.width
153+
.and_then(|u| UInt::try_from(u).ok())
154+
.ok_or(RoomError::InvalidAttachmentData)?;
155+
let size = thumbnail_info
156+
.width
157+
.and_then(|u| UInt::try_from(u).ok())
158+
.ok_or(RoomError::InvalidAttachmentData)?;
149159

150160
let mime_str =
151161
thumbnail_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
152162
let mime_type =
153163
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;
154164

155-
let thumbnail = Thumbnail {
156-
data: thumbnail_data,
157-
content_type: mime_type,
158-
info: Some(base_thumbnail_info),
159-
};
165+
let thumbnail =
166+
Thumbnail { data: thumbnail_data, content_type: mime_type, height, width, size };
160167

161168
Ok(AttachmentConfig::with_thumbnail(thumbnail))
162169
}

crates/matrix-sdk/src/attachment.rs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,48 +148,30 @@ impl From<AttachmentInfo> for FileInfo {
148148
}
149149
}
150150

151-
#[derive(Debug, Default, Clone)]
152-
/// Base metadata about a thumbnail.
153-
pub struct BaseThumbnailInfo {
154-
/// The height of the thumbnail in pixels.
155-
pub height: Option<UInt>,
156-
/// The width of the thumbnail in pixels.
157-
pub width: Option<UInt>,
158-
/// The file size of the thumbnail in bytes.
159-
pub size: Option<UInt>,
160-
}
161-
162-
impl From<BaseThumbnailInfo> for ThumbnailInfo {
163-
fn from(info: BaseThumbnailInfo) -> Self {
164-
assign!(ThumbnailInfo::new(), {
165-
height: info.height,
166-
width: info.width,
167-
size: info.size,
168-
})
169-
}
170-
}
171-
172151
/// A thumbnail to upload and send for an attachment.
173152
#[derive(Debug)]
174153
pub struct Thumbnail {
175154
/// The raw bytes of the thumbnail.
176155
pub data: Vec<u8>,
177156
/// The type of the thumbnail, this will be used as the content-type header.
178157
pub content_type: mime::Mime,
179-
/// The metadata of the thumbnail.
180-
pub info: Option<BaseThumbnailInfo>,
158+
/// The height of the thumbnail in pixels.
159+
pub height: UInt,
160+
/// The width of the thumbnail in pixels.
161+
pub width: UInt,
162+
/// The file size of the thumbnail in bytes.
163+
pub size: UInt,
181164
}
182165

183166
impl Thumbnail {
184167
/// Convert this `Thumbnail` into a `(data, content_type, info)` tuple.
185168
pub fn into_parts(self) -> (Vec<u8>, mime::Mime, Box<ThumbnailInfo>) {
186-
let thumbnail_info = assign!(
187-
self.info
188-
.as_ref()
189-
.map(|info| ThumbnailInfo::from(info.clone()))
190-
.unwrap_or_default(),
191-
{ mimetype: Some(self.content_type.to_string()) }
192-
);
169+
let thumbnail_info = assign!(ThumbnailInfo::new(), {
170+
height: Some(self.height),
171+
width: Some(self.width),
172+
size: Some(self.size),
173+
mimetype: Some(self.content_type.to_string())
174+
});
193175
(self.data, self.content_type, Box::new(thumbnail_info))
194176
}
195177
}

crates/matrix-sdk/src/room/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,14 +1949,9 @@ impl Room {
19491949

19501950
// If necessary, store caching data for the thumbnail ahead of time.
19511951
let thumbnail_cache_info = if store_in_cache {
1952-
// Use a small closure returning Option to avoid an unnecessary complicated
1953-
// chain of map/and_then.
1954-
let get_info = || {
1955-
let thumbnail = thumbnail.as_ref()?;
1956-
let info = thumbnail.info.as_ref()?;
1957-
Some((thumbnail.data.clone(), info.height?, info.width?))
1958-
};
1959-
get_info()
1952+
thumbnail
1953+
.as_ref()
1954+
.map(|thumbnail| (thumbnail.data.clone(), thumbnail.height, thumbnail.width))
19601955
} else {
19611956
None
19621957
};

crates/matrix-sdk/src/send_queue/upload.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use ruma::{
3131
},
3232
AnyMessageLikeEventContent,
3333
},
34-
uint, OwnedMxcUri, OwnedTransactionId, TransactionId, UInt,
34+
OwnedMxcUri, OwnedTransactionId, TransactionId, UInt,
3535
};
3636
use tracing::{debug, error, instrument, trace, warn, Span};
3737

@@ -182,23 +182,17 @@ impl RoomSendQueue {
182182

183183
// Process the thumbnail, if it's been provided.
184184
if let Some(thumbnail) = config.thumbnail.take() {
185-
// Create the information required for filling the thumbnail section of the
186-
// media event.
187-
let (data, content_type, thumbnail_info) = thumbnail.into_parts();
188-
189185
// Normalize information to retrieve the thumbnail in the cache store.
190-
let height = thumbnail_info.height.unwrap_or_else(|| {
191-
trace!("thumbnail height is unknown, using 0 for the cache entry");
192-
uint!(0)
193-
});
194-
let width = thumbnail_info.width.unwrap_or_else(|| {
195-
trace!("thumbnail width is unknown, using 0 for the cache entry");
196-
uint!(0)
197-
});
186+
let height = thumbnail.height;
187+
let width = thumbnail.width;
198188

199189
let txn = TransactionId::new();
200190
trace!(upload_thumbnail_txn = %txn, thumbnail_size = ?(height, width), "attachment has a thumbnail");
201191

192+
// Create the information required for filling the thumbnail section of the
193+
// media event.
194+
let (data, content_type, thumbnail_info) = thumbnail.into_parts();
195+
202196
// Cache thumbnail in the cache store.
203197
let thumbnail_media_request =
204198
make_local_thumbnail_media_request(&txn, height, width);
@@ -320,27 +314,18 @@ impl QueueStorage {
320314
let from_req =
321315
make_local_thumbnail_media_request(&info.txn, info.height, info.width);
322316

323-
if info.height == uint!(0) || info.width == uint!(0) {
324-
trace!(from = ?from_req.source, "removing thumbnail with unknown dimension from cache store");
325-
326-
cache_store
327-
.remove_media_content(&from_req)
328-
.await
329-
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;
330-
} else {
331-
trace!(from = ?from_req.source, to = ?new_source, "renaming thumbnail file key in cache store");
332-
333-
// Reuse the same format for the cached thumbnail with the final MXC ID.
334-
let new_format = from_req.format.clone();
335-
336-
cache_store
337-
.replace_media_key(
338-
&from_req,
339-
&MediaRequestParameters { source: new_source, format: new_format },
340-
)
341-
.await
342-
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;
343-
}
317+
trace!(from = ?from_req.source, to = ?new_source, "renaming thumbnail file key in cache store");
318+
319+
// Reuse the same format for the cached thumbnail with the final MXC ID.
320+
let new_format = from_req.format.clone();
321+
322+
cache_store
323+
.replace_media_key(
324+
&from_req,
325+
&MediaRequestParameters { source: new_source, format: new_format },
326+
)
327+
.await
328+
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;
344329
}
345330
}
346331

crates/matrix-sdk/tests/integration/room/attachment/mod.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use std::time::Duration;
22

33
use matrix_sdk::{
4-
attachment::{
5-
AttachmentConfig, AttachmentInfo, BaseImageInfo, BaseThumbnailInfo, BaseVideoInfo,
6-
Thumbnail,
7-
},
4+
attachment::{AttachmentConfig, AttachmentInfo, BaseImageInfo, BaseVideoInfo, Thumbnail},
85
media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings},
96
test_utils::mocks::MatrixMockServer,
107
};
@@ -210,11 +207,9 @@ async fn test_room_attachment_send_info_thumbnail() {
210207
let config = AttachmentConfig::with_thumbnail(Thumbnail {
211208
data: b"Thumbnail".to_vec(),
212209
content_type: mime::IMAGE_JPEG,
213-
info: Some(BaseThumbnailInfo {
214-
height: Some(uint!(360)),
215-
width: Some(uint!(480)),
216-
size: Some(uint!(3600)),
217-
}),
210+
height: uint!(360),
211+
width: uint!(480),
212+
size: uint!(3600),
218213
})
219214
.info(AttachmentInfo::Image(BaseImageInfo {
220215
height: Some(uint!(600)),

0 commit comments

Comments
 (0)