Skip to content

Commit 568a3e3

Browse files
matheus23rklaehn
andauthored
docs: Update the transfer example to iroh 0.90 (#105)
## Description Revives the [old transfer example](https://github.com/n0-computer/iroh-blobs/blob/8a975ecda8bca5e8988db911857012e6817b456e/examples/transfer.rs) so we can use it on the https://www.iroh.computer/docs/quickstart page ## Notes & open questions Also fixes a bug. ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. --------- Co-authored-by: Ruediger Klaehn <[email protected]>
1 parent bc61e8e commit 568a3e3

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

examples/transfer.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::path::PathBuf;
2+
3+
use iroh::{protocol::Router, Endpoint};
4+
use iroh_blobs::{net_protocol::Blobs, store::mem::MemStore, ticket::BlobTicket};
5+
6+
#[tokio::main]
7+
async fn main() -> anyhow::Result<()> {
8+
// Create an endpoint, it allows creating and accepting
9+
// connections in the iroh p2p world
10+
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
11+
12+
// We initialize an in-memory backing store for iroh-blobs
13+
let store = MemStore::new();
14+
// Then we initialize a struct that can accept blobs requests over iroh connections
15+
let blobs = Blobs::new(&store, endpoint.clone(), None);
16+
17+
// Grab all passed in arguments, the first one is the binary itself, so we skip it.
18+
let args: Vec<String> = std::env::args().skip(1).collect();
19+
// Convert to &str, so we can pattern-match easily:
20+
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
21+
22+
match arg_refs.as_slice() {
23+
["send", filename] => {
24+
let filename: PathBuf = filename.parse()?;
25+
let abs_path = std::path::absolute(&filename)?;
26+
27+
println!("Hashing file.");
28+
29+
// When we import a blob, we get back a "tag" that refers to said blob in the store
30+
// and allows us to control when/if it gets garbage-collected
31+
let tag = store.blobs().add_path(abs_path).await?;
32+
33+
let node_id = endpoint.node_id();
34+
let ticket = BlobTicket::new(node_id.into(), tag.hash, tag.format);
35+
36+
println!("File hashed. Fetch this file by running:");
37+
println!(
38+
"cargo run --example transfer -- receive {ticket} {}",
39+
filename.display()
40+
);
41+
42+
// For sending files we build a router that accepts blobs connections & routes them
43+
// to the blobs protocol.
44+
let router = Router::builder(endpoint)
45+
.accept(iroh_blobs::ALPN, blobs)
46+
.spawn();
47+
48+
tokio::signal::ctrl_c().await?;
49+
50+
// Gracefully shut down the node
51+
println!("Shutting down.");
52+
router.shutdown().await?;
53+
}
54+
["receive", ticket, filename] => {
55+
let filename: PathBuf = filename.parse()?;
56+
let abs_path = std::path::absolute(filename)?;
57+
let ticket: BlobTicket = ticket.parse()?;
58+
59+
// For receiving files, we create a "downloader" that allows us to fetch files
60+
// from other nodes via iroh connections
61+
let downloader = store.downloader(&endpoint);
62+
63+
println!("Starting download.");
64+
65+
downloader
66+
.download(ticket.hash(), Some(ticket.node_addr().node_id))
67+
.await?;
68+
69+
println!("Finished download.");
70+
println!("Copying to destination.");
71+
72+
store.blobs().export(ticket.hash(), abs_path).await?;
73+
74+
println!("Finished copying.");
75+
76+
// Gracefully shut down the node
77+
println!("Shutting down.");
78+
endpoint.close().await;
79+
}
80+
_ => {
81+
println!("Couldn't parse command line arguments: {args:?}");
82+
println!("Usage:");
83+
println!(" # to send:");
84+
println!(" cargo run --example transfer -- send [FILE]");
85+
println!(" # this will print a ticket.");
86+
println!();
87+
println!(" # to receive:");
88+
println!(" cargo run --example transfer -- receive [TICKET] [FILE]");
89+
}
90+
}
91+
92+
Ok(())
93+
}

src/api/downloader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl Downloader {
364364
self.download_with_opts(DownloadOptions {
365365
request,
366366
providers,
367-
strategy: SplitStrategy::Split,
367+
strategy: SplitStrategy::None,
368368
})
369369
}
370370

0 commit comments

Comments
 (0)