From 94d867470968dcdd930ea56a2e8a6971a41efb0a Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 8 Apr 2026 00:48:51 +0200 Subject: [PATCH] refactor(ffi): Deduplicate thumbnail conversion code --- bindings/matrix-sdk-ffi/src/room/mod.rs | 53 +++++++++++-------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/room/mod.rs b/bindings/matrix-sdk-ffi/src/room/mod.rs index 3e21d7b34..8c0cfc92f 100644 --- a/bindings/matrix-sdk-ffi/src/room/mod.rs +++ b/bindings/matrix-sdk-ffi/src/room/mod.rs @@ -1672,23 +1672,30 @@ impl TryFrom for SdkDraftAttachment { type Error = ClientError; fn try_from(value: DraftAttachment) -> Result { + fn draft_thumbnail( + thumbnail_info: Option, + thumbnail_source: Option, + ) -> Result, ClientError> { + if let Some(info) = thumbnail_info + && let Some(source) = thumbnail_source + { + let (data, filename) = read_upload_source(source)?; + Ok(Some(DraftThumbnail { + filename, + data, + mimetype: info.mimetype, + width: info.width, + height: info.height, + size: info.size, + })) + } else { + Ok(None) + } + } + match value { DraftAttachment::Image { image_info, source, thumbnail_source, .. } => { let (data, filename) = read_upload_source(source)?; - let thumbnail = match (image_info.thumbnail_info, thumbnail_source) { - (Some(info), Some(source)) => { - let (data, filename) = read_upload_source(source)?; - Some(DraftThumbnail { - filename, - data, - mimetype: info.mimetype, - width: info.width, - height: info.height, - size: info.size, - }) - } - _ => None, - }; Ok(Self { filename, content: DraftAttachmentContent::Image { @@ -1698,26 +1705,12 @@ impl TryFrom for SdkDraftAttachment { width: image_info.width, height: image_info.height, blurhash: image_info.blurhash, - thumbnail, + thumbnail: draft_thumbnail(image_info.thumbnail_info, thumbnail_source)?, }, }) } DraftAttachment::Video { video_info, source, thumbnail_source, .. } => { let (data, filename) = read_upload_source(source)?; - let thumbnail = match (video_info.thumbnail_info, thumbnail_source) { - (Some(info), Some(source)) => { - let (data, filename) = read_upload_source(source)?; - Some(DraftThumbnail { - filename, - data, - mimetype: info.mimetype, - width: info.width, - height: info.height, - size: info.size, - }) - } - _ => None, - }; Ok(Self { filename, content: DraftAttachmentContent::Video { @@ -1728,7 +1721,7 @@ impl TryFrom for SdkDraftAttachment { height: video_info.height, duration: video_info.duration, blurhash: video_info.blurhash, - thumbnail, + thumbnail: draft_thumbnail(video_info.thumbnail_info, thumbnail_source)?, }, }) }