diff --git a/inliner.go b/inliner.go new file mode 100644 index 0000000..b08f298 --- /dev/null +++ b/inliner.go @@ -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) +}