@@ -38,5 +38,247 @@ Total: 328 B
38
38
Collection: bafkr4ie3xsx3vdsbflainnk6p4xs4h2hq3hdmuasuoflkgybvnsbljb3ke
39
39
```
40
40
41
+ ``` python {{ title: 'python' }}
42
+ import os
43
+ import sys
44
+ import threading
45
+ import time
46
+ import shutil
47
+ import datetime
48
+ from iroh import IrohNode, SetTagOption, WrapOption, AddProgressType
49
+
50
+ class AddCallback :
51
+ def progress (self , event ):
52
+ t = event.type()
53
+ if t == AddProgressType.FOUND :
54
+ print (" AddProgress - Found:" )
55
+ found = event.as_found()
56
+ print (f " \t id: { found.id} , name: { found.name} , size: { found.size} " )
57
+ elif t == AddProgressType.PROGRESS :
58
+ print (" AddProgress - Progress:" )
59
+ progress = event.as_progress()
60
+ print (f " \t id: { progress.id} , offset: { progress.offset} " )
61
+ elif t == AddProgressType.DONE :
62
+ print (" AddProgress - Done:" )
63
+ done = event.as_done()
64
+ print (f " \t id: { done.id} , hash: { done.hash.to_string()} " )
65
+ elif t == AddProgressType.ALL_DONE :
66
+ print (" AddProgress - AllDone:" )
67
+ all_done = event.as_all_done()
68
+ print (f " \t hash: { all_done.hash.to_string()} , format: { all_done.format} , tag: { all_done.tag.to_string()} " )
69
+ elif t == AddProgressType.ABORT :
70
+ print (" AddProgress - Abort:" )
71
+ abort = event.as_abort()
72
+ print (f " \t error: { abort.error} " )
73
+ else :
74
+ print (" Unknown AddProgress event:" , event)
75
+
76
+ # Create folder
77
+ os.mkdir(" tmp" )
78
+
79
+ try :
80
+ path = os.path.abspath(os.path.join(" tmp" ))
81
+ print (" Created dir \" tmp\" " )
82
+
83
+ file_names = [" foo" , " bar" , " bat" ]
84
+
85
+ # Create three files in the folder
86
+ for file_name in file_names:
87
+ file_path = os.path.join(" tmp" , file_name)
88
+ with open (file_path, " w" ) as f:
89
+ f.write(f " { file_name} " )
90
+ print (f " Created file { file_path} " )
91
+
92
+ node = IrohNode(" iroh_data_dir" )
93
+
94
+ # When `in_place` is True, iroh will NOT copy over the files into its
95
+ # internal database. Only use this option when you know the file will never
96
+ # move or change.
97
+ in_place = False
98
+
99
+ # Iroh blobs can be "tagged" with human-readable names, this creates a tag
100
+ # automatically.
101
+ tag = SetTagOption.auto()
102
+
103
+ # When adding a single file, if you use `WrapOption.wrap()`, you will turn
104
+ # the single file into a collection with one entry.
105
+ wrap = WrapOption.no_wrap()
106
+
107
+ # You can use this callback to react to progress updates.
108
+ callback = AddCallback()
109
+
110
+ # Import the directory, creating one blob for each file, and one metadata
111
+ # blob that stores the file names for each blob.
112
+ # Also creates a 'collection' from the directory, grouping together the
113
+ # blobs.
114
+
115
+ node.blobs_add_from_path(path, in_place, tag, wrap, callback)
116
+ except Exception as e:
117
+ print (" error: " , e)
118
+
119
+ # cleanup dir
120
+ shutil.rmtree(" tmp" )
121
+
122
+ # Output:
123
+ # Created dir "tmp"
124
+ # Created file tmp/foo
125
+ # Created file tmp/bar
126
+ # Created file tmp/bat
127
+ # AddProgress - Found:
128
+ # id: 1, name: $HOME/tmp/bar, size: 3
129
+ # AddProgress - Found:
130
+ # id: 0, name: $HOME/tmp/foo, size: 3
131
+ # AddProgress - Found:
132
+ # id: 2, name: $HOME/tmp/bat, size: 3
133
+ # AddProgress - Progress:
134
+ # id: 2, offset: 3
135
+ # AddProgress - Progress:
136
+ # id: 0, offset: 3
137
+ # AddProgress - Done:
138
+ # id: 0, hash: bafkr4iae4c5tt4yldi76xcpvg3etxykqkvec352im5fqbutolj2xo5yc5e
139
+ # AddProgress - Done:
140
+ # id: 2, hash: bafkr4iabccdb2eyeu764xoewbcqv62sjaggxibtmxx5tnmwer3wp3rquq4
141
+ # AddProgress - Progress:
142
+ # id: 1, offset: 3
143
+ # AddProgress - Done:
144
+ # id: 1, hash: bafkr4ihs5cl65v6sa3gykxkecwmpuuq2xr22vfuvh2l4amgjmewdbqjjhu
145
+ # AddProgress - AllDone:
146
+ # hash: bafkr4iaotzhxuiak7eusnngngnwsqdu4crf4lmdxzkbhuebunevecjzkim, format: BlobFormat.HASH_SEQ, tag: "auto-2023-12-09T22:25:45.296Z"
147
+
148
+ ```
149
+
150
+ ``` swift {{ title: 'go' }}
151
+ package main
152
+
153
+ import (
154
+ " fmt"
155
+ " os"
156
+ " path/filepath"
157
+
158
+ " github.com/n0-computer/iroh-ffi/iroh-go/iroh"
159
+ )
160
+
161
+ type addCallback struct {
162
+ }
163
+
164
+ func (a addCallback) Progress (event * iroh.AddProgress ) * iroh.IrohError {
165
+ switch t : = event.Type (); t {
166
+ case iroh.AddProgressTypeFound :
167
+ fmt.Println (" AddProgress - Found:" )
168
+ found : = event.AsFound ()
169
+ fmt.Printf (" \t id: %d, name: %s, size: %d\n " , found.Id , found.Name , found.Size )
170
+ case iroh.AddProgressTypeProgress :
171
+ fmt.Println (" AddProgress - Progress:" )
172
+ progress : = event.AsProgress ()
173
+ fmt.Printf (" \t id: %d, offset: %d\n " , progress.Id , progress.Offset )
174
+ case iroh.AddProgressTypeDone :
175
+ fmt.Println (" AddProgress - Done:" )
176
+ done : = event.AsDone ()
177
+ fmt.Printf (" \t id: %d, hash: %s\n " , done.Id , done.Hash .ToString ())
178
+ case iroh.AddProgressTypeAllDone :
179
+ fmt.Println (" AddProgress - AllDone:" )
180
+ allDone : = event.AsAllDone ()
181
+ fmt.Printf (" \t hash: %s, format: %v, tag: %s\n " , allDone.Hash .ToString (), allDone.Format , allDone.Tag .ToString ())
182
+ case iroh.AddProgressTypeAbort :
183
+ fmt.Println (" AddProgress - Abort:" )
184
+ abort : = event.AsAbort ()
185
+ fmt.Printf (" \t error: %s" , abort.Error )
186
+ default :
187
+ fmt.Println (" Unknown AddProgress event: %v" , event)
188
+ return & iroh.IrohError {}
189
+ }
190
+ return nil
191
+ }
192
+
193
+ func main () {
194
+ // create folder
195
+ if err : = os.Mkdir (" tmp" , os.ModePerm ); err != nil {
196
+ panic (err)
197
+ }
198
+ defer func () {
199
+ os.RemoveAll (" tmp" )
200
+ fmt.Println (" Removed dir 'tmp'" )
201
+ }()
202
+
203
+ path, err : = filepath.Abs (filepath.Join (" tmp" ))
204
+ if err != nil {
205
+ panic (err)
206
+ }
207
+
208
+ fmt.Println (" Created dir \" tmp\" " )
209
+
210
+ fileNames : = []string {" foo" , " bar" , " bat" }
211
+ // create three files in the folder
212
+ for _ , fileName : = range fileNames {
213
+ path : = filepath.Join (" tmp" , fileName)
214
+ f, err : = os.Create (path)
215
+ if err != nil {
216
+ panic (err)
217
+ }
218
+ _ , err = f.WriteString (fmt.Sprintf (" %s" , fileName))
219
+ if err != nil {
220
+ panic (err)
221
+ }
222
+ fmt.Printf (" Created file %q\n " , path)
223
+ }
224
+
225
+ node, err : = iroh.NewIrohNode (" iroh_data_dir" )
226
+ if err != nil {
227
+ panic (err)
228
+ }
229
+
230
+ // when `inPlace` is true, iroh will NOT copy over the files into its
231
+ // internal database. Only use this option when you know the file will never
232
+ // move or change
233
+ inPlace : = false
234
+ // iroh blobs can be "tagged" with human readable names, this creates a tag
235
+ // automatically
236
+ tag : = iroh.SetTagOptionAuto ()
237
+ // when adding a single file, if you use `iroh.WrapOptionWrap`, you will turn
238
+ // the single file into a collection with one entry
239
+ wrap : = iroh.WrapOptionNoWrap ()
240
+ // you can use this callback to react to progress updates
241
+ callback : = addCallback {}
242
+
243
+ // import the directory, creating one blob for each file, and one metadata
244
+ // blob that stores the file names for each blob
245
+ // also creates a 'collection' from the directory, grouping together the
246
+ // blobs
247
+
248
+ err = node.BlobsAddFromPath (path, inPlace, tag, wrap, callback)
249
+ if err != nil {
250
+ panic (err)
251
+ }
252
+
253
+ // Output:
254
+ // Created dir "tmp"
255
+ // Created file "tmp/foo"
256
+ // Created file "tmp/bar"
257
+ // Created file "tmp/bat"
258
+ // AddProgress - Found:
259
+ // id: 2, name: $HOME/tmp/bar, size: 3
260
+ // AddProgress - Found:
261
+ // id: 0, name: $HOME/tmp/foo, size: 3
262
+ // AddProgress - Found:
263
+ // id: 1, name: $HOME/tmp/bat, size: 3
264
+ // AddProgress - Progress:
265
+ // id: 1, offset: 3
266
+ // AddProgress - Done:
267
+ // id: 1, hash: bafkr4iabccdb2eyeu764xoewbcqv62sjaggxibtmxx5tnmwer3wp3rquq4
268
+ // AddProgress - Progress:
269
+ // id: 2, offset: 3
270
+ // AddProgress - Progress:
271
+ // id: 0, offset: 3
272
+ // AddProgress - Done:
273
+ // id: 2, hash: bafkr4ihs5cl65v6sa3gykxkecwmpuuq2xr22vfuvh2l4amgjmewdbqjjhu
274
+ // AddProgress - Done:
275
+ // id: 0, hash: bafkr4iae4c5tt4yldi76xcpvg3etxykqkvec352im5fqbutolj2xo5yc5e
276
+ // AddProgress - AllDone:
277
+ // hash: bafkr4iaotzhxuiak7eusnngngnwsqdu4crf4lmdxzkbhuebunevecjzkim, format: 2, tag: "auto-2023-12-09T17:37:42.432Z"
278
+ // Removed dir 'tmp'
279
+ }
280
+
281
+ ```
282
+
41
283
42
284
</CodeGroup >
0 commit comments