47 lines
714 B
Go
47 lines
714 B
Go
package store
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"io/ioutil"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type MemoryStore struct {
|
|
data map[string][]byte
|
|
}
|
|
|
|
func NewMemoryStore() *MemoryStore {
|
|
return &MemoryStore{data: map[string][]byte{}}
|
|
}
|
|
|
|
func (s *MemoryStore) Add(r io.Reader) (string, error) {
|
|
id := uuid.Must(uuid.NewRandom()).String()
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
s.data[id] = data
|
|
return id, nil
|
|
}
|
|
|
|
func (s *MemoryStore) Delete(id string) error {
|
|
delete(s.data, id)
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryStore) Get(id string) (io.ReadCloser, error) {
|
|
data, ok := s.data[id]
|
|
if !ok {
|
|
return nil, ErrNoSuchItem
|
|
}
|
|
br := bytes.NewReader(data)
|
|
|
|
r := io.NopCloser(br)
|
|
|
|
return r, nil
|
|
}
|