Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ffi): Generate correct temporary file names for media attachments #1851

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl Client {
pub fn get_media_file(
&self,
media_source: Arc<MediaSource>,
body: Option<String>,
mime_type: String,
) -> Result<Arc<MediaFileHandle>, ClientError> {
let client = self.client.clone();
Expand All @@ -206,6 +207,7 @@ impl Client {
.media()
.get_media_file(
&MediaRequest { source, format: MediaFormat::File },
body,
&mime_type,
true,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ matrix-sdk-common = { version = "0.6.0", path = "../matrix-sdk-common" }
matrix-sdk-indexeddb = { version = "0.2.0", path = "../matrix-sdk-indexeddb", default-features = false, optional = true }
matrix-sdk-sled = { version = "0.2.0", path = "../matrix-sdk-sled", default-features = false, optional = true }
mime = "0.3.16"
mime_guess = "2.0.4"
mime2ext = "0.1.52"
once_cell = { workspace = true }
pin-project-lite = "0.2.9"
rand = { version = "0.8.5", optional = true }
Expand Down
65 changes: 54 additions & 11 deletions crates/matrix-sdk/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ use std::time::Duration;
pub use matrix_sdk_base::media::*;
use mime::Mime;
#[cfg(not(target_arch = "wasm32"))]
use mime_guess;
use mime2ext;
use ruma::{
api::client::media::{create_content, get_content, get_content_thumbnail},
assign,
events::room::MediaSource,
MxcUri,
};
#[cfg(not(target_arch = "wasm32"))]
use tempfile::{Builder as TempFileBuilder, NamedTempFile};
use tempfile::{Builder as TempFileBuilder, NamedTempFile, TempDir};
#[cfg(not(target_arch = "wasm32"))]
use tokio::{fs::File as TokioFile, io::AsyncWriteExt};

Expand Down Expand Up @@ -60,6 +60,10 @@ pub struct Media {
pub struct MediaFileHandle {
/// The temporary file that contains the media.
file: NamedTempFile,
/// An intermediary temporary directory used in certain cases.
///
/// Only stored for its `Drop` semantics.
_directory: Option<TempDir>,
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -140,22 +144,61 @@ impl Media {
pub async fn get_media_file(
&self,
request: &MediaRequest,
body: Option<String>,
content_type: &Mime,
use_cache: bool,
) -> Result<MediaFileHandle> {
let data = self.get_media_content(request, use_cache).await?;

let mut suffix = String::from("");
if let Some(extension) =
mime_guess::get_mime_extensions(content_type).and_then(|a| a.first())
{
suffix = String::from(".") + extension;
}
let inferred_extension = mime2ext::mime2ext(content_type);

let body_path = body.as_ref().map(Path::new);
let filename = body_path.and_then(|f| f.file_name().and_then(|f| f.to_str()));
let filename_with_extension = body_path.and_then(|f| {
if f.extension().is_some() {
f.file_name().and_then(|f| f.to_str())
} else {
None
}
});

let (temp_file, temp_dir) = match (filename, filename_with_extension, inferred_extension) {
// If the body is a file name and has an extension use that
(Some(_), Some(filename_with_extension), Some(_)) => {
// Use an intermediary directory to avoid conflicts
let temp_dir = TempFileBuilder::new().tempdir()?;
let temp_file = TempFileBuilder::new()
.prefix(filename_with_extension)
.rand_bytes(0)
.tempfile_in(&temp_dir)?;
(temp_file, Some(temp_dir))
}
// If the body is a file name but doesn't have an extension try inferring one for it
(Some(filename), None, Some(inferred_extension)) => {
// Use an intermediary directory to avoid conflicts
let temp_dir = TempFileBuilder::new().tempdir()?;
let temp_file = TempFileBuilder::new()
.prefix(filename)
.suffix(&(".".to_owned() + inferred_extension))
.rand_bytes(0)
.tempfile_in(&temp_dir)?;
(temp_file, Some(temp_dir))
}
// If the only thing we have is an inferred extension then use that together with a
// randomly generated file name
(None, None, Some(inferred_extension)) => (
TempFileBuilder::new()
.suffix(&&(".".to_owned() + inferred_extension))
.tempfile()?,
None,
),
// Otherwise just use a completely random file name
_ => (TempFileBuilder::new().tempfile()?, None),
};

let file = TempFileBuilder::new().suffix(&suffix).tempfile()?;
TokioFile::from_std(file.reopen()?).write_all(&data).await?;
TokioFile::from_std(temp_file.reopen()?).write_all(&data).await?;

Ok(MediaFileHandle { file })
Ok(MediaFileHandle { file: temp_file, _directory: temp_dir })
}

/// Get a media file's content.
Expand Down