Skip to content

Commit

Permalink
feat: cli add arg service-disable
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhazhazhu committed Jan 24, 2024
1 parent ef3aa6d commit 9f6476a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
17 changes: 17 additions & 0 deletions crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ use colored::*;
pub struct Cli {
/// Sets a custom file path
pub path: Option<String>,
#[clap(flatten)]
pub config: AirncConfig,
}

#[derive(clap::Parser, Debug)]
pub struct AirncConfig {
/// Whether to disable the link remote service
#[clap(long = "service-disable", global = true)]
pub service_disable: bool,
}

impl Default for AirncConfig {
fn default() -> Self {
Self {
service_disable: false,
}
}
}

pub fn run_cli() -> Option<Cli> {
Expand Down
43 changes: 26 additions & 17 deletions crates/cli/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ pub async fn run_server(cli: Cli) -> Result<(), std::io::Error> {
let download_url = format!("http://{}/{}", addr.to_string(), &file_name);

create_qrcode(&download_url).unwrap();
println!("\nDownload URL: {}", download_url);

println!("");
println!("➤ Home: {}", "https://air.zhazhazhu.me".green());
println!("➤ Download: {}", download_url.green());
println!("");

let service = Service {
ip: local_ip.to_string(),
link: download_url.to_string(),
};

let ws_task = task::spawn(async {
connect_and_handle_messages(service).await;
let service_disable = cli.config.service_disable.clone();
let ws_task = task::spawn(async move {
if !service_disable {
connect_and_handle_messages(service).await;
}
});

HttpServer::new(move || {
Expand All @@ -73,28 +80,35 @@ async fn download_handler(
let mut ip = String::new();
if let Some(val) = user_addr {
ip = val.ip().to_string();
println!("User: {} request was initiated.", ip.to_string().green());
println!("User: {} Request was initiated.", ip.to_string().green());
};
let mut file = File::open(&data.file_path)?;
let file_size = file.metadata()?.len();
let (sender, mut receiver) = mpsc::channel::<Result<Bytes, io::Error>>(32);

let mut remaining_bytes = 8192;
let mut buffer = [0u8; 8192];
let mut remaining_bytes = 1024;
let mut buffer = [0u8; 1024];
let pb = ProgressBar::new(file_size);
let download_task = tokio::task::spawn_blocking(move || {
while remaining_bytes < file_size {
let bytes_to_read = buffer.len().min(remaining_bytes as usize);
let bytes_read = file.read(&mut buffer[..bytes_to_read])?;

let chunk = Bytes::copy_from_slice(&buffer[..bytes_read]);
sender
.blocking_send(Ok(chunk))
.expect(format!("{}", "Failed to send chunk".red()).as_str());
let send = sender.blocking_send(Ok(chunk));
if send.is_err() {
println!(
"User: {} {}",
ip.to_string().green(),
"Close Download".yellow()
);
return Err(io::Error::new(io::ErrorKind::Other, "Download closed"));
}

remaining_bytes += bytes_read as u64;
pb.inc(8192)
pb.inc(1024);
}

let text = format!("{} Download finish", ip.green());
println!("{}", text);
pb.finish_and_clear();
Expand All @@ -103,13 +117,8 @@ async fn download_handler(
});

tokio::spawn(async move {
if let Err(e) = download_task.await {
let text = format!(
"{} {}",
"Error occurred during file download:".red(),
e.to_string().red()
);
println!("{}", text);
if let Err(_) = download_task.await {
println!("{}", "Error occurred during file download:".red());
}
});

Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ pub async fn connect_and_handle_messages(service: Service) {
match msg {
Frame::Text { payload, .. } => {
// 处理接收到的文本消息
println!("Received text message: {}", payload);
println!("➤ Received: {}", payload);
}
Frame::Close { .. } => {
println!("Received close message");
println!("Received close");
break;
}
_ => {}
Expand Down

0 comments on commit 9f6476a

Please sign in to comment.