inliner.go 649 Bytes
Newer Older
Kevin Atkinson's avatar
Kevin Atkinson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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 {
	cid.Builder
	Limit int
}

// WithCodec implements the cid.Builder interface
func (p Inliner) WithCodec(c uint64) cid.Builder {
	return Inliner{p.Builder.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.Builder.Sum(data)
	}
	return cid.V1Builder{Codec: p.GetCodec(), MhType: mhash.ID}.Sum(data)
}