diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-17 14:39:42 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-17 14:39:42 -0600 |
commit | 7b7bdcf57a5fa1e02041e3ef563c55f31d908f67 (patch) | |
tree | 7e1c19be77f606abb99e2ad82c84545b4891c6f3 /srv/src/post/asset.go | |
parent | 9a67ef921100d16294b30b7d111206789c8e1feb (diff) |
Initial implementation of admin assets page
Diffstat (limited to 'srv/src/post/asset.go')
-rw-r--r-- | srv/src/post/asset.go | 69 |
1 files changed, 15 insertions, 54 deletions
diff --git a/srv/src/post/asset.go b/srv/src/post/asset.go index 18af8f6..a7b605b 100644 --- a/srv/src/post/asset.go +++ b/srv/src/post/asset.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "sync" ) var ( @@ -28,6 +27,9 @@ type AssetStore interface { // Delete's the body stored for the id, if any. Delete(id string) error + + // List returns all ids which are currently stored. + List() ([]string, error) } type assetStore struct { @@ -86,68 +88,27 @@ func (s *assetStore) Delete(id string) error { return err } -//////////////////////////////////////////////////////////////////////////////// - -type cachedAssetStore struct { - inner AssetStore - m sync.Map -} - -// NewCachedAssetStore wraps an AssetStore in an in-memory cache. -func NewCachedAssetStore(assetStore AssetStore) AssetStore { - return &cachedAssetStore{ - inner: assetStore, - } -} - -func (s *cachedAssetStore) Set(id string, from io.Reader) error { +func (s *assetStore) List() ([]string, error) { - buf := new(bytes.Buffer) - from = io.TeeReader(from, buf) + rows, err := s.db.Query(`SELECT id FROM assets ORDER BY id ASC`) - if err := s.inner.Set(id, from); err != nil { - return err + if err != nil { + return nil, fmt.Errorf("querying: %w", err) } - s.m.Store(id, buf.Bytes()) - return nil -} - -func (s *cachedAssetStore) Get(id string, into io.Writer) error { + defer rows.Close() - if bodyI, ok := s.m.Load(id); ok { + var ids []string - if err, ok := bodyI.(error); ok { - return err - } + for rows.Next() { - if _, err := io.Copy(into, bytes.NewReader(bodyI.([]byte))); err != nil { - return fmt.Errorf("writing body to io.Writer: %w", err) + var id string + if err := rows.Scan(&id); err != nil { + return nil, fmt.Errorf("scanning row: %w", err) } - return nil + ids = append(ids, id) } - buf := new(bytes.Buffer) - into = io.MultiWriter(into, buf) - - if err := s.inner.Get(id, into); errors.Is(err, ErrAssetNotFound) { - s.m.Store(id, err) - return err - } else if err != nil { - return err - } - - s.m.Store(id, buf.Bytes()) - return nil -} - -func (s *cachedAssetStore) Delete(id string) error { - - if err := s.inner.Delete(id); err != nil { - return err - } - - s.m.Delete(id) - return nil + return ids, nil } |