Skip to content

Commit f21f60a

Browse files
committed
update
1 parent 25fc37c commit f21f60a

File tree

7 files changed

+222
-100
lines changed

7 files changed

+222
-100
lines changed

Cargo.lock

+95-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ description = "easy file transfer on same network"
1010
[dependencies]
1111
anyhow = "1.0.59"
1212
clap = { version = "3.2.16", features = ["derive", "cargo"] }
13-
env_logger = "0.9.0"
1413
if-addrs = "0.7.0"
15-
indicatif = "0.17.0"
16-
log = "0.4.17"
14+
indicatif = { version = "0.17.0", features = ["tokio"] }
1715
mdns-sd = "0.5.5"
1816
names = { version = "0.14.0", default-features = false }
1917
tokio = { version = "1.20.1", features = ["full", "rt-multi-thread", "net"] }
18+
tracing = "0.1"
19+
tracing-subscriber = "0.3.15"

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ $ cargo install --git https://github.com/rnbguy/rope
1414
# Send
1515

1616
```
17-
$ rope send <FILE>
18-
MAGIC -> blistering-barnacles
17+
$ rope send video.mp4
18+
MAGIC: blistering-barnacles
1919
```
2020

2121
# Receive
2222

2323
```
2424
$ rope recv blistering-barnacles
25+
$ ls video.mp4
2526
```

src/cli.rs

+46-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
use std::fs::metadata;
2+
use std::path::{Path, PathBuf};
3+
14
use crate::AResult;
5+
use anyhow::anyhow;
26
use clap::Parser;
37
use clap::{crate_authors, crate_description, crate_name, crate_version};
48
use tokio::sync::oneshot;
9+
use tracing::{debug, info};
510

611
use crate::{generate_magic_string, recv_file, recv_msg, send_file, send_msg};
712

@@ -10,38 +15,68 @@ use crate::{generate_magic_string, recv_file, recv_msg, send_file, send_msg};
1015
pub enum App {
1116
Send {
1217
file_path: String,
13-
opt_magic_string: Option<String>,
18+
magic_string: Option<String>,
1419
},
1520
Recv {
1621
magic_string: String,
22+
save_dir: Option<PathBuf>,
1723
},
1824
}
1925

2026
impl App {
2127
pub async fn run(&self) -> AResult<()> {
28+
debug!("{self:?}");
2229
match self {
2330
App::Send {
24-
opt_magic_string,
31+
magic_string,
2532
file_path,
2633
} => {
27-
let magic_string = opt_magic_string
34+
let magic_string = magic_string
2835
.to_owned()
2936
.unwrap_or_else(generate_magic_string);
30-
println!("MAGIC -> {magic_string}");
37+
38+
println!("MAGIC: {magic_string}");
39+
40+
info!("MAGIC: {magic_string}");
3141

3242
let (tx, rx) = oneshot::channel();
33-
let port = send_file(file_path, tx).await?;
43+
44+
let file_size = metadata(file_path)?.len();
45+
46+
let port = send_file(file_path, file_size, tx).await?;
47+
3448
send_msg(
3549
&magic_string,
3650
port,
37-
[("name".into(), file_path.into())].into(),
38-
rx,
39-
)
40-
.await?;
51+
[
52+
("name".into(), file_path.into()),
53+
("size".into(), file_size.to_string()),
54+
]
55+
.into(),
56+
)?;
57+
58+
rx.await?;
4159
}
42-
App::Recv { magic_string } => {
60+
App::Recv {
61+
magic_string,
62+
save_dir,
63+
} => {
4364
let (addrs, port, data) = recv_msg(magic_string)?;
44-
recv_file(addrs.iter().next().unwrap(), port, &data["name"]).await?;
65+
let name = Path::new(&data["name"])
66+
.file_name()
67+
.and_then(|x| x.to_str())
68+
.ok_or_else(|| anyhow!("Error while read filename"))?;
69+
let path = save_dir.clone().unwrap_or_else(PathBuf::new).join(name);
70+
for addr in &addrs {
71+
debug!("Trying {addr}");
72+
if recv_file(addr, port, &path, data["size"].parse()?)
73+
.await
74+
.is_ok()
75+
{
76+
debug!("File is received. Breaking loop");
77+
break;
78+
}
79+
}
4580
}
4681
}
4782
Ok(())

0 commit comments

Comments
 (0)