github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/content/byteStorage.go (about) 1 package content 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 ) 7 8 // ByteStorage wraps a Storage and provides it with 9 // SetBytes and GetBytes methods 10 type ByteStorage struct { 11 Storage 12 } 13 14 // NewByteStorage returns a new ByteStorage instance, 15 // wrapping the given Storage. 16 func NewByteStorage(s Storage) *ByteStorage { 17 return &ByteStorage{Storage: s} 18 } 19 20 // GetBytes returns the data stored under the given id as bytes. 21 func (s *ByteStorage) GetBytes(id string) ([]byte, error) { 22 r, err := s.Storage.Get(id) 23 if err != nil { 24 return nil, err 25 } 26 defer r.Close() 27 data, err := ioutil.ReadAll(r) 28 if err != nil { 29 return nil, err 30 } 31 return data, nil 32 } 33 34 // SetBytes initially sets or updates the bytes for the given id. 35 func (s *ByteStorage) SetBytes(id string, data []byte) error { 36 r := bytes.NewReader(data) 37 return s.Storage.Set(id, r) 38 }