Commit 2899d4f45a

2899d4f45a1e7967fb3153926f1b493d7fbd5e65

parent: ad8b4a60d2

Unregistered key

Benjamin Große <ste3ls@gmail.com> · 2023-05-06 19:56 UTC
committer: <git@ulyssa.dev>

Support uploading image attachments from clipboard (#36)

Layout: unified · split

Cargo.lock +148 −1
@@ -239,6 +239,12 @@ version = "0.6.3"
239239source = "registry+https://github.com/rust-lang/crates.io-index"
240240checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
241241
242[[package]]
243name = "bit_field"
244version = "0.10.2"
245source = "registry+https://github.com/rust-lang/crates.io-index"
246checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
247
242248[[package]]
243249name = "bitflags"
244250version = "1.3.2"
@@ -587,6 +593,17 @@ dependencies = [
587593 "crossbeam-utils",
588594]
589595
596[[package]]
597name = "crossbeam-deque"
598version = "0.8.3"
599source = "registry+https://github.com/rust-lang/crates.io-index"
600checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
601dependencies = [
602 "cfg-if",
603 "crossbeam-epoch",
604 "crossbeam-utils",
605]
606
590607[[package]]
591608name = "crossbeam-epoch"
592609version = "0.9.14"
@@ -650,6 +667,12 @@ dependencies = [
650667 "winapi",
651668]
652669
670[[package]]
671name = "crunchy"
672version = "0.2.2"
673source = "registry+https://github.com/rust-lang/crates.io-index"
674checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
675
653676[[package]]
654677name = "crypto-common"
655678version = "0.1.6"
@@ -1009,6 +1032,22 @@ version = "2.5.3"
10091032source = "registry+https://github.com/rust-lang/crates.io-index"
10101033checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
10111034
1035[[package]]
1036name = "exr"
1037version = "1.6.3"
1038source = "registry+https://github.com/rust-lang/crates.io-index"
1039checksum = "bdd2162b720141a91a054640662d3edce3d50a944a50ffca5313cd951abb35b4"
1040dependencies = [
1041 "bit_field",
1042 "flume",
1043 "half",
1044 "lebe",
1045 "miniz_oxide",
1046 "rayon-core",
1047 "smallvec",
1048 "zune-inflate",
1049]
1050
10121051[[package]]
10131052name = "fancy-regex"
10141053version = "0.7.1"
@@ -1029,6 +1068,19 @@ dependencies = [
10291068 "miniz_oxide",
10301069]
10311070
1071[[package]]
1072name = "flume"
1073version = "0.10.14"
1074source = "registry+https://github.com/rust-lang/crates.io-index"
1075checksum = "1657b4441c3403d9f7b3409e47575237dac27b1b5726df654a6ecbf92f0f7577"
1076dependencies = [
1077 "futures-core",
1078 "futures-sink",
1079 "nanorand",
1080 "pin-project",
1081 "spin 0.9.8",
1082]
1083
10321084[[package]]
10331085name = "fnv"
10341086version = "1.0.7"
@@ -1271,6 +1323,16 @@ dependencies = [
12711323 "wasm-bindgen",
12721324]
12731325
1326[[package]]
1327name = "gif"
1328version = "0.11.4"
1329source = "registry+https://github.com/rust-lang/crates.io-index"
1330checksum = "3edd93c6756b4dfaf2709eafcc345ba2636565295c198a9cfbf75fa5e3e00b06"
1331dependencies = [
1332 "color_quant",
1333 "weezl",
1334]
1335
12741336[[package]]
12751337name = "h2"
12761338version = "0.3.16"
@@ -1290,6 +1352,15 @@ dependencies = [
12901352 "tracing",
12911353]
12921354
1355[[package]]
1356name = "half"
1357version = "2.2.1"
1358source = "registry+https://github.com/rust-lang/crates.io-index"
1359checksum = "02b4af3693f1b705df946e9fe5631932443781d0aabb423b62fcd4d73f6d2fd0"
1360dependencies = [
1361 "crunchy",
1362]
1363
12931364[[package]]
12941365name = "hashbrown"
12951366version = "0.12.3"
@@ -1427,6 +1498,7 @@ dependencies = [
14271498name = "iamb"
14281499version = "0.0.7"
14291500dependencies = [
1501 "arboard",
14301502 "bitflags 1.3.2",
14311503 "chrono",
14321504 "clap",
@@ -1436,6 +1508,7 @@ dependencies = [
14361508 "emojis",
14371509 "gethostname 0.4.1",
14381510 "html5ever",
1511 "image",
14391512 "lazy_static 1.4.0",
14401513 "markup5ever_rcdom",
14411514 "matrix-sdk",
@@ -1506,9 +1579,13 @@ dependencies = [
15061579 "bytemuck",
15071580 "byteorder",
15081581 "color_quant",
1582 "exr",
1583 "gif",
1584 "jpeg-decoder",
15091585 "num-rational",
15101586 "num-traits",
15111587 "png",
1588 "scoped_threadpool",
15121589 "tiff",
15131590]
15141591
@@ -1617,6 +1694,9 @@ name = "jpeg-decoder"
16171694version = "0.3.0"
16181695source = "registry+https://github.com/rust-lang/crates.io-index"
16191696checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e"
1697dependencies = [
1698 "rayon",
1699]
16201700
16211701[[package]]
16221702name = "js-sys"
@@ -1657,6 +1737,12 @@ version = "1.4.0"
16571737source = "registry+https://github.com/rust-lang/crates.io-index"
16581738checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
16591739
1740[[package]]
1741name = "lebe"
1742version = "0.5.2"
1743source = "registry+https://github.com/rust-lang/crates.io-index"
1744checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8"
1745
16601746[[package]]
16611747name = "libc"
16621748version = "0.2.140"
@@ -2044,6 +2130,15 @@ dependencies = [
20442130 "unicode-width",
20452131]
20462132
2133[[package]]
2134name = "nanorand"
2135version = "0.7.0"
2136source = "registry+https://github.com/rust-lang/crates.io-index"
2137checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3"
2138dependencies = [
2139 "getrandom 0.2.8",
2140]
2141
20472142[[package]]
20482143name = "new_debug_unreachable"
20492144version = "1.0.4"
@@ -2730,6 +2825,28 @@ dependencies = [
27302825 "unicode-width",
27312826]
27322827
2828[[package]]
2829name = "rayon"
2830version = "1.7.0"
2831source = "registry+https://github.com/rust-lang/crates.io-index"
2832checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b"
2833dependencies = [
2834 "either",
2835 "rayon-core",
2836]
2837
2838[[package]]
2839name = "rayon-core"
2840version = "1.11.0"
2841source = "registry+https://github.com/rust-lang/crates.io-index"
2842checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d"
2843dependencies = [
2844 "crossbeam-channel",
2845 "crossbeam-deque",
2846 "crossbeam-utils",
2847 "num_cpus",
2848]
2849
27332850[[package]]
27342851name = "rdrand"
27352852version = "0.4.0"
@@ -2824,7 +2941,7 @@ dependencies = [
28242941 "cc",
28252942 "libc",
28262943 "once_cell",
2827 "spin",
2944 "spin 0.5.2",
28282945 "untrusted",
28292946 "web-sys",
28302947 "winapi",
@@ -3039,6 +3156,12 @@ dependencies = [
30393156 "winapi-util",
30403157]
30413158
3159[[package]]
3160name = "scoped_threadpool"
3161version = "0.1.9"
3162source = "registry+https://github.com/rust-lang/crates.io-index"
3163checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
3164
30423165[[package]]
30433166name = "scopeguard"
30443167version = "1.1.0"
@@ -3194,6 +3317,12 @@ version = "1.6.4"
31943317source = "registry+https://github.com/rust-lang/crates.io-index"
31953318checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c"
31963319
3320[[package]]
3321name = "simd-adler32"
3322version = "0.3.5"
3323source = "registry+https://github.com/rust-lang/crates.io-index"
3324checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f"
3325
31973326[[package]]
31983327name = "siphasher"
31993328version = "0.3.10"
@@ -3262,6 +3391,15 @@ version = "0.5.2"
32623391source = "registry+https://github.com/rust-lang/crates.io-index"
32633392checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
32643393
3394[[package]]
3395name = "spin"
3396version = "0.9.8"
3397source = "registry+https://github.com/rust-lang/crates.io-index"
3398checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
3399dependencies = [
3400 "lock_api",
3401]
3402
32653403[[package]]
32663404name = "spki"
32673405version = "0.5.4"
@@ -4341,3 +4479,12 @@ dependencies = [
43414479 "syn 1.0.109",
43424480 "synstructure",
43434481]
4482
4483[[package]]
4484name = "zune-inflate"
4485version = "0.2.54"
4486source = "registry+https://github.com/rust-lang/crates.io-index"
4487checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02"
4488dependencies = [
4489 "simd-adler32",
4490]
Cargo.toml +2
@@ -14,6 +14,7 @@ categories = ["command-line-utilities"]
1414rust-version = "1.66"
1515
1616[dependencies]
17arboard = "3.2.0"
1718bitflags = "1.3.2"
1819chrono = "0.4"
1920clap = {version = "4.0", features = ["derive"]}
@@ -23,6 +24,7 @@ dirs = "4.0.0"
2324emojis = "~0.5.2"
2425gethostname = "0.4.1"
2526html5ever = "0.26.0"
27image = "0.24.5"
2628markup5ever_rcdom = "0.2.0"
2729mime = "^0.3.16"
2830mime_guess = "^2.0.4"
src/base.rs +7
@@ -192,6 +192,7 @@ pub enum RoomAction {
192192pub enum SendAction {
193193 Submit,
194194 Upload(String),
195 UploadImage(usize, usize, Cow<'static, [u8]>),
195196}
196197
197198#[derive(Clone, Debug, Eq, PartialEq)]
@@ -360,6 +361,12 @@ pub enum IambError {
360361
361362 #[error("Verification request error: {0}")]
362363 VerificationRequestError(#[from] matrix_sdk::encryption::identities::RequestVerificationError),
364
365 #[error("Image error: {0}")]
366 Image(#[from] image::ImageError),
367
368 #[error("Could not use system clipboard data")]
369 Clipboard,
363370}
364371
365372impl From<IambError> for UIError<IambInfo> {
src/windows/room/chat.rs +40
@@ -4,6 +4,7 @@ use std::fs;
44use std::ops::Deref;
55use std::path::{Path, PathBuf};
66
7use modalkit::editing::store::RegisterError;
78use tokio;
89
910use matrix_sdk::{
@@ -471,6 +472,36 @@ impl ChatState {
471472 let msg = MessageType::Text(msg);
472473 let msg = RoomMessageEventContent::new(msg);
473474
475 (resp.event_id, msg)
476 },
477 SendAction::UploadImage(width, height, bytes) => {
478 // Convert to png because arboard does not give us the mime type.
479 let bytes =
480 image::ImageBuffer::from_raw(width as _, height as _, bytes.into_owned())
481 .ok_or(IambError::Clipboard)
482 .and_then(|imagebuf| {
483 let dynimage = image::DynamicImage::ImageRgba8(imagebuf);
484 let bytes = Vec::<u8>::new();
485 let mut buff = std::io::Cursor::new(bytes);
486 dynimage.write_to(&mut buff, image::ImageOutputFormat::Png)?;
487 Ok(buff.into_inner())
488 })
489 .map_err(IambError::from)?;
490 let mime = mime::IMAGE_PNG;
491
492 let name = Cow::from(format!("Clipboard.png"));
493 let config = AttachmentConfig::new();
494
495 let resp = room
496 .send_attachment(name.as_ref(), &mime, bytes.as_ref(), config)
497 .await
498 .map_err(IambError::from)?;
499
500 // Mock up the local echo message for the scrollback.
501 let msg = TextMessageEventContent::plain(format!("[Attached File: {name}]"));
502 let msg = MessageType::Text(msg);
503 let msg = RoomMessageEventContent::new(msg);
504
474505 (resp.event_id, msg)
475506 },
476507 };
@@ -617,6 +648,15 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState {
617648 // Run command again.
618649 delegate!(self, w => w.editor_command(act, ctx, store))
619650 },
651 Err(EditError::Register(RegisterError::ClipboardImage(data))) => {
652 let msg = "Do you really want to upload the image from your system clipboard?";
653 let send =
654 IambAction::Send(SendAction::UploadImage(data.width, data.height, data.bytes));
655 let prompt = PromptYesNo::new(msg, vec![Action::from(send)]);
656 let prompt = Box::new(prompt);
657
658 Err(EditError::NeedConfirm(prompt))
659 },
620660 res @ Err(_) => res,
621661 }
622662 }