libs5-go/metadata/file_version.go

117 lines
2.5 KiB
Go
Raw Normal View History

2024-01-04 13:20:37 +00:00
package metadata
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"github.com/emirpasic/gods/maps/linkedhashmap"
2024-01-04 13:20:37 +00:00
"github.com/vmihailenco/msgpack/v5"
)
type FileVersion struct {
Ts int `json:"ts"`
EncryptedCID *encoding.EncryptedCID `json:"encryptedCID,string"`
2024-01-04 15:27:23 +00:00
PlaintextCID *encoding.CID `json:"cid,string"`
2024-01-04 13:20:37 +00:00
Thumbnail *FileVersionThumbnail `json:"thumbnail"`
Hashes []*encoding.Multihash `json:"hashes"`
Ext map[string]interface{} `json:"ext"`
}
func NewFileVersion(ts int, encryptedCID *encoding.EncryptedCID, plaintextCID *encoding.CID, thumbnail *FileVersionThumbnail, hashes []*encoding.Multihash, ext map[string]interface{}) *FileVersion {
return &FileVersion{
Ts: ts,
EncryptedCID: encryptedCID,
PlaintextCID: plaintextCID,
Thumbnail: thumbnail,
Hashes: hashes,
Ext: ext,
}
}
func (fv *FileVersion) EncodeMsgpack(enc *msgpack.Encoder) error {
fmap := &fileVersionSerializationMap{*linkedhashmap.New()}
fmap.Put(8, fv.Ts)
2024-01-04 13:20:37 +00:00
if fv.EncryptedCID != nil {
fmap.Put(1, fv.EncryptedCID)
2024-01-04 13:20:37 +00:00
}
if fv.PlaintextCID != nil {
fmap.Put(2, fv.PlaintextCID)
2024-01-04 13:20:37 +00:00
}
if len(fv.Hashes) > 0 {
hashesData := make([][]byte, len(fv.Hashes))
for i, hash := range fv.Hashes {
hashesData[i] = hash.FullBytes()
2024-01-04 13:20:37 +00:00
}
fmap.Put(9, hashesData)
2024-01-04 13:20:37 +00:00
}
if fv.Thumbnail != nil {
fmap.Put(10, fv.Thumbnail)
2024-01-04 13:20:37 +00:00
}
return enc.Encode(fmap)
2024-01-04 13:20:37 +00:00
}
func (fv *FileVersion) DecodeMsgpack(dec *msgpack.Decoder) error {
mapLen, err := dec.DecodeMapLen()
if err != nil {
return err
}
for i := 0; i < mapLen; i++ {
key, err := dec.DecodeInt8()
if err != nil {
return err
}
switch key {
case int8(1):
err := dec.Decode(&fv.EncryptedCID)
if err != nil {
return err
}
case int8(2):
err := dec.Decode(&fv.PlaintextCID)
if err != nil {
return err
}
case int8(8):
err := dec.Decode(&fv.Ts)
if err != nil {
return err
}
case int8(9):
hashesData, err := dec.DecodeSlice()
if err != nil {
return err
}
fv.Hashes = make([]*encoding.Multihash, len(hashesData))
for i, hashData := range hashesData {
hashBytes := hashData.([]byte)
fv.Hashes[i] = encoding.NewMultihash(hashBytes)
}
case int8(10):
err := dec.Decode(&fv.Thumbnail)
if err != nil {
return err
}
}
}
return nil
}
func (fv *FileVersion) CID() *encoding.CID {
if fv.PlaintextCID != nil {
return fv.PlaintextCID
}
return &fv.EncryptedCID.OriginalCID
2024-01-04 15:27:56 +00:00
}