Add media directory commands

This commit is contained in:
2026-05-30 22:06:59 -07:00
parent 1154f9d3c8
commit b1f31a0de8
4 changed files with 52 additions and 13 deletions
+35 -10
View File
@@ -2,8 +2,12 @@ use clap::{Parser,Subcommand,ArgAction};
use fedichat::client::{ClientMessage,SignedClientMessage,AuthMethod};
use fedichat::ServerAddr;
use fedichat::state::StatePath;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use time::OffsetDateTime;
use thiserror::Error;
use uuid::Uuid;
// Everything except server, user, and password lives in these
@@ -61,6 +65,15 @@ pub enum Command {
#[clap(short, long, value_parser=Room::from_str)]
room: Room,
},
Upload {
file: PathBuf,
},
Fetch {
#[clap(short, long)]
id: String,
#[clap(short, long)]
file: PathBuf
},
GetState {
#[clap(short, long, value_parser=Room::from_str)]
room: Room,
@@ -90,9 +103,9 @@ pub enum Command {
}
impl Command {
// Returns clientmessage and target server
fn into_client_message(self,username: String) -> (ClientMessage,Option<ServerAddr>) {
fn into_client_message(self,username: String) -> Result<(ClientMessage,Option<ServerAddr>),MessageError> {
use Command::*;
match self {
Ok(match self {
CreateUser {
password,
} => (ClientMessage::UserCreate {username, password},None),
@@ -118,6 +131,14 @@ impl Command {
Create {
room,
} => (ClientMessage::RoomCreate{room_id: room.get_coord()},room.get_server()),
Upload {file} => {
let mut buf = Vec::with_capacity(4096);
let mut file = File::open(file)?;
file.read_to_end(&mut buf)?;
(ClientMessage::MediaUpload{bytes: buf},None)
},
Fetch {file: _, id} => (ClientMessage::MediaFetch{id: Uuid::parse_str(&id)?},None),
GetState {
room,
path,
@@ -137,20 +158,19 @@ impl Command {
body,
user,
} => (ClientMessage::MessagePost{body, user: user.clone()},Some(ServerAddr(user.server))),
}
})
}
pub fn into_signed_message(self, username: String) -> SignedClientMessage {
let (message,target) = self.into_client_message(username);
SignedClientMessage {
pub fn into_signed_message(self, username: String) -> Result<SignedClientMessage,MessageError> {
let (message,target) = self.into_client_message(username)?;
Ok(SignedClientMessage {
message,
target,
timestamp: OffsetDateTime::now_utc(),
// TODO: actually implement signatures
signature: Box::new([0])
}
})
}
pub fn needs_auth(&self) -> bool {
@@ -180,7 +200,7 @@ impl Command {
signature: Box::new([0])
})
}
messages.push(self.into_signed_message(username));
messages.push(self.into_signed_message(username)?);
// If this message is authenticating us then we should be generating
// a new token and saving it for future commands
if !needs_auth {
@@ -198,7 +218,12 @@ impl Command {
#[derive(Error,Debug)]
pub enum MessageError {
#[error("Command needs a token. Login or create an account first.")]
NoToken
NoToken,
#[error("Error while parsing uuid: {0}")]
UuidError(#[from] uuid::Error),
#[error("Error during file IO: {0}")]
IoError(#[from] std::io::Error),
}