-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |