chore: improve the media uploader (#161)

* refactor upload

* .

* .
This commit is contained in:
reya
2025-09-22 07:30:32 +07:00
committed by GitHub
parent 1de3045505
commit fb3da096f8
2 changed files with 113 additions and 93 deletions

78
Cargo.lock generated
View File

@@ -87,9 +87,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.99"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "arbitrary"
@@ -912,9 +912,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.2.37"
version = "1.2.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65193589c6404eb80b450d618eaf9a2cafaaafd57ecce47370519ef674a7bd44"
checksum = "80f41ae168f955c12fb8960b057d70d0ca153fb83182b57d86380443527be7e9"
dependencies = [
"find-msvc-tools",
"jobserver",
@@ -1131,7 +1131,7 @@ dependencies = [
[[package]]
name = "collections"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"indexmap",
"rustc-hash 2.1.1",
@@ -1572,7 +1572,7 @@ dependencies = [
[[package]]
name = "derive_refineable"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"proc-macro2",
"quote",
@@ -1739,9 +1739,9 @@ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "embed-resource"
version = "3.0.5"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c6d81016d6c977deefb2ef8d8290da019e27cc26167e102185da528e6c0ab38"
checksum = "55a075fc573c64510038d7ee9abc7990635863992f83ebc52c8b433b8411a02e"
dependencies = [
"cc",
"memchr",
@@ -1976,9 +1976,9 @@ dependencies = [
[[package]]
name = "find-msvc-tools"
version = "0.1.1"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fd99930f64d146689264c637b5af2f0233a933bef0d8570e2526bf9e083192d"
checksum = "1ced73b1dacfc750a6db6c0a0c3a3853c8b41997e2e2c563dc90804ae6867959"
[[package]]
name = "flatbuffers"
@@ -2499,7 +2499,7 @@ dependencies = [
[[package]]
name = "gpui"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"anyhow",
"as-raw-xcb-connection",
@@ -2571,6 +2571,7 @@ dependencies = [
"thiserror 2.0.16",
"usvg",
"util",
"util_macros",
"uuid",
"waker-fn",
"wayland-backend",
@@ -2592,7 +2593,7 @@ dependencies = [
[[package]]
name = "gpui_macros"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"heck 0.5.0",
"proc-macro2",
@@ -2604,7 +2605,7 @@ dependencies = [
[[package]]
name = "gpui_tokio"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"anyhow",
"gpui",
@@ -2824,7 +2825,7 @@ dependencies = [
[[package]]
name = "http_client"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"anyhow",
"bytes",
@@ -2844,7 +2845,7 @@ dependencies = [
[[package]]
name = "http_client_tls"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"rustls",
"rustls-platform-verifier",
@@ -3636,7 +3637,7 @@ dependencies = [
[[package]]
name = "media"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"anyhow",
"bindgen 0.71.1",
@@ -4751,9 +4752,9 @@ dependencies = [
[[package]]
name = "pxfm"
version = "0.1.23"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f55f4fedc84ed39cb7a489322318976425e42a147e2be79d8f878e2884f94e84"
checksum = "83f9b339b02259ada5c0f4a389b7fb472f933aa17ce176fd2ad98f28bb401fde"
dependencies = [
"num-traits",
]
@@ -5076,7 +5077,7 @@ dependencies = [
[[package]]
name = "refineable"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"derive_refineable",
"workspace-hack",
@@ -5230,7 +5231,7 @@ dependencies = [
[[package]]
name = "reqwest_client"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"anyhow",
"bytes",
@@ -5442,9 +5443,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.23.31"
version = "0.23.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0ebcbd2f03de0fc1122ad9bb24b127a5a6cd51d72604a3f3c50ac459762b6cc"
checksum = "cd3c25631629d034ce7cd9940adc9d45762d46de2b0f57193c4443b92c6d4d40"
dependencies = [
"aws-lc-rs",
"log",
@@ -5765,7 +5766,7 @@ checksum = "0f7d95a54511e0c7be3f51e8867aa8cf35148d7b9445d44de2f943e2b206e749"
[[package]]
name = "semantic_version"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"anyhow",
"serde",
@@ -5780,9 +5781,9 @@ checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2"
[[package]]
name = "serde"
version = "1.0.225"
version = "1.0.226"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6c24dee235d0da097043389623fb913daddf92c76e9f5a1db88607a0bcbd1d"
checksum = "0dca6411025b24b60bfa7ec1fe1f8e710ac09782dca409ee8237ba74b51295fd"
dependencies = [
"serde_core",
"serde_derive",
@@ -5790,18 +5791,18 @@ dependencies = [
[[package]]
name = "serde_core"
version = "1.0.225"
version = "1.0.226"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "659356f9a0cb1e529b24c01e43ad2bdf520ec4ceaf83047b83ddcc2251f96383"
checksum = "ba2ba63999edb9dac981fb34b3e5c0d111a69b0924e253ed29d83f7c99e966a4"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.225"
version = "1.0.226"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ea936adf78b1f766949a4977b91d2f5595825bd6ec079aa9543ad2685fc4516"
checksum = "8db53ae22f34573731bafa1db20f04027b2d25e02d8205921b569171699cdb33"
dependencies = [
"proc-macro2",
"quote",
@@ -6217,7 +6218,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "sum_tree"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"arrayvec",
"log",
@@ -6592,11 +6593,12 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.43"
version = "0.3.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83bde6f1ec10e72d583d91623c939f623002284ef622b87de38cfd546cbf2031"
checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d"
dependencies = [
"deranged",
"itoa",
"num-conv",
"powerfmt",
"serde",
@@ -7259,7 +7261,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "util"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#e5e308ba78c3de679b822e50a07b3432a1624ce1"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"anyhow",
"async-fs",
@@ -7291,6 +7293,16 @@ dependencies = [
"workspace-hack",
]
[[package]]
name = "util_macros"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349"
dependencies = [
"quote",
"syn 2.0.106",
"workspace-hack",
]
[[package]]
name = "uuid"
version = "1.18.1"

View File

@@ -1,6 +1,5 @@
use std::collections::HashMap;
use anyhow::anyhow;
use common::display::{ReadableProfile, ReadableTimestamp};
use common::nip96::nip96_upload;
use global::{app_state, nostr_client};
@@ -31,6 +30,7 @@ use ui::dock_area::panel::{Panel, PanelEvent};
use ui::emoji_picker::EmojiPicker;
use ui::input::{InputEvent, InputState, TextInput};
use ui::modal::ModalButtonProps;
use ui::notification::Notification;
use ui::popup_menu::{PopupMenu, PopupMenuExt};
use ui::text::RenderedText;
use ui::{
@@ -215,7 +215,6 @@ impl Chat {
id: room.read(cx).id.to_string().into(),
image_cache: RetainAllImageCache::new(cx),
focus_handle: cx.focus_handle(),
uploading: false,
rendered_texts_by_id: BTreeMap::new(),
reports_by_id: BTreeMap::new(),
relays,
@@ -225,6 +224,7 @@ impl Chat {
input,
replies_to,
attachments,
uploading: false,
_subscriptions: subscriptions,
_tasks: tasks,
}
@@ -282,15 +282,17 @@ impl Chat {
let attachments = self.attachments.read(cx);
if !attachments.is_empty() {
content = format!(
"{}\n{}",
content,
attachments
.iter()
.map(|url| url.to_string())
.collect_vec()
.join("\n")
)
let urls = attachments
.iter()
.map(|url| url.to_string())
.collect_vec()
.join("\n");
if content.is_empty() {
content = urls;
} else {
content = format!("{content}\n{urls}");
}
}
content
@@ -342,6 +344,9 @@ impl Chat {
// Remove all replies
this.remove_all_replies(cx);
// remove all attachments
this.remove_all_attachments(cx);
// Reset the input state
this.input.update(cx, |this, cx| {
this.set_loading(false, cx);
@@ -533,73 +538,74 @@ impl Chat {
}
fn upload(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.uploading {
return;
}
// Block the upload button to until current task is resolved
self.uploading(true, cx);
// Get the user's configured NIP96 server
let nip96_server = AppSettings::get_media_server(cx);
// Open native file dialog
let paths = cx.prompt_for_paths(PathPromptOptions {
let path = cx.prompt_for_paths(PathPromptOptions {
files: true,
directories: false,
multiple: false,
prompt: None,
});
let task = Tokio::spawn(cx, async move {
match Flatten::flatten(paths.await.map_err(|e| e.into())) {
Ok(Some(mut paths)) => {
if let Some(path) = paths.pop() {
let file = fs::read(path).await?;
let url = nip96_upload(nostr_client(), &nip96_server, file).await?;
cx.spawn_in(window, async move |this, cx| {
let mut paths = path.await.ok()?.ok()??;
let path = paths.pop()?;
Ok(url)
} else {
Err(anyhow!("Path not found"))
let upload = Tokio::spawn(cx, async move {
let client = nostr_client();
let file = fs::read(path).await.ok()?;
let url = nip96_upload(client, &nip96_server, file).await.ok()?;
Some(url)
});
if let Ok(task) = upload {
this.update(cx, |this, cx| {
this.set_uploading(true, cx);
})
.ok();
match Flatten::flatten(task.await.map_err(|e| e.into())) {
Ok(Some(url)) => {
this.update(cx, |this, cx| {
this.add_attachment(url, cx);
this.set_uploading(false, cx);
})
.ok();
}
Ok(None) => {
this.update_in(cx, |this, window, cx| {
window.push_notification("Failed to upload file", cx);
this.set_uploading(false, cx);
})
.ok();
}
Err(e) => {
this.update_in(cx, |this, window, cx| {
window.push_notification(Notification::error(e.to_string()), cx);
this.set_uploading(false, cx);
})
.ok();
}
}
Ok(None) => Err(anyhow!("User cancelled")),
Err(e) => Err(anyhow!("File dialog error: {e}")),
}
});
cx.spawn_in(window, async move |this, cx| {
match Flatten::flatten(task.await.map_err(|e| e.into())) {
Ok(Ok(url)) => {
this.update(cx, |this, cx| {
this.add_attachment(url, cx);
})
.ok();
}
Ok(Err(e)) => {
log::warn!("User cancelled: {e}");
this.update(cx, |this, cx| {
this.uploading(false, cx);
})
.ok();
}
Err(e) => {
this.update_in(cx, |this, window, cx| {
window.push_notification(e.to_string(), cx);
this.uploading(false, cx);
})
.ok();
}
}
Some(())
})
.detach();
}
fn set_uploading(&mut self, uploading: bool, cx: &mut Context<Self>) {
self.uploading = uploading;
cx.notify();
}
fn add_attachment(&mut self, url: Url, cx: &mut Context<Self>) {
self.attachments.update(cx, |this, cx| {
this.push(url);
cx.notify();
});
self.uploading(false, cx);
}
fn remove_attachment(&mut self, url: &Url, _window: &mut Window, cx: &mut Context<Self>) {
@@ -611,9 +617,11 @@ impl Chat {
});
}
fn uploading(&mut self, status: bool, cx: &mut Context<Self>) {
self.uploading = status;
cx.notify();
fn remove_all_attachments(&mut self, cx: &mut Context<Self>) {
self.attachments.update(cx, |this, cx| {
this.clear();
cx.notify();
});
}
fn render_announcement(&mut self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
@@ -1418,10 +1426,10 @@ impl Render for Chat {
.child(
Button::new("upload")
.icon(IconName::Upload)
.loading(self.uploading)
.disabled(self.uploading)
.ghost()
.large()
.disabled(self.uploading)
.loading(self.uploading)
.on_click(cx.listener(
move |this, _, window, cx| {
this.upload(window, cx);