2021-04-01 18:46:45 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
)
|
|
|
|
|
2021-04-19 17:42:19 +00:00
|
|
|
// NewInMem returns an in-memory implementation of the Storage interface.
|
|
|
|
// This is good for testing and whatnot but ***SHOULD ABSOLUTELY NOT EVER
|
|
|
|
// BE USED IN A PRODUCTION SETTING***, because A) everything will be wiped out
|
|
|
|
// if you restart the server and B) if you store lots of images your RAM use
|
|
|
|
// will absolutely go through the roof.
|
2021-04-01 18:46:45 +00:00
|
|
|
func NewInMem(c *config.Config, log *logrus.Logger) (Storage, error) {
|
|
|
|
return &inMemStorage{
|
|
|
|
stored: make(map[string][]byte),
|
2021-04-19 17:42:19 +00:00
|
|
|
log: log,
|
2021-04-01 18:46:45 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type inMemStorage struct {
|
|
|
|
stored map[string][]byte
|
2021-04-19 17:42:19 +00:00
|
|
|
log *logrus.Logger
|
2021-04-01 18:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) StoreFileAt(path string, data []byte) error {
|
2021-04-19 17:42:19 +00:00
|
|
|
l := s.log.WithField("func", "StoreFileAt")
|
|
|
|
l.Debugf("storing at path %s", path)
|
2021-04-01 18:46:45 +00:00
|
|
|
s.stored[path] = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) RetrieveFileFrom(path string) ([]byte, error) {
|
2021-04-19 17:42:19 +00:00
|
|
|
l := s.log.WithField("func", "RetrieveFileFrom")
|
|
|
|
l.Debugf("retrieving from path %s", path)
|
2021-04-01 18:46:45 +00:00
|
|
|
d, ok := s.stored[path]
|
2021-05-08 12:25:55 +00:00
|
|
|
if !ok || len(d) == 0 {
|
2021-04-01 18:46:45 +00:00
|
|
|
return nil, fmt.Errorf("no data found at path %s", path)
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
2021-04-19 17:42:19 +00:00
|
|
|
|
|
|
|
func (s *inMemStorage) ListKeys() ([]string, error) {
|
|
|
|
keys := []string{}
|
|
|
|
for k := range s.stored {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) RemoveFileAt(path string) error {
|
|
|
|
delete(s.stored, path)
|
|
|
|
return nil
|
|
|
|
}
|