diff --git a/cid/cid.go b/cid/cid.go index 2fb1666..30a5245 100644 --- a/cid/cid.go +++ b/cid/cid.go @@ -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) } diff --git a/service/files_service.go b/service/files_service.go index 540154c..4dd21f2 100644 --- a/service/files_service.go +++ b/service/files_service.go @@ -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