Skip to content

Commit d0257d1

Browse files
zecakehbnjbvr
authored andcommitted
refactor(media): Add method to split Thumbnail into parts
Signed-off-by: Kévin Commaille <[email protected]>
1 parent ecf4434 commit d0257d1

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

crates/matrix-sdk/src/attachment.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ pub struct Thumbnail {
180180
pub info: Option<BaseThumbnailInfo>,
181181
}
182182

183+
impl Thumbnail {
184+
/// Convert this `Thumbnail` into a `(data, content_type, info)` tuple.
185+
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+
);
193+
(self.data, self.content_type, Box::new(thumbnail_info))
194+
}
195+
}
196+
183197
/// Configuration for sending an attachment.
184198
#[derive(Debug, Default)]
185199
pub struct AttachmentConfig {

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -485,20 +485,15 @@ impl Client {
485485
return Ok(None);
486486
};
487487

488-
let mut cursor = Cursor::new(thumbnail.data);
488+
let (data, content_type, thumbnail_info) = thumbnail.into_parts();
489+
let mut cursor = Cursor::new(data);
489490

490491
let file = self
491-
.upload_encrypted_file(&thumbnail.content_type, &mut cursor)
492+
.upload_encrypted_file(&content_type, &mut cursor)
492493
.with_send_progress_observable(send_progress)
493494
.await?;
494495

495-
#[rustfmt::skip]
496-
let thumbnail_info =
497-
assign!(thumbnail.info.map(ThumbnailInfo::from).unwrap_or_default(), {
498-
mimetype: Some(thumbnail.content_type.as_ref().to_owned())
499-
});
500-
501-
Ok(Some((MediaSource::Encrypted(Box::new(file)), Box::new(thumbnail_info))))
496+
Ok(Some((MediaSource::Encrypted(Box::new(file)), thumbnail_info)))
502497
}
503498

504499
/// Claim one-time keys creating new Olm sessions.

crates/matrix-sdk/src/media.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -669,20 +669,14 @@ impl Media {
669669
return Ok(None);
670670
};
671671

672+
let (data, content_type, thumbnail_info) = thumbnail.into_parts();
673+
672674
let response = self
673-
.upload(&thumbnail.content_type, thumbnail.data, None)
675+
.upload(&content_type, data, None)
674676
.with_send_progress_observable(send_progress)
675677
.await?;
676678
let url = response.content_uri;
677679

678-
let thumbnail_info = assign!(
679-
thumbnail.info
680-
.as_ref()
681-
.map(|info| ThumbnailInfo::from(info.clone()))
682-
.unwrap_or_default(),
683-
{ mimetype: Some(thumbnail.content_type.as_ref().to_owned()) }
684-
);
685-
686-
Ok(Some((MediaSource::Plain(url), Box::new(thumbnail_info))))
680+
Ok(Some((MediaSource::Plain(url), thumbnail_info)))
687681
}
688682
}

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ use matrix_sdk_base::{
2424
};
2525
use mime::Mime;
2626
use ruma::{
27-
assign,
2827
events::{
2928
room::{
3029
message::{FormattedBody, MessageType, RoomMessageEventContent},
31-
MediaSource, ThumbnailInfo,
30+
MediaSource,
3231
},
3332
AnyMessageLikeEventContent,
3433
},
@@ -183,13 +182,16 @@ impl RoomSendQueue {
183182

184183
// Process the thumbnail, if it's been provided.
185184
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+
186189
// Normalize information to retrieve the thumbnail in the cache store.
187-
let info = thumbnail.info.as_ref();
188-
let height = info.and_then(|info| info.height).unwrap_or_else(|| {
190+
let height = thumbnail_info.height.unwrap_or_else(|| {
189191
trace!("thumbnail height is unknown, using 0 for the cache entry");
190192
uint!(0)
191193
});
192-
let width = info.and_then(|info| info.width).unwrap_or_else(|| {
194+
let width = thumbnail_info.width.unwrap_or_else(|| {
193195
trace!("thumbnail width is unknown, using 0 for the cache entry");
194196
uint!(0)
195197
});
@@ -201,25 +203,17 @@ impl RoomSendQueue {
201203
let thumbnail_media_request =
202204
make_local_thumbnail_media_request(&txn, height, width);
203205
cache_store
204-
.add_media_content(&thumbnail_media_request, thumbnail.data.clone())
206+
.add_media_content(&thumbnail_media_request, data)
205207
.await
206208
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;
207209

208-
// Create the information required for filling the thumbnail section of the
209-
// media event.
210-
let thumbnail_info = Box::new(
211-
assign!(thumbnail.info.map(ThumbnailInfo::from).unwrap_or_default(), {
212-
mimetype: Some(thumbnail.content_type.as_ref().to_owned())
213-
}),
214-
);
215-
216210
(
217211
Some(txn.clone()),
218212
Some((thumbnail_media_request.source.clone(), thumbnail_info)),
219213
Some((
220214
FinishUploadThumbnailInfo { txn, width, height },
221215
thumbnail_media_request,
222-
thumbnail.content_type,
216+
content_type,
223217
)),
224218
)
225219
} else {

0 commit comments

Comments
 (0)