fix: GetMetadataByCID has bad logic

This commit is contained in:
Derrick Hammer 2024-01-09 11:01:57 -05:00
parent 71192c4169
commit 26e0a4c9df
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 25 additions and 23 deletions

View File

@ -247,42 +247,44 @@ func (n *NodeImpl) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error)
} }
} }
func (n *NodeImpl) GetMetadataByCID(cid *encoding.CID) (metadata.Metadata, error) { func (n *NodeImpl) GetMetadataByCID(cid *encoding.CID) (md metadata.Metadata, err error) {
var md metadata.Metadata
hashStr, err := cid.Hash.ToString() hashStr, err := cid.Hash.ToString()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if n.metadataCache.Contains(hashStr) { if n.metadataCache.Contains(hashStr) {
bytes, err := n.DownloadBytesByHash(&cid.Hash) md, _ := n.metadataCache.Get(hashStr)
return md.(metadata.Metadata), nil
}
bytes, err := n.DownloadBytesByHash(&cid.Hash)
if err != nil {
return nil, err
}
switch cid.Type {
case types.CIDTypeMetadataMedia, types.CIDTypeBridge: // Both cases use the same deserialization method
md = metadata.NewEmptyMediaMetadata()
err = msgpack.Unmarshal(bytes, md)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case types.CIDTypeMetadataWebapp:
md = metadata.NewEmptyWebAppMetadata()
switch cid.Type { err = msgpack.Unmarshal(bytes, md)
case types.CIDTypeMetadataMedia, types.CIDTypeBridge: // Both cases use the same deserialization method if err != nil {
md = metadata.NewEmptyMediaMetadata() return nil, err
err = msgpack.Unmarshal(bytes, md)
if err != nil {
return nil, err
}
case types.CIDTypeMetadataWebapp:
md = metadata.NewEmptyWebAppMetadata()
err = msgpack.Unmarshal(bytes, md)
if err != nil {
return nil, err
}
default:
return nil, errors.New("unsupported metadata format")
} }
default:
n.metadataCache.Put(hashStr, md) return nil, errors.New("unsupported metadata format")
} }
n.metadataCache.Put(hashStr, md)
return md, nil return md, nil
} }
func (n *NodeImpl) WaitOnConnectedPeers() { func (n *NodeImpl) WaitOnConnectedPeers() {