1
1
package options
2
2
3
3
import (
4
- "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
4
+ "fmt"
5
+ mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
6
+ cid "gx/ipfs/QmYjnkEL7i731PirfVH1sis89evN7jt4otSHw5D2xXXwUV/go-cid"
5
7
)
6
8
7
9
type BlockPutSettings struct {
@@ -17,20 +19,52 @@ type BlockRmSettings struct {
17
19
type BlockPutOption func (* BlockPutSettings ) error
18
20
type BlockRmOption func (* BlockRmSettings ) error
19
21
20
- func BlockPutOptions (opts ... BlockPutOption ) (* BlockPutSettings , error ) {
22
+ func BlockPutOptions (opts ... BlockPutOption ) (* BlockPutSettings , cid. Prefix , error ) {
21
23
options := & BlockPutSettings {
22
24
Codec : "" ,
23
- MhType : multihash .SHA2_256 ,
25
+ MhType : mh .SHA2_256 ,
24
26
MhLength : - 1 ,
25
27
}
26
28
27
29
for _ , opt := range opts {
28
30
err := opt (options )
29
31
if err != nil {
30
- return nil , err
32
+ return nil , cid. Prefix {}, err
31
33
}
32
34
}
33
- return options , nil
35
+
36
+ var pref cid.Prefix
37
+ pref .Version = 1
38
+
39
+ if options .Codec == "" {
40
+ if options .MhType != mh .SHA2_256 || (options .MhLength != - 1 && options .MhLength != 32 ) {
41
+ options .Codec = "protobuf"
42
+ } else {
43
+ options .Codec = "v0"
44
+ }
45
+ }
46
+
47
+ if options .Codec == "v0" && options .MhType == mh .SHA2_256 {
48
+ pref .Version = 0
49
+ }
50
+
51
+ formatval , ok := cid .Codecs [options .Codec ]
52
+ if ! ok {
53
+ return nil , cid.Prefix {}, fmt .Errorf ("unrecognized format: %s" , options .Codec )
54
+ }
55
+
56
+ if options .Codec == "v0" {
57
+ if options .MhType != mh .SHA2_256 || (options .MhLength != - 1 && options .MhLength != 32 ) {
58
+ return nil , cid.Prefix {}, fmt .Errorf ("only sha2-255-32 is allowed with CIDv0" )
59
+ }
60
+ }
61
+
62
+ pref .Codec = formatval
63
+
64
+ pref .MhType = options .MhType
65
+ pref .MhLength = options .MhLength
66
+
67
+ return options , pref , nil
34
68
}
35
69
36
70
func BlockRmOptions (opts ... BlockRmOption ) (* BlockRmSettings , error ) {
0 commit comments