From 89396b8f07ee967cd05c8c4f45f5f55edb1ade37 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Tue, 15 Sep 2026 21:03:24 +0700 Subject: [PATCH] fix --- crates/state/src/blossom.rs | 42 ++++++++++++++++++------ crates/state/src/file.rs | 64 +++++++++++++++---------------------- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/crates/state/src/blossom.rs b/crates/state/src/blossom.rs index b1628545..eed8c6b6 100644 --- a/crates/state/src/blossom.rs +++ b/crates/state/src/blossom.rs @@ -4,30 +4,52 @@ use anyhow::{Error, anyhow}; use gpui::AsyncApp; #[cfg(not(target_arch = "wasm32"))] use gpui_tokio::Tokio; +#[cfg(not(target_arch = "wasm32"))] use mime_guess::from_path; use nostr_blossom::prelude::*; use nostr_sdk::prelude::*; #[cfg(not(target_arch = "wasm32"))] -pub async fn upload(server: Url, path: PathBuf, cx: &AsyncApp) -> Result { - let content_type = from_path(&path).first_or_octet_stream().to_string(); - let data = smol::fs::read(path).await?; - let keys = Keys::generate(); +use crate::file::sha256_hex; - // Construct the blossom client - let client = BlossomClient::new(server); +/// Upload a blob to a blossom server and return its URL +#[cfg(not(target_arch = "wasm32"))] +pub(crate) async fn upload_blob( + server: &Url, + data: Vec, + content_type: &str, + sha256: &str, + cx: &AsyncApp, +) -> Result { + let client = BlossomClient::new(server.clone()); + let keys = Keys::generate(); + let content_type = content_type.to_string(); + let base = server.clone(); + let hash = sha256.to_string(); Tokio::spawn(cx, async move { - let blob = client + match client .upload_blob(data, Some(content_type), None, Some(&keys)) - .await?; - - Ok(blob.url) + .await + { + Ok(blob) => Ok(blob.url), + Err(e) if e.to_string().contains("201 Created") => Ok::(base.join(&hash)?), + Err(e) => Err(anyhow!(e.to_string())), + } }) .await .map_err(|e| anyhow!("Upload error: {e}"))? } +#[cfg(not(target_arch = "wasm32"))] +pub async fn upload(server: Url, path: PathBuf, cx: &AsyncApp) -> Result { + let content_type = from_path(&path).first_or_octet_stream().to_string(); + let data = smol::fs::read(&path).await?; + let sha256 = sha256_hex(&data); + + upload_blob(&server, data, &content_type, &sha256, cx).await +} + #[cfg(target_arch = "wasm32")] pub async fn upload(_server: Url, _path: PathBuf, _cx: &AsyncApp) -> Result { Err(anyhow!("File upload not supported on web")) diff --git a/crates/state/src/file.rs b/crates/state/src/file.rs index 7ef69b88..924d8621 100644 --- a/crates/state/src/file.rs +++ b/crates/state/src/file.rs @@ -10,12 +10,8 @@ use futures::AsyncReadExt; use gpui::http_client::AsyncBody; use gpui::{AsyncApp, SharedString}; #[cfg(not(target_arch = "wasm32"))] -use gpui_tokio::Tokio; -#[cfg(not(target_arch = "wasm32"))] use mime_guess::from_path; use nostr::nips::nip94::Sha256Hash; -#[cfg(not(target_arch = "wasm32"))] -use nostr_blossom::prelude::*; use nostr_sdk::prelude::*; use sha2::{Digest, Sha256}; @@ -182,49 +178,41 @@ pub async fn upload_encrypted( let data = smol::fs::read(&path).await?; let encrypted = encrypt(&data)?; - let sha256 = Some(sha256_hex(&encrypted.data)); - let original_sha256 = Some(sha256_hex(&data)); - let size = Some(encrypted.data.len() as u64); + let sha256 = sha256_hex(&encrypted.data); + let original_sha256 = sha256_hex(&data); + let size = encrypted.data.len() as u64; let base_url = server.to_string(); - let keys = Keys::generate(); - let client = BlossomClient::new(server); - let url = Tokio::spawn(cx, async move { - let blob = client - .upload_blob( - encrypted.data, - Some("application/octet-stream".to_string()), - None, - Some(&keys), - ) - .await - .map_err(|e| { - let message = e.to_string(); - - if !message.contains("415") { - return anyhow!(message); - } - - anyhow!( - "{base_url} rejected the encrypted file. Encrypted attachments are uploaded as - opaque data, which this file server does not accept. Choose a different file - server in the settings." - ) - })?; - - Ok::(blob.url) - }) + let url = crate::blossom::upload_blob( + &server, + encrypted.data, + "application/octet-stream", + &sha256, + cx, + ) .await - .map_err(|e| anyhow!("Upload error: {e}"))??; + .map_err(|e| { + let message = e.to_string(); + + if !message.contains("415") { + return anyhow!(message); + } + + anyhow!( + "{base_url} rejected the encrypted file. Encrypted attachments are uploaded as + opaque data, which this file server does not accept. Choose a different file + server in the settings." + ) + })?; Ok(FileAttachment { url, mime, key: encrypted.key, nonce: encrypted.nonce, - sha256, - original_sha256, - size, + sha256: Some(sha256), + original_sha256: Some(original_sha256), + size: Some(size), dim: None, name, })