fix: encode size as uint64 to the end of the cid

This commit is contained in:
Derrick Hammer 2023-05-04 08:16:44 -04:00
parent 479df7eb39
commit 5aca66d919
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 8 additions and 3 deletions

View File

@ -1,11 +1,16 @@
package cid
import (
"encoding/binary"
"github.com/multiformats/go-multibase"
)
func EncodeHashSimple(hash [32]byte) (string, error) {
func EncodeHashSimple(hash [32]byte, size uint64) (string, error) {
sizeBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(sizeBytes, size)
prefixedHash := append([]byte{0x26, 0x1f}, hash[:]...)
prefixedHash = append(prefixedHash, sizeBytes...)
return multibase.Encode(multibase.Base58BTC, prefixedHash)
}

View File

@ -35,7 +35,7 @@ func InitFiles() {
func (f *FilesService) PostUpload() {
ctx := f.Ctx
file, _, err := f.Ctx.FormFile("file")
file, meta, err := f.Ctx.FormFile("file")
if internalErrorCustom(ctx, err, errors.New("invalid file data")) {
return
}
@ -49,7 +49,7 @@ func (f *FilesService) PostUpload() {
hashBytes := blake3.Sum256(buf.Bytes())
hashHex := hex.EncodeToString(hashBytes[:])
fileCid, err := cid.EncodeHashSimple(hashBytes)
fileCid, err := cid.EncodeHashSimple(hashBytes, uint64(meta.Size))
if internalError(ctx, err) {
return