github.com/ethersphere/bee/v2@v2.2.0/pkg/manifest/manifest.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package manifest contains the abstractions needed for 6 // collection representation in Swarm. 7 package manifest 8 9 import ( 10 "context" 11 "errors" 12 13 "github.com/ethersphere/bee/v2/pkg/file" 14 "github.com/ethersphere/bee/v2/pkg/swarm" 15 ) 16 17 const DefaultManifestType = ManifestMantarayContentType 18 19 const ( 20 RootPath = "/" 21 WebsiteIndexDocumentSuffixKey = "website-index-document" 22 WebsiteErrorDocumentPathKey = "website-error-document" 23 EntryMetadataContentTypeKey = "Content-Type" 24 EntryMetadataFilenameKey = "Filename" 25 ) 26 27 var ( 28 // ErrNotFound is returned when an Entry is not found in the manifest. 29 ErrNotFound = errors.New("manifest: not found") 30 31 // ErrInvalidManifestType is returned when an unknown manifest type 32 // is provided to the function. 33 ErrInvalidManifestType = errors.New("manifest: invalid type") 34 35 // ErrMissingReference is returned when the reference for the manifest file 36 // is missing. 37 ErrMissingReference = errors.New("manifest: missing reference") 38 ) 39 40 // StoreSizeFunc is a callback on every content size that will be stored by 41 // the Store function. 42 type StoreSizeFunc func(int64) error 43 44 // Interface for operations with manifest. 45 type Interface interface { 46 // Type returns manifest implementation type information 47 Type() string 48 // Add a manifest entry to the specified path. 49 Add(context.Context, string, Entry) error 50 // Remove a manifest entry on the specified path. 51 Remove(context.Context, string) error 52 // Lookup returns a manifest entry if one is found in the specified path. 53 Lookup(context.Context, string) (Entry, error) 54 // HasPrefix tests whether the specified prefix path exists. 55 HasPrefix(context.Context, string) (bool, error) 56 // Store stores the manifest, returning the resulting address. 57 Store(context.Context, ...StoreSizeFunc) (swarm.Address, error) 58 // IterateAddresses is used to iterate over chunks addresses for 59 // the manifest. 60 IterateAddresses(context.Context, swarm.AddressIterFunc) error 61 } 62 63 // Entry represents a single manifest entry. 64 type Entry interface { 65 // Reference returns the address of the file. 66 Reference() swarm.Address 67 // Metadata returns the metadata of the file. 68 Metadata() map[string]string 69 } 70 71 // NewDefaultManifest creates a new manifest with default type. 72 func NewDefaultManifest( 73 ls file.LoadSaver, 74 encrypted bool, 75 ) (Interface, error) { 76 return NewManifest(DefaultManifestType, ls, encrypted) 77 } 78 79 // NewDefaultManifestReference creates a new manifest with default type. 80 func NewDefaultManifestReference( 81 reference swarm.Address, 82 ls file.LoadSaver, 83 ) (Interface, error) { 84 return NewManifestReference(DefaultManifestType, reference, ls) 85 } 86 87 // NewManifest creates a new manifest. 88 func NewManifest( 89 manifestType string, 90 ls file.LoadSaver, 91 encrypted bool, 92 ) (Interface, error) { 93 switch manifestType { 94 case ManifestSimpleContentType: 95 return NewSimpleManifest(ls) 96 case ManifestMantarayContentType: 97 return NewMantarayManifest(ls, encrypted) 98 default: 99 return nil, ErrInvalidManifestType 100 } 101 } 102 103 // NewManifestReference loads existing manifest. 104 func NewManifestReference( 105 manifestType string, 106 reference swarm.Address, 107 ls file.LoadSaver, 108 ) (Interface, error) { 109 switch manifestType { 110 case ManifestSimpleContentType: 111 return NewSimpleManifestReference(reference, ls) 112 case ManifestMantarayContentType: 113 return NewMantarayManifestReference(reference, ls) 114 default: 115 return nil, ErrInvalidManifestType 116 } 117 } 118 119 type manifestEntry struct { 120 reference swarm.Address 121 metadata map[string]string 122 } 123 124 // NewEntry creates a new manifest entry. 125 func NewEntry(reference swarm.Address, metadata map[string]string) Entry { 126 return &manifestEntry{ 127 reference: reference, 128 metadata: metadata, 129 } 130 } 131 132 func (e *manifestEntry) Reference() swarm.Address { 133 return e.reference 134 } 135 136 func (e *manifestEntry) Metadata() map[string]string { 137 return e.metadata 138 }