feat: add decoding for MediaMetadata

This commit is contained in:
Derrick Hammer 2024-03-01 00:18:07 -05:00
parent fd786ac3c1
commit 7c3ef2ae86
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 247 additions and 4 deletions

View File

@ -1,6 +1,15 @@
package metadata
import "git.lumeweb.com/LumeWeb/libs5-go/encoding"
import (
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"github.com/vmihailenco/msgpack/v5"
)
var (
_ msgpack.CustomDecoder = (*MediaMetadataLinks)(nil)
_ msgpack.CustomEncoder = (*MediaMetadataLinks)(nil)
)
type MediaMetadataLinks struct {
Count int
@ -9,6 +18,53 @@ type MediaMetadataLinks struct {
Tail []*encoding.CID
}
func (m MediaMetadataLinks) EncodeMsgpack(enc *msgpack.Encoder) error {
return errors.New("Not implemented")
}
func (m MediaMetadataLinks) DecodeMsgpack(dec *msgpack.Decoder) error {
data, err := decodeIntMap(dec)
if err != nil {
return err
}
for key, value := range data {
switch key {
case 1:
m.Count = value.(int)
case 2:
head := value.([]interface{})
for _, h := range head {
cid, err := encoding.CIDFromBytes(h.([]byte))
if err != nil {
return err
}
m.Head = append(m.Head, cid)
}
case 3:
collapsed := value.([]interface{})
for _, c := range collapsed {
cid, err := encoding.CIDFromBytes(c.([]byte))
if err != nil {
return err
}
m.Collapsed = append(m.Collapsed, cid)
}
case 4:
tail := value.([]interface{})
for _, t := range tail {
cid, err := encoding.CIDFromBytes(t.([]byte))
if err != nil {
return err
}
m.Tail = append(m.Tail, cid)
}
}
}
return nil
}
func NewMediaMetadataLinks(head []*encoding.CID) *MediaMetadataLinks {
return &MediaMetadataLinks{
Count: len(head),

View File

@ -1,12 +1,24 @@
package metadata
import (
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/serialize"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
)
var (
_ Metadata = (*MediaMetadata)(nil)
_ msgpack.CustomDecoder = (*MediaMetadata)(nil)
_ msgpack.CustomEncoder = (*MediaMetadata)(nil)
_ msgpack.CustomDecoder = (*mediaMap)(nil)
)
type mediaMap map[string][]MediaFormat
type MediaMetadata struct {
Name string
MediaTypes map[string][]MediaFormat
MediaTypes mediaMap
Parents []MetadataParentLink
Details MediaMetadataDetails
Links *MediaMetadataLinks
@ -27,3 +39,92 @@ func NewMediaMetadata(name string, details MediaMetadataDetails, parents []Metad
func NewEmptyMediaMetadata() *MediaMetadata {
return &MediaMetadata{}
}
func (m *MediaMetadata) EncodeMsgpack(enc *msgpack.Encoder) error {
return errors.New("Not implemented")
}
func (m *MediaMetadata) DecodeMsgpack(dec *msgpack.Decoder) error {
kind, err := serialize.InitUnmarshaller(dec, types.MetadataTypeProof, types.MetadataTypeMedia)
if err != nil {
return err
}
switch kind {
case types.MetadataTypeProof:
return m.decodeProof(dec)
case types.MetadataTypeMedia:
return m.decodeMedia(dec)
default:
return errors.New("Invalid metadata type")
}
}
func (m *MediaMetadata) decodeProof(dec *msgpack.Decoder) error {
return errors.New("Not implemented")
}
func (m *MediaMetadata) decodeMedia(dec *msgpack.Decoder) error {
_, err := dec.DecodeArrayLen()
if err != nil {
return err
}
err = dec.Decode(&m.Name)
if err != nil {
return err
}
err = dec.Decode(&m.Details)
if err != nil {
return err
}
err = dec.Decode(&m.Parents)
if err != nil {
return err
}
err = dec.Decode(&m.MediaTypes)
if err != nil {
return err
}
err = dec.Decode(&m.Links)
if err != nil {
return err
}
err = dec.Decode(&m.ExtraMetadata)
if err != nil {
return err
}
return nil
}
func (m *mediaMap) DecodeMsgpack(dec *msgpack.Decoder) error {
mapLen, err := dec.DecodeMapLen()
if err != nil {
return err
}
for i := 0; i < mapLen; i++ {
typ, err := dec.DecodeString()
if err != nil {
return err
}
var formats []MediaFormat
err = dec.Decode(&formats)
if err != nil {
return err
}
(*m)[typ] = formats
}
return nil
}

View File

@ -1,5 +1,15 @@
package metadata
import (
"errors"
"github.com/vmihailenco/msgpack/v5"
)
var (
_ msgpack.CustomDecoder = (*MediaMetadataDetails)(nil)
_ msgpack.CustomEncoder = (*MediaMetadataDetails)(nil)
)
type MediaMetadataDetails struct {
Data map[int]interface{}
}
@ -7,3 +17,29 @@ type MediaMetadataDetails struct {
func NewMediaMetadataDetails(data map[int]interface{}) *MediaMetadataDetails {
return &MediaMetadataDetails{Data: data}
}
func (mmd *MediaMetadataDetails) EncodeMsgpack(enc *msgpack.Encoder) error {
return errors.New("Not implemented")
}
func (mmd *MediaMetadataDetails) 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
}
value, err := dec.DecodeInterface()
if err != nil {
return err
}
mmd.Data[int(key)] = value
}
return nil
}

View File

@ -1,8 +1,15 @@
package metadata
import (
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
)
var (
_ msgpack.CustomDecoder = (*MetadataParentLink)(nil)
_ msgpack.CustomEncoder = (*MetadataParentLink)(nil)
)
// MetadataParentLink represents the structure for Metadata Parent Link.
@ -13,6 +20,49 @@ type MetadataParentLink struct {
Signed bool
}
func (m *MetadataParentLink) EncodeMsgpack(enc *msgpack.Encoder) error {
return errors.New("Not implemented")
}
func (m *MetadataParentLink) 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
}
value, err := dec.DecodeInterface()
if err != nil {
return err
}
switch key {
case 0:
m.Type = types.ParentLinkType(value.(int))
case 1:
cid, err := encoding.CIDFromBytes(value.([]byte))
if err != nil {
return err
}
m.CID = cid
}
}
if m.Type == 0 {
m.Type = types.ParentLinkTypeUserIdentity
}
m.Signed = false
return nil
}
// NewMetadataParentLink creates a new MetadataParentLink with the provided values.
func NewMetadataParentLink(cid *encoding.CID, role string, signed bool) *MetadataParentLink {
return &MetadataParentLink{