Skip to content

Commit 12cbff2

Browse files
authored
refactor(sdk): Use spawn_blocking when generating attachment thumbnails
1 parent bdab40a commit 12cbff2

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -687,12 +687,26 @@ impl Joined {
687687
#[cfg(feature = "image-proc")]
688688
let data_slot;
689689
#[cfg(feature = "image-proc")]
690-
let thumbnail = if config.generate_thumbnail {
691-
match generate_image_thumbnail(
692-
content_type,
693-
Cursor::new(&data),
694-
config.thumbnail_size,
695-
) {
690+
let (data, thumbnail) = if config.generate_thumbnail {
691+
let content_type = content_type.clone();
692+
let make_thumbnail = move |data| {
693+
let res = generate_image_thumbnail(
694+
&content_type,
695+
Cursor::new(&data),
696+
config.thumbnail_size,
697+
);
698+
(data, res)
699+
};
700+
701+
#[cfg(not(target_arch = "wasm32"))]
702+
let (data, res) = tokio::task::spawn_blocking(move || make_thumbnail(data))
703+
.await
704+
.expect("Task join error");
705+
706+
#[cfg(target_arch = "wasm32")]
707+
let (data, res) = make_thumbnail(data);
708+
709+
let thumbnail = match res {
696710
Ok((thumbnail_data, thumbnail_info)) => {
697711
data_slot = thumbnail_data;
698712
Some(Thumbnail {
@@ -705,9 +719,11 @@ impl Joined {
705719
ImageError::ThumbnailBiggerThanOriginal | ImageError::FormatNotSupported,
706720
) => None,
707721
Err(error) => return Err(error.into()),
708-
}
722+
};
723+
724+
(data, thumbnail)
709725
} else {
710-
None
726+
(data, None)
711727
};
712728

713729
let config = AttachmentConfig {

0 commit comments

Comments
 (0)