can send text, needs a ui to enter it

This commit is contained in:
Joe Ardent 2025-08-04 10:52:58 -07:00
parent 358b96a744
commit 35b053be79
3 changed files with 59 additions and 6 deletions

View file

@ -1,6 +1,5 @@
use std::{collections::BTreeMap, net::SocketAddr, time::Duration};
use axum::body::Bytes;
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind};
use futures::{FutureExt, StreamExt};
use joecalsend::{JoecalService, ReceiveDialog, ReceiveRequest, TransferEvent, error::Result};
@ -35,7 +34,7 @@ pub struct App {
// other end is held by the service
event_listener: UnboundedReceiver<TransferEvent>,
file_picker: FileExplorer,
content: Option<Bytes>,
text: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -61,7 +60,7 @@ impl App {
event_listener,
screen: vec![CurrentScreen::Main],
file_picker: FileExplorer::new().expect("could not create file explorer"),
content: None,
text: Some("this is content".to_string()),
events: Default::default(),
peers: Default::default(),
peer_state: Default::default(),
@ -235,9 +234,14 @@ impl App {
async fn send_content(&mut self) {
debug!("sending content");
if let Some(_bytes) = self.content.to_owned() {
// self.service.send_bytes(session_id, content_id, token, body)
warn!("entering text is not yet supported");
if let Some(text) = self.text.to_owned() {
if let Some(idx) = self.peer_state.selected()
&& let Some(peer) = self.peers.get(idx)
{
if let Err(e) = self.service.send_text(&peer.fingerprint, &text).await {
error!("got error sending \"{text}\" to {}: {e:?}", peer.alias);
}
}
} else {
let file = self.file_picker.current().path().clone();
if file.is_dir()

View file

@ -62,6 +62,24 @@ impl FileMetadata {
metadata,
})
}
pub fn from_text(text: &str) -> Self {
let id = Julid::new().as_string();
let size = text.as_bytes().len() as u64;
let file_type = "text/plain".into();
let sha256 = Some(sha256::digest(text));
FileMetadata {
id,
file_name: "".to_string(),
size,
file_type,
sha256,
preview: Some(text.to_string()),
metadata: None,
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -168,6 +168,37 @@ impl JoecalService {
Ok(())
}
pub async fn send_text(&self, peer: &str, text: &str) -> Result<()> {
// Generate file metadata
let file_metadata = FileMetadata::from_text(text);
// Prepare files map
let mut files = BTreeMap::new();
files.insert(file_metadata.id.clone(), file_metadata.clone());
// Prepare upload
let prepare_response = self.prepare_upload(peer, files).await?;
// Get file token
let token = prepare_response
.files
.get(&file_metadata.id)
.ok_or(LocalSendError::InvalidToken)?;
let bytes = Bytes::from(text.to_owned());
// Upload file
self.send_bytes(
prepare_response.session_id,
file_metadata.id,
token.clone(),
bytes,
)
.await?;
Ok(())
}
pub async fn cancel_upload(&self, session_id: String) -> Result<()> {
let sessions = self.sessions.lock().await;
let session = sessions.get(&session_id).unwrap();