2024-01-04 13:20:37 +00:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
2024-01-04 15:26:44 +00:00
|
|
|
"encoding/json"
|
2024-01-04 15:50:08 +00:00
|
|
|
"fmt"
|
2024-01-04 13:20:37 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
)
|
|
|
|
|
2024-01-04 15:33:57 +00:00
|
|
|
type jsonData = map[string]interface{}
|
|
|
|
|
2024-01-04 15:26:44 +00:00
|
|
|
var names = map[types.MetadataExtension]string{
|
|
|
|
types.MetadataExtensionLicenses: "licenses",
|
|
|
|
types.MetadataExtensionDonationKeys: "donationKeys",
|
|
|
|
types.MetadataExtensionWikidataClaims: "wikidataClaims",
|
|
|
|
types.MetadataExtensionLanguages: "languages",
|
|
|
|
types.MetadataExtensionSourceUris: "sourceUris",
|
|
|
|
types.MetadataExtensionPreviousVersions: "previousVersions",
|
|
|
|
types.MetadataExtensionTimestamp: "timestamp",
|
|
|
|
types.MetadataExtensionOriginalTimestamp: "originalTimestamp",
|
|
|
|
types.MetadataExtensionTags: "tags",
|
|
|
|
types.MetadataExtensionCategories: "categories",
|
|
|
|
types.MetadataExtensionBasicMediaMetadata: "basicMediaMetadata",
|
|
|
|
types.MetadataExtensionViewTypes: "viewTypes",
|
|
|
|
types.MetadataExtensionBridge: "bridge",
|
|
|
|
types.MetadataExtensionRoutingHints: "routingHints",
|
|
|
|
}
|
|
|
|
|
|
|
|
var namesReverse = map[string]types.MetadataExtension{
|
|
|
|
"licenses": types.MetadataExtensionLicenses,
|
|
|
|
"donationKeys": types.MetadataExtensionDonationKeys,
|
|
|
|
"wikidataClaims": types.MetadataExtensionWikidataClaims,
|
|
|
|
"languages": types.MetadataExtensionLanguages,
|
|
|
|
"sourceUris": types.MetadataExtensionSourceUris,
|
|
|
|
"previousVersions": types.MetadataExtensionPreviousVersions,
|
|
|
|
"timestamp": types.MetadataExtensionTimestamp,
|
|
|
|
"originalTimestamp": types.MetadataExtensionOriginalTimestamp,
|
|
|
|
"tags": types.MetadataExtensionTags,
|
|
|
|
"categories": types.MetadataExtensionCategories,
|
|
|
|
"basicMediaMetadata": types.MetadataExtensionBasicMediaMetadata,
|
|
|
|
"viewTypes": types.MetadataExtensionViewTypes,
|
|
|
|
"bridge": types.MetadataExtensionBridge,
|
|
|
|
"routingHints": types.MetadataExtensionRoutingHints,
|
|
|
|
}
|
|
|
|
|
2024-01-04 13:20:37 +00:00
|
|
|
type ExtraMetadata struct {
|
|
|
|
Data map[int]interface{}
|
|
|
|
}
|
2024-01-04 15:50:08 +00:00
|
|
|
type keyValue struct {
|
|
|
|
Key interface{}
|
|
|
|
Value interface{}
|
|
|
|
}
|
2024-01-04 13:20:37 +00:00
|
|
|
|
2024-01-04 15:26:44 +00:00
|
|
|
var _ SerializableMetadata = (*ExtraMetadata)(nil)
|
|
|
|
|
2024-01-04 13:20:37 +00:00
|
|
|
func NewExtraMetadata(data map[int]interface{}) *ExtraMetadata {
|
|
|
|
return &ExtraMetadata{
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 15:26:44 +00:00
|
|
|
func (em ExtraMetadata) MarshalJSON() ([]byte, error) {
|
2024-01-04 15:33:57 +00:00
|
|
|
data, err := em.encode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-04 13:20:37 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:33:57 +00:00
|
|
|
return json.Marshal(data)
|
2024-01-04 15:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (em *ExtraMetadata) UnmarshalJSON(data []byte) error {
|
2024-01-04 15:50:08 +00:00
|
|
|
|
|
|
|
em.Data = make(map[int]interface{})
|
|
|
|
jsonObject := make(map[int]interface{})
|
2024-01-04 15:26:44 +00:00
|
|
|
if err := json.Unmarshal(data, &jsonObject); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, value := range jsonObject {
|
2024-01-04 15:50:08 +00:00
|
|
|
err := em.decodeItem(keyValue{Key: name, Value: value})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (em *ExtraMetadata) decodeItem(pair keyValue) error {
|
|
|
|
var metadataKey int
|
|
|
|
|
|
|
|
// Determine the type of the key and convert it if necessary
|
|
|
|
switch k := pair.Key.(type) {
|
|
|
|
case string:
|
|
|
|
if val, ok := namesReverse[k]; ok {
|
|
|
|
metadataKey = int(val)
|
2024-01-04 15:26:44 +00:00
|
|
|
} else {
|
2024-01-04 15:50:08 +00:00
|
|
|
return fmt.Errorf("unknown key in JSON: %s", k)
|
2024-01-04 15:26:44 +00:00
|
|
|
}
|
2024-01-04 15:50:08 +00:00
|
|
|
case int8:
|
|
|
|
metadataKey = int(k)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported key type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if metadataKey == int(types.MetadataExtensionUpdateCID) {
|
|
|
|
// Convert string to CID bytes for MetadataExtensionUpdateCID
|
2024-01-09 13:23:35 +00:00
|
|
|
cid, err := encoding.CIDFromString(pair.Value.(string))
|
2024-01-04 15:50:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
em.Data[metadataKey] = cid
|
|
|
|
} else {
|
|
|
|
em.Data[metadataKey] = pair.Value
|
2024-01-04 15:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-01-04 13:20:37 +00:00
|
|
|
}
|
2024-01-04 15:26:44 +00:00
|
|
|
|
2024-01-04 13:20:37 +00:00
|
|
|
func (em *ExtraMetadata) 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
|
|
|
|
}
|
2024-01-04 15:50:08 +00:00
|
|
|
|
|
|
|
var value interface{}
|
|
|
|
if key == int8(types.MetadataExtensionUpdateCID) {
|
|
|
|
value, err = dec.DecodeString()
|
|
|
|
} else {
|
|
|
|
value, err = dec.DecodeInterface()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = em.decodeItem(keyValue{Key: key, Value: value})
|
2024-01-04 13:20:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-01-18 17:09:01 +00:00
|
|
|
|
|
|
|
if mapLen == 0 {
|
|
|
|
em.Data = make(map[int]interface{})
|
|
|
|
}
|
|
|
|
|
2024-01-04 13:20:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (em ExtraMetadata) EncodeMsgpack(enc *msgpack.Encoder) error {
|
2024-01-04 15:33:57 +00:00
|
|
|
data, err := em.encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc.Encode(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (em ExtraMetadata) encode() (jsonData, error) {
|
|
|
|
jsonObject := make(map[string]interface{})
|
|
|
|
for key, value := range em.Data {
|
|
|
|
name, ok := names[types.MetadataExtension(key)]
|
|
|
|
if ok {
|
|
|
|
if types.MetadataExtension(key) == types.MetadataExtensionUpdateCID {
|
|
|
|
cid, err := encoding.CIDFromBytes(value.([]byte))
|
|
|
|
var cidString string
|
|
|
|
if err == nil {
|
|
|
|
cidString, err = cid.ToString()
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
jsonObject["updateCID"] = cidString
|
|
|
|
} else {
|
|
|
|
jsonObject["updateCID"] = ""
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
jsonObject[name] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObject, nil
|
2024-01-04 13:20:37 +00:00
|
|
|
}
|