11use std:: path:: PathBuf ;
22
3- use anyhow:: Result ;
43use iroh:: { protocol:: Router , Endpoint } ;
5- use iroh_blobs:: {
6- api:: blobs:: { AddPathOptions , ExportMode , ExportOptions , ImportMode } ,
7- net_protocol:: Blobs ,
8- store:: mem:: MemStore ,
9- ticket:: BlobTicket ,
10- BlobFormat ,
11- } ;
4+ use iroh_blobs:: { net_protocol:: Blobs , store:: mem:: MemStore , ticket:: BlobTicket } ;
125
136#[ tokio:: main]
14- async fn main ( ) -> Result < ( ) > {
7+ async fn main ( ) -> anyhow :: Result < ( ) > {
158 // Create an endpoint, it allows creating and accepting
169 // connections in the iroh p2p world
1710 let endpoint = Endpoint :: builder ( ) . discovery_n0 ( ) . bind ( ) . await ?;
11+
1812 // We initialize an in-memory backing store for iroh-blobs
1913 let store = MemStore :: new ( ) ;
2014 // Then we initialize a struct that can accept blobs requests over iroh connections
@@ -27,29 +21,16 @@ async fn main() -> Result<()> {
2721
2822 match arg_refs. as_slice ( ) {
2923 [ "send" , filename] => {
30- // For sending files we build a router that accepts blobs connections & routes them
31- // to the blobs protocol.
32- let router = Router :: builder ( endpoint)
33- . accept ( iroh_blobs:: ALPN , blobs)
34- . spawn ( ) ;
35-
3624 let filename: PathBuf = filename. parse ( ) ?;
3725 let abs_path = std:: path:: absolute ( & filename) ?;
3826
3927 println ! ( "Hashing file." ) ;
4028
41- // When we import a blob, we get back a tag that refers to said blob in the store
29+ // When we import a blob, we get back a " tag" that refers to said blob in the store
4230 // and allows us to control when/if it gets garbage-collected
43- let tag = store
44- . blobs ( )
45- . add_path_with_opts ( AddPathOptions {
46- path : abs_path,
47- format : BlobFormat :: Raw ,
48- mode : ImportMode :: TryReference ,
49- } )
50- . await ?;
31+ let tag = store. blobs ( ) . add_path ( abs_path) . await ?;
5132
52- let node_id = router . endpoint ( ) . node_id ( ) ;
33+ let node_id = endpoint. node_id ( ) ;
5334 let ticket = BlobTicket :: new ( node_id. into ( ) , tag. hash , tag. format ) ;
5435
5536 println ! ( "File hashed. Fetch this file by running:" ) ;
@@ -58,38 +39,36 @@ async fn main() -> Result<()> {
5839 filename. display( )
5940 ) ;
6041
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+
6148 tokio:: signal:: ctrl_c ( ) . await ?;
6249
6350 // Gracefully shut down the node
6451 println ! ( "Shutting down." ) ;
6552 router. shutdown ( ) . await ?;
6653 }
6754 [ "receive" , ticket, filename] => {
68- // For receiving files, we create a "downloader" that allows us to fetch files
69- // from other nodes via iroh connections
70- let downloader = store. downloader ( & endpoint) ;
71-
7255 let filename: PathBuf = filename. parse ( ) ?;
7356 let abs_path = std:: path:: absolute ( filename) ?;
7457 let ticket: BlobTicket = ticket. parse ( ) ?;
7558
7659 println ! ( "Starting download." ) ;
7760
78- downloader
61+ // For receiving files, we create a "downloader" that allows us to fetch files
62+ // from other nodes via iroh connections
63+ store
64+ . downloader ( & endpoint)
7965 . download ( ticket. hash ( ) , Some ( ticket. node_addr ( ) . node_id ) )
8066 . await ?;
8167
8268 println ! ( "Finished download." ) ;
8369 println ! ( "Copying to destination." ) ;
8470
85- store
86- . blobs ( )
87- . export_with_opts ( ExportOptions {
88- hash : ticket. hash ( ) ,
89- mode : ExportMode :: TryReference ,
90- target : abs_path,
91- } )
92- . await ?;
71+ store. blobs ( ) . export ( ticket. hash ( ) , abs_path) . await ?;
9372
9473 println ! ( "Finished copying." ) ;
9574
0 commit comments