mirror of
https://github.com/youwen5/iamb.git
synced 2025-06-20 05:39:52 -07:00
Support uploading image attachments from clipboard (#36)
This commit is contained in:
parent
ad8b4a60d2
commit
2899d4f45a
4 changed files with 197 additions and 1 deletions
|
@ -192,6 +192,7 @@ pub enum RoomAction {
|
|||
pub enum SendAction {
|
||||
Submit,
|
||||
Upload(String),
|
||||
UploadImage(usize, usize, Cow<'static, [u8]>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -360,6 +361,12 @@ pub enum IambError {
|
|||
|
||||
#[error("Verification request error: {0}")]
|
||||
VerificationRequestError(#[from] matrix_sdk::encryption::identities::RequestVerificationError),
|
||||
|
||||
#[error("Image error: {0}")]
|
||||
Image(#[from] image::ImageError),
|
||||
|
||||
#[error("Could not use system clipboard data")]
|
||||
Clipboard,
|
||||
}
|
||||
|
||||
impl From<IambError> for UIError<IambInfo> {
|
||||
|
|
|
@ -4,6 +4,7 @@ use std::fs;
|
|||
use std::ops::Deref;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use modalkit::editing::store::RegisterError;
|
||||
use tokio;
|
||||
|
||||
use matrix_sdk::{
|
||||
|
@ -471,6 +472,36 @@ impl ChatState {
|
|||
let msg = MessageType::Text(msg);
|
||||
let msg = RoomMessageEventContent::new(msg);
|
||||
|
||||
(resp.event_id, msg)
|
||||
},
|
||||
SendAction::UploadImage(width, height, bytes) => {
|
||||
// Convert to png because arboard does not give us the mime type.
|
||||
let bytes =
|
||||
image::ImageBuffer::from_raw(width as _, height as _, bytes.into_owned())
|
||||
.ok_or(IambError::Clipboard)
|
||||
.and_then(|imagebuf| {
|
||||
let dynimage = image::DynamicImage::ImageRgba8(imagebuf);
|
||||
let bytes = Vec::<u8>::new();
|
||||
let mut buff = std::io::Cursor::new(bytes);
|
||||
dynimage.write_to(&mut buff, image::ImageOutputFormat::Png)?;
|
||||
Ok(buff.into_inner())
|
||||
})
|
||||
.map_err(IambError::from)?;
|
||||
let mime = mime::IMAGE_PNG;
|
||||
|
||||
let name = Cow::from(format!("Clipboard.png"));
|
||||
let config = AttachmentConfig::new();
|
||||
|
||||
let resp = room
|
||||
.send_attachment(name.as_ref(), &mime, bytes.as_ref(), config)
|
||||
.await
|
||||
.map_err(IambError::from)?;
|
||||
|
||||
// Mock up the local echo message for the scrollback.
|
||||
let msg = TextMessageEventContent::plain(format!("[Attached File: {name}]"));
|
||||
let msg = MessageType::Text(msg);
|
||||
let msg = RoomMessageEventContent::new(msg);
|
||||
|
||||
(resp.event_id, msg)
|
||||
},
|
||||
};
|
||||
|
@ -617,6 +648,15 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
|||
// Run command again.
|
||||
delegate!(self, w => w.editor_command(act, ctx, store))
|
||||
},
|
||||
Err(EditError::Register(RegisterError::ClipboardImage(data))) => {
|
||||
let msg = "Do you really want to upload the image from your system clipboard?";
|
||||
let send =
|
||||
IambAction::Send(SendAction::UploadImage(data.width, data.height, data.bytes));
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(send)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
Err(EditError::NeedConfirm(prompt))
|
||||
},
|
||||
res @ Err(_) => res,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue