From 32c7a1ab78ad537e47ea06cf0cd75ccc562ed0d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 15 May 2024 10:50:26 +0200 Subject: [PATCH] ui: Take a PathBuf to send an attachment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I found the previous API confusing. Firstly, the parameter name was "filename" when we want a file path. Secondly, we take a `String` when we want a `Path`. Signed-off-by: Kévin Commaille --- crates/matrix-sdk-ui/CHANGELOG.md | 10 ++++++++++ crates/matrix-sdk-ui/src/timeline/futures.rs | 18 +++++++++--------- crates/matrix-sdk-ui/src/timeline/mod.rs | 8 ++++---- 3 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 crates/matrix-sdk-ui/CHANGELOG.md diff --git a/crates/matrix-sdk-ui/CHANGELOG.md b/crates/matrix-sdk-ui/CHANGELOG.md new file mode 100644 index 000000000..817cbcb9f --- /dev/null +++ b/crates/matrix-sdk-ui/CHANGELOG.md @@ -0,0 +1,10 @@ +# unreleased + +Breaking changes: + +- `Timeline::send_attachment` now takes an `impl Into` for the path of + the file to send. + +# 0.7.0 + +Initial release \ No newline at end of file diff --git a/crates/matrix-sdk-ui/src/timeline/futures.rs b/crates/matrix-sdk-ui/src/timeline/futures.rs index 459be113d..694a2203f 100644 --- a/crates/matrix-sdk-ui/src/timeline/futures.rs +++ b/crates/matrix-sdk-ui/src/timeline/futures.rs @@ -1,4 +1,4 @@ -use std::{fs, future::IntoFuture, path::Path}; +use std::{fs, future::IntoFuture, path::PathBuf}; use eyeball::{SharedObservable, Subscriber}; use matrix_sdk::{attachment::AttachmentConfig, TransmissionProgress}; @@ -10,7 +10,7 @@ use super::{Error, Timeline}; pub struct SendAttachment<'a> { timeline: &'a Timeline, - filename: String, + path: PathBuf, mime_type: Mime, config: AttachmentConfig, tracing_span: Span, @@ -20,13 +20,13 @@ pub struct SendAttachment<'a> { impl<'a> SendAttachment<'a> { pub(crate) fn new( timeline: &'a Timeline, - filename: String, + path: PathBuf, mime_type: Mime, config: AttachmentConfig, ) -> Self { Self { timeline, - filename, + path, mime_type, config, tracing_span: Span::current(), @@ -47,18 +47,18 @@ impl<'a> IntoFuture for SendAttachment<'a> { boxed_into_future!(extra_bounds: 'a); fn into_future(self) -> Self::IntoFuture { - let Self { timeline, filename, mime_type, config, tracing_span, send_progress } = self; + let Self { timeline, path, mime_type, config, tracing_span, send_progress } = self; let fut = async move { - let body = Path::new(&filename) + let filename = path .file_name() .ok_or(Error::InvalidAttachmentFileName)? .to_str() - .expect("path was created from UTF-8 string, hence filename part is UTF-8 too"); - let data = fs::read(&filename).map_err(|_| Error::InvalidAttachmentData)?; + .ok_or(Error::InvalidAttachmentFileName)?; + let data = fs::read(&path).map_err(|_| Error::InvalidAttachmentData)?; timeline .room() - .send_attachment(body, &mime_type, data, config) + .send_attachment(filename, &mime_type, data, config) .with_send_progress_observable(send_progress) .await .map_err(|_| Error::FailedSendingAttachment)?; diff --git a/crates/matrix-sdk-ui/src/timeline/mod.rs b/crates/matrix-sdk-ui/src/timeline/mod.rs index 2f0eb016b..1b6d2166e 100644 --- a/crates/matrix-sdk-ui/src/timeline/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/mod.rs @@ -16,7 +16,7 @@ //! //! See [`Timeline`] for details. -use std::{pin::Pin, sync::Arc, task::Poll}; +use std::{path::PathBuf, pin::Pin, sync::Arc, task::Poll}; use eyeball::SharedObservable; use eyeball_im::VectorDiff; @@ -542,7 +542,7 @@ impl Timeline { /// /// # Arguments /// - /// * `filename` - The filename of the file to be sent + /// * `path` - The path of the file to be sent /// /// * `mime_type` - The attachment's mime type /// @@ -553,11 +553,11 @@ impl Timeline { #[instrument(skip_all)] pub fn send_attachment( &self, - filename: String, + path: impl Into, mime_type: Mime, config: AttachmentConfig, ) -> SendAttachment<'_> { - SendAttachment::new(self, filename, mime_type, config) + SendAttachment::new(self, path.into(), mime_type, config) } /// Retry sending a message that previously failed to send.