Skip to content

Commit 9676dae

Browse files
authored
feat(wasm): Remove MediaFileHandle from ffi on Wasm platforms (#5249)
Remove the MediaFileHandle concept from the matrix-sdk-ffi crate on Wasm platforms. File handles are not supported in the browser. Signed-off-by: Daniel Salinas
1 parent 798cece commit 9676dae

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

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

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ use std::{
22
collections::HashMap,
33
fmt::Debug,
44
path::PathBuf,
5-
sync::{Arc, OnceLock, RwLock},
5+
sync::{Arc, OnceLock},
66
time::Duration,
77
};
88

99
use anyhow::{anyhow, Context as _};
1010
use futures_util::pin_mut;
11+
#[cfg(not(target_family = "wasm"))]
12+
use matrix_sdk::media::MediaFileHandle as SdkMediaFileHandle;
1113
use matrix_sdk::{
1214
authentication::oauth::{
1315
AccountManagementActionFull, ClientId, OAuthAuthorizationData, OAuthSession,
1416
},
1517
event_cache::EventCacheError,
16-
media::{
17-
MediaFileHandle as SdkMediaFileHandle, MediaFormat, MediaRequestParameters,
18-
MediaRetentionPolicy, MediaThumbnailSettings,
19-
},
18+
media::{MediaFormat, MediaRequestParameters, MediaRetentionPolicy, MediaThumbnailSettings},
2019
ruma::{
2120
api::client::{
2221
discovery::{
@@ -493,32 +492,6 @@ impl Client {
493492
Ok(())
494493
}
495494

496-
pub async fn get_media_file(
497-
&self,
498-
media_source: Arc<MediaSource>,
499-
filename: Option<String>,
500-
mime_type: String,
501-
use_cache: bool,
502-
temp_dir: Option<String>,
503-
) -> Result<Arc<MediaFileHandle>, ClientError> {
504-
let source = (*media_source).clone();
505-
let mime_type: mime::Mime = mime_type.parse()?;
506-
507-
let handle = self
508-
.inner
509-
.media()
510-
.get_media_file(
511-
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
512-
filename,
513-
&mime_type,
514-
use_cache,
515-
temp_dir,
516-
)
517-
.await?;
518-
519-
Ok(Arc::new(MediaFileHandle::new(handle)))
520-
}
521-
522495
/// Restores the client from a `Session`.
523496
///
524497
/// It reloads the entire set of rooms from the previous session.
@@ -736,6 +709,39 @@ impl Client {
736709
}
737710
}
738711

712+
#[cfg(not(target_family = "wasm"))]
713+
#[matrix_sdk_ffi_macros::export]
714+
impl Client {
715+
/// Retrieves a media file from the media source
716+
///
717+
/// Not available on Wasm platforms, due to lack of accessible file system.
718+
pub async fn get_media_file(
719+
&self,
720+
media_source: Arc<MediaSource>,
721+
filename: Option<String>,
722+
mime_type: String,
723+
use_cache: bool,
724+
temp_dir: Option<String>,
725+
) -> Result<Arc<MediaFileHandle>, ClientError> {
726+
let source = (*media_source).clone();
727+
let mime_type: mime::Mime = mime_type.parse()?;
728+
729+
let handle = self
730+
.inner
731+
.media()
732+
.get_media_file(
733+
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
734+
filename,
735+
&mime_type,
736+
use_cache,
737+
temp_dir,
738+
)
739+
.await?;
740+
741+
Ok(Arc::new(MediaFileHandle::new(handle)))
742+
}
743+
}
744+
739745
impl Client {
740746
/// Whether or not the client's homeserver supports the password login flow.
741747
pub(crate) async fn supports_password_login(&self) -> anyhow::Result<bool> {
@@ -2166,17 +2172,20 @@ fn gen_transaction_id() -> String {
21662172

21672173
/// A file handle that takes ownership of a media file on disk. When the handle
21682174
/// is dropped, the file will be removed from the disk.
2175+
#[cfg(not(target_family = "wasm"))]
21692176
#[derive(uniffi::Object)]
21702177
pub struct MediaFileHandle {
2171-
inner: RwLock<Option<SdkMediaFileHandle>>,
2178+
inner: std::sync::RwLock<Option<SdkMediaFileHandle>>,
21722179
}
21732180

2181+
#[cfg(not(target_family = "wasm"))]
21742182
impl MediaFileHandle {
21752183
fn new(handle: SdkMediaFileHandle) -> Self {
2176-
Self { inner: RwLock::new(Some(handle)) }
2184+
Self { inner: std::sync::RwLock::new(Some(handle)) }
21772185
}
21782186
}
21792187

2188+
#[cfg(not(target_family = "wasm"))]
21802189
#[matrix_sdk_ffi_macros::export]
21812190
impl MediaFileHandle {
21822191
/// Get the media file's path.

0 commit comments

Comments
 (0)