2024-01-05 14:02:26 +00:00
|
|
|
package metadata
|
|
|
|
|
2024-01-18 17:06:47 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2024-03-03 09:01:04 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-18 17:06:47 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/serialize"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-03-03 08:27:15 +00:00
|
|
|
"github.com/emirpasic/gods/maps/linkedhashmap"
|
2024-01-18 17:06:47 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2024-01-09 13:16:14 +00:00
|
|
|
var (
|
2024-01-18 17:06:47 +00:00
|
|
|
_ Metadata = (*WebAppMetadata)(nil)
|
|
|
|
_ SerializableMetadata = (*WebAppMetadata)(nil)
|
2024-03-03 08:27:15 +00:00
|
|
|
_ SerializableMetadata = (*WebAppFileMap)(nil)
|
2024-03-03 09:28:45 +00:00
|
|
|
_ SerializableMetadata = (*WebAppErrorPages)(nil)
|
2024-01-09 13:16:14 +00:00
|
|
|
)
|
|
|
|
|
2024-03-03 09:28:45 +00:00
|
|
|
type WebAppErrorPages map[int]string
|
|
|
|
|
2024-01-05 14:02:26 +00:00
|
|
|
type WebAppMetadata struct {
|
2024-01-09 13:16:14 +00:00
|
|
|
BaseMetadata
|
2024-03-03 09:34:03 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
TryFiles []string `json:"tryFiles"`
|
|
|
|
ErrorPages WebAppErrorPages `json:"errorPages"`
|
|
|
|
ExtraMetadata ExtraMetadata `json:"extraMetadata"`
|
|
|
|
Paths *WebAppFileMap `json:"paths"`
|
2024-01-05 14:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 09:34:03 +00:00
|
|
|
func NewWebAppMetadata(name string, tryFiles []string, extraMetadata ExtraMetadata, errorPages WebAppErrorPages, paths *WebAppFileMap) *WebAppMetadata {
|
2024-01-05 14:02:26 +00:00
|
|
|
return &WebAppMetadata{
|
|
|
|
Name: name,
|
|
|
|
TryFiles: tryFiles,
|
|
|
|
ExtraMetadata: extraMetadata,
|
|
|
|
ErrorPages: errorPages,
|
|
|
|
Paths: paths,
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 13:16:42 +00:00
|
|
|
func NewEmptyWebAppMetadata() *WebAppMetadata {
|
|
|
|
return &WebAppMetadata{}
|
|
|
|
}
|
2024-01-18 17:06:47 +00:00
|
|
|
|
|
|
|
func (wm *WebAppMetadata) EncodeMsgpack(enc *msgpack.Encoder) error {
|
2024-03-01 04:36:48 +00:00
|
|
|
err := serialize.InitMarshaller(enc, types.MetadataTypeWebApp)
|
2024-01-18 17:06:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
items := make([]interface{}, 5)
|
|
|
|
|
2024-03-03 10:22:13 +00:00
|
|
|
if wm.ErrorPages == nil {
|
|
|
|
wm.ErrorPages = make(WebAppErrorPages)
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:06:47 +00:00
|
|
|
items[0] = wm.Name
|
|
|
|
items[1] = wm.TryFiles
|
2024-03-03 10:22:31 +00:00
|
|
|
items[2] = &wm.ErrorPages
|
2024-03-03 08:27:15 +00:00
|
|
|
items[3] = wm.Paths
|
2024-01-18 17:06:47 +00:00
|
|
|
items[4] = wm.ExtraMetadata
|
|
|
|
|
|
|
|
return enc.Encode(items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *WebAppMetadata) DecodeMsgpack(dec *msgpack.Decoder) error {
|
2024-03-01 04:35:45 +00:00
|
|
|
_, err := serialize.InitUnmarshaller(dec, types.MetadataTypeWebApp)
|
2024-01-18 17:06:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := dec.DecodeArrayLen()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if val != 5 {
|
|
|
|
return errors.New(" Corrupted metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < val; i++ {
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
wm.Name, err = dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
err = dec.Decode(&wm.TryFiles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
err = dec.Decode(&wm.ErrorPages)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 3:
|
2024-03-03 08:27:15 +00:00
|
|
|
err = dec.Decode(&wm.Paths)
|
2024-01-18 17:06:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
err = dec.Decode(&wm.ExtraMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New(" Corrupted metadata")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wm.Type = "web_app"
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-03 08:27:15 +00:00
|
|
|
|
|
|
|
type WebAppFileMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebAppFileMap() *WebAppFileMap {
|
|
|
|
return &WebAppFileMap{*linkedhashmap.New()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wafm *WebAppFileMap) Put(key string, value WebAppMetadataFileReference) {
|
|
|
|
wafm.Map.Put(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wafm *WebAppFileMap) Get(key string) (WebAppMetadataFileReference, bool) {
|
|
|
|
value, found := wafm.Map.Get(key)
|
|
|
|
if !found {
|
|
|
|
return WebAppMetadataFileReference{}, false
|
|
|
|
}
|
|
|
|
return value.(WebAppMetadataFileReference), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wafm *WebAppFileMap) Remove(key string) {
|
|
|
|
wafm.Map.Remove(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wafm *WebAppFileMap) Keys() []string {
|
|
|
|
keys := wafm.Map.Keys()
|
|
|
|
ret := make([]string, len(keys))
|
|
|
|
for i, key := range keys {
|
|
|
|
ret[i] = key.(string)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-03-03 08:33:36 +00:00
|
|
|
func (wafm *WebAppFileMap) Values() []WebAppMetadataFileReference {
|
|
|
|
values := wafm.Map.Values()
|
|
|
|
ret := make([]WebAppMetadataFileReference, len(values))
|
|
|
|
for i, value := range values {
|
|
|
|
ret[i] = value.(WebAppMetadataFileReference)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-03-03 08:27:15 +00:00
|
|
|
func (wafm *WebAppFileMap) Sort() {
|
|
|
|
keys := wafm.Keys()
|
|
|
|
newMap := NewWebAppFileMap()
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
value, _ := wafm.Get(key)
|
|
|
|
newMap.Put(key, value)
|
|
|
|
}
|
|
|
|
wafm.Map = newMap.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wafm *WebAppFileMap) EncodeMsgpack(encoder *msgpack.Encoder) error {
|
|
|
|
wafm.Sort()
|
|
|
|
|
2024-03-03 10:34:27 +00:00
|
|
|
err := encoder.EncodeArrayLen(wafm.Size())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-03 08:27:15 +00:00
|
|
|
for _, key := range wafm.Keys() {
|
|
|
|
value, _ := wafm.Get(key)
|
2024-03-03 09:01:04 +00:00
|
|
|
|
|
|
|
data :=
|
|
|
|
make([]interface{}, 3)
|
|
|
|
|
|
|
|
data[0] = key
|
|
|
|
data[1] = value.Cid.ToBytes()
|
|
|
|
data[2] = value.ContentType
|
|
|
|
|
|
|
|
err := encoder.Encode(data)
|
2024-03-03 08:27:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wafm *WebAppFileMap) DecodeMsgpack(decoder *msgpack.Decoder) error {
|
|
|
|
arrLen, err := decoder.DecodeArrayLen()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-03 12:16:24 +00:00
|
|
|
wafm.Map = *linkedhashmap.New()
|
|
|
|
|
2024-03-03 08:27:15 +00:00
|
|
|
for i := 0; i < arrLen; i++ {
|
2024-03-03 09:01:04 +00:00
|
|
|
data := make([]interface{}, 3)
|
|
|
|
|
|
|
|
if len(data) != 3 {
|
|
|
|
return errors.New("Corrupted metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = decoder.Decode(&data)
|
2024-03-03 08:27:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-03 09:01:04 +00:00
|
|
|
|
|
|
|
path, ok := data[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Corrupted metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
cidData, ok := data[1].([]byte)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Corrupted metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType, ok := data[2].(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Corrupted metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := encoding.CIDFromBytes(cidData)
|
2024-03-03 08:27:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-03 09:01:04 +00:00
|
|
|
|
|
|
|
wafm.Put(path, *NewWebAppMetadataFileReference(cid, contentType))
|
2024-03-03 08:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wafm.Sort()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-03 09:28:45 +00:00
|
|
|
|
2024-03-03 09:41:52 +00:00
|
|
|
func (w *WebAppErrorPages) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
if w == nil || *w == nil {
|
|
|
|
return enc.EncodeMapLen(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := enc.EncodeMapLen(len(*w))
|
2024-03-03 09:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-03 09:41:52 +00:00
|
|
|
for k, v := range *w {
|
|
|
|
if err := enc.EncodeInt(int64(k)); err != nil {
|
2024-03-03 09:28:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-03-03 09:41:52 +00:00
|
|
|
if err := enc.EncodeString(v); err != nil {
|
2024-03-03 09:28:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebAppErrorPages) DecodeMsgpack(dec *msgpack.Decoder) error {
|
2024-03-03 09:41:52 +00:00
|
|
|
if *w == nil {
|
|
|
|
*w = make(map[int]string)
|
|
|
|
}
|
|
|
|
|
2024-03-03 09:28:45 +00:00
|
|
|
mapLen, err := dec.DecodeMapLen()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*w = make(map[int]string, mapLen)
|
|
|
|
|
|
|
|
for i := 0; i < mapLen; i++ {
|
|
|
|
key, err := dec.DecodeInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
(*w)[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|