feat: add CID method

This commit is contained in:
Derrick Hammer 2024-01-24 19:22:31 -05:00
parent 6cac5c6a28
commit 7b7c705c0d
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 11 additions and 0 deletions

View File

@ -3,6 +3,8 @@ package storage
import ( import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/portal/db/models" "git.lumeweb.com/LumeWeb/portal/db/models"
"git.lumeweb.com/LumeWeb/portal/interfaces" "git.lumeweb.com/LumeWeb/portal/interfaces"
"io" "io"
@ -18,6 +20,7 @@ type File struct {
hash []byte hash []byte
storage interfaces.StorageService storage interfaces.StorageService
record *models.Upload record *models.Upload
cid *encoding.CID
} }
func NewFile(hash []byte, storage interfaces.StorageService) *File { func NewFile(hash []byte, storage interfaces.StorageService) *File {
@ -145,3 +148,11 @@ func (f *File) Size() uint64 {
return record.Size return record.Size
} }
func (f *File) CID() *encoding.CID {
if f.cid == nil {
multihash := encoding.MultihashFromBytes(f.Hash(), types.HashTypeBlake3)
cid := encoding.NewCID(types.CIDTypeRaw, *multihash, f.Size())
f.cid = cid
}
return f.cid
}