feat: split off meta parsing to ParseMetadata

This commit is contained in:
Derrick Hammer 2024-03-07 16:53:55 -05:00
parent fc212ef246
commit cc2964e80f
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 17 additions and 5 deletions

View File

@ -331,25 +331,38 @@ func (s *StorageService) GetMetadataByCID(cid *encoding.CID) (md metadata.Metada
return nil, err return nil, err
} }
md, err = s.ParseMetadata(bytes, cid)
if err != nil {
return nil, err
}
s.metadataCache.Put(hashStr, md)
return md, nil
}
func (s *StorageService) ParseMetadata(bytes []byte, cid *encoding.CID) (metadata.Metadata, error) {
var md metadata.Metadata
switch cid.Type { switch cid.Type {
case types.CIDTypeMetadataMedia, types.CIDTypeBridge: // Both cases use the same deserialization method case types.CIDTypeMetadataMedia, types.CIDTypeBridge: // Both cases use the same deserialization method
md = metadata.NewEmptyMediaMetadata() md = metadata.NewEmptyMediaMetadata()
err = msgpack.Unmarshal(bytes, md) err := msgpack.Unmarshal(bytes, md)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case types.CIDTypeMetadataWebapp: case types.CIDTypeMetadataWebapp:
md = metadata.NewEmptyWebAppMetadata() md = metadata.NewEmptyWebAppMetadata()
err = msgpack.Unmarshal(bytes, md) err := msgpack.Unmarshal(bytes, md)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case types.CIDTypeDirectory: case types.CIDTypeDirectory:
md = metadata.NewEmptyDirectoryMetadata() md = metadata.NewEmptyDirectoryMetadata()
err = msgpack.Unmarshal(bytes, md) err := msgpack.Unmarshal(bytes, md)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -357,7 +370,5 @@ func (s *StorageService) GetMetadataByCID(cid *encoding.CID) (md metadata.Metada
return nil, ErrUnsupportedMetaFormat return nil, ErrUnsupportedMetaFormat
} }
s.metadataCache.Put(hashStr, md)
return md, nil return md, nil
} }

View File

@ -15,5 +15,6 @@ type StorageService interface {
DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error)
DownloadBytesByCID(cid *encoding.CID) ([]byte, error) DownloadBytesByCID(cid *encoding.CID) ([]byte, error)
GetMetadataByCID(cid *encoding.CID) (metadata.Metadata, error) GetMetadataByCID(cid *encoding.CID) (metadata.Metadata, error)
ParseMetadata(bytes []byte, cid *encoding.CID) (metadata.Metadata, error)
Service Service
} }