Skip to content

Commit

Permalink
Add Inliner CID Builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevina committed Aug 17, 2018
1 parent b6f51d5 commit dd739ab
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions inliner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cidutil

import (
cid "github.com/ipfs/go-cid"
mhash "github.com/multiformats/go-multihash"
)

// Inliner is a cid.Builder that will use the id multihash when the
// size of the content is no more than limit
type Inliner struct {
base cid.Builder
limit int
}

// GetCodec implements the cid.Builder interface
func (p Inliner) GetCodec() uint64 {
return p.base.GetCodec()
}

// WithCodec implements the cid.Builder interface
func (p Inliner) WithCodec(c uint64) cid.Builder {
return Inliner{p.base.WithCodec(c), p.limit}
}

// Sum implements the cid.Builder interface
func (p Inliner) Sum(data []byte) (*cid.Cid, error) {
if len(data) > p.limit {
return p.base.Sum(data)
}
return cid.V1Builder{Codec: p.base.GetCodec(), MhType: mhash.ID}.Sum(data)
}

0 comments on commit dd739ab

Please sign in to comment.