Skip to content

Commit

Permalink
ws::handlers::file > handle_file_(get\need) improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
canewsin committed Jan 11, 2024
1 parent 4ce69ad commit 2b7f2d8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
19 changes: 19 additions & 0 deletions src/plugins/site_server/handlers/sites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ impl Handler<FileGetRequest> for Site {
}
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, Message)]
#[rtype(result = "Result<bool, Error>")]
pub struct FileNeedRequest {
pub inner_path: String,
#[serde(default)]
pub timeout: usize,
#[serde(default)]
pub priority: usize,
}

impl Handler<FileNeedRequest> for Site {
type Result = Result<bool, Error>;

fn handle(&mut self, msg: FileNeedRequest, _ctx: &mut Context<Self>) -> Self::Result {
let res = block_on(self.need_file(msg.inner_path, None, None));
res
}
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, Message)]
#[rtype(result = "Option<Value>")]
pub struct FileRulesRequest {
Expand Down
33 changes: 19 additions & 14 deletions src/plugins/websocket/handlers/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ use serde_json::{json, Value};
use super::super::{error::Error, request::Command, response::Message, ZeruWebsocket};
use crate::{
environment::ENV,
plugins::site_server::handlers::sites::{FileGetRequest, FileRulesRequest},
plugins::site_server::handlers::sites::{FileGetRequest, FileNeedRequest, FileRulesRequest},
};

pub fn handle_file_need(
_: &ZeruWebsocket,
_: &mut WebsocketContext<ZeruWebsocket>,
_: &Command,
) -> Result<Message, Error> {
unimplemented!("Please File a Bug Report")
pub fn handle_file_need(ws: &ZeruWebsocket, cmd: &Command) -> Result<Message, Error> {
trace!("Handling FileNeed request");
let msg: FileNeedRequest = match serde_json::from_value(cmd.params.clone()) {
Ok(m) => m,
Err(e) => {
error!("{:?}", e);
FileNeedRequest::default()
}
};
let _ = block_on(ws.site_addr.send(msg))?;
cmd.respond("ok")
}

pub fn handle_file_get(
ws: &ZeruWebsocket,
_: &mut WebsocketContext<ZeruWebsocket>,
command: &Command,
) -> Result<Message, Error> {
pub fn handle_file_get(ws: &ZeruWebsocket, command: &Command) -> Result<Message, Error> {
trace!("Handling FileGet request {:?}", command);
let msg: FileGetRequest = match serde_json::from_value(command.params.clone()) {
Ok(m) => m,
Expand Down Expand Up @@ -69,15 +70,19 @@ pub fn handle_file_rules(
}
},
};
let mut rules = block_on(ws.site_addr.send(msg))?;
let mut rules = block_on(ws.site_addr.send(msg.clone()))?;
if rules.is_none() {
//TODO! Don't Send Empty Rules
// return Err(Error {
// error: String::from("File not found"),
// });
rules = Some(json!({"":""}));
}
command.respond(rules.unwrap())
let rules = rules.unwrap();
if msg.inner_path.ends_with("content.json") {
// TODO!: current_size value
}
command.respond(rules)
}

pub fn handle_file_query(
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ impl ZeruWebsocket {
ChannelJoin => handle_channel_join(ctx, command),
DbQuery => handle_db_query(self, command),

FileGet => handle_file_get(self, ctx, command),
FileNeed => handle_file_need(self, ctx, command),
FileGet => handle_file_get(self, command),
FileNeed => handle_file_need(self, command),
FileRules => handle_file_rules(self, ctx, command),
FileQuery => handle_file_query(self, ctx, command),
FileWrite => handle_file_write(self, ctx, command),
Expand Down

0 comments on commit 2b7f2d8

Please sign in to comment.