1
1
use std:: path:: PathBuf ;
2
2
3
- use anyhow:: Result ;
4
3
use 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 } ;
12
5
13
6
#[ tokio:: main]
14
- async fn main ( ) -> Result < ( ) > {
7
+ async fn main ( ) -> anyhow :: Result < ( ) > {
15
8
// Create an endpoint, it allows creating and accepting
16
9
// connections in the iroh p2p world
17
10
let endpoint = Endpoint :: builder ( ) . discovery_n0 ( ) . bind ( ) . await ?;
11
+
18
12
// We initialize an in-memory backing store for iroh-blobs
19
13
let store = MemStore :: new ( ) ;
20
14
// Then we initialize a struct that can accept blobs requests over iroh connections
@@ -27,29 +21,16 @@ async fn main() -> Result<()> {
27
21
28
22
match arg_refs. as_slice ( ) {
29
23
[ "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
-
36
24
let filename: PathBuf = filename. parse ( ) ?;
37
25
let abs_path = std:: path:: absolute ( & filename) ?;
38
26
39
27
println ! ( "Hashing file." ) ;
40
28
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
42
30
// 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 ?;
51
32
52
- let node_id = router . endpoint ( ) . node_id ( ) ;
33
+ let node_id = endpoint. node_id ( ) ;
53
34
let ticket = BlobTicket :: new ( node_id. into ( ) , tag. hash , tag. format ) ;
54
35
55
36
println ! ( "File hashed. Fetch this file by running:" ) ;
@@ -58,38 +39,36 @@ async fn main() -> Result<()> {
58
39
filename. display( )
59
40
) ;
60
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
+
61
48
tokio:: signal:: ctrl_c ( ) . await ?;
62
49
63
50
// Gracefully shut down the node
64
51
println ! ( "Shutting down." ) ;
65
52
router. shutdown ( ) . await ?;
66
53
}
67
54
[ "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
-
72
55
let filename: PathBuf = filename. parse ( ) ?;
73
56
let abs_path = std:: path:: absolute ( filename) ?;
74
57
let ticket: BlobTicket = ticket. parse ( ) ?;
75
58
76
59
println ! ( "Starting download." ) ;
77
60
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)
79
65
. download ( ticket. hash ( ) , Some ( ticket. node_addr ( ) . node_id ) )
80
66
. await ?;
81
67
82
68
println ! ( "Finished download." ) ;
83
69
println ! ( "Copying to destination." ) ;
84
70
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 ?;
93
72
94
73
println ! ( "Finished copying." ) ;
95
74
0 commit comments