ui: Take a PathBuf to send an attachment

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 <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2024-05-15 10:50:26 +02:00
parent da249bbc51
commit 32c7a1ab78
3 changed files with 23 additions and 13 deletions

View File

@@ -0,0 +1,10 @@
# unreleased
Breaking changes:
- `Timeline::send_attachment` now takes an `impl Into<PathBuf>` for the path of
the file to send.
# 0.7.0
Initial release

View File

@@ -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)?;

View File

@@ -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<PathBuf>,
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.