github.com/avenga/couper@v1.12.2/assets/asset.go (about) 1 package assets 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 ) 8 9 var _ io.WriterTo = &AssetFile{} 10 11 type AssetFile struct { 12 bytes []byte 13 size string 14 } 15 16 type Box struct { 17 files map[string]*AssetFile 18 } 19 20 // Assets is referenced by the servers file serving. 21 var Assets *Box 22 23 func (af *AssetFile) Bytes() []byte { 24 return af.bytes[:] 25 } 26 27 func (af *AssetFile) WriteTo(w io.Writer) (int64, error) { 28 n, err := w.Write(af.bytes) 29 return int64(n), err 30 } 31 32 func (af *AssetFile) Size() string { 33 return af.size 34 } 35 36 func New() *Box { 37 return &Box{ 38 files: map[string]*AssetFile{}, 39 } 40 } 41 42 func (b *Box) Open(file string) (*AssetFile, error) { 43 if f, ok := b.files[file]; ok { 44 return f, nil 45 } 46 return nil, errors.New("file not found: " + file) 47 } 48 49 func (b *Box) MustOpen(file string) *AssetFile { 50 f, err := b.Open(file) 51 if err != nil { 52 return &AssetFile{bytes: []byte("not found")} 53 } 54 return f 55 } 56 57 func (af *AssetFile) String() string { 58 buf := &bytes.Buffer{} 59 _, _ = af.WriteTo(buf) 60 return buf.String() 61 }