backend: Context in blob store interface signature

This commit is contained in:
Kevin Lin 2024-02-04 11:14:37 -08:00
parent 15d8d08c22
commit 387383f19d
No known key found for this signature in database
GPG key ID: A8F9E05B9AB4D240
5 changed files with 14 additions and 12 deletions

View file

@ -1,6 +1,7 @@
package blobstore package blobstore
import ( import (
"context"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -29,11 +30,11 @@ func NewLocalBlobStore(root string) BlobStore {
} }
} }
func (l *localBlobStore) Get(key string) (io.ReadCloser, error) { func (l *localBlobStore) Get(ctx context.Context, key string) (io.ReadCloser, error) {
return os.Open(l.resolvePath(key)) return os.Open(l.resolvePath(key))
} }
func (l *localBlobStore) Put(key string, content io.Reader) (string, error) { func (l *localBlobStore) Put(ctx context.Context, key string, content io.Reader) (string, error) {
path := pathlib.Safe(l.resolvePath(key)) path := pathlib.Safe(l.resolvePath(key))
parent := filepath.Dir(path) parent := filepath.Dir(path)
@ -55,7 +56,7 @@ func (l *localBlobStore) Put(key string, content io.Reader) (string, error) {
return key, nil return key, nil
} }
func (l *localBlobStore) Delete(key string) error { func (l *localBlobStore) Delete(ctx context.Context, key string) error {
return os.Remove(l.resolvePath(key)) return os.Remove(l.resolvePath(key))
} }

View file

@ -2,6 +2,7 @@
package blobstore package blobstore
import ( import (
"context"
"io" "io"
) )
@ -12,13 +13,13 @@ import (
type BlobStore interface { type BlobStore interface {
// Get retrieves a blob by key, returning an io.ReadCloser capable of streaming the blob // Get retrieves a blob by key, returning an io.ReadCloser capable of streaming the blob
// contents. Callers should close the returned blob to avoid leaks. // contents. Callers should close the returned blob to avoid leaks.
Get(key string) (io.ReadCloser, error) Get(ctx context.Context, key string) (io.ReadCloser, error)
// Put creates a new blob with the specified key and contents, and returns a normalized key // Put creates a new blob with the specified key and contents, and returns a normalized key
// that can be used for future R/W. // that can be used for future R/W.
// //
// Note that the returned key may be identical to that supplied in the original request; // Note that the returned key may be identical to that supplied in the original request;
// the behavior is implementation-defined. // the behavior is implementation-defined.
Put(key string, content io.Reader) (string, error) Put(ctx context.Context, key string, content io.Reader) (string, error)
// Delete deletes a blob by key. // Delete deletes a blob by key.
Delete(key string) error Delete(ctx context.Context, key string) error
} }

View file

@ -61,7 +61,7 @@ func TestItemService_AddAttachment(t *testing.T) {
assert.Equal(t, path.Join(tGroup.ID.String(), "documents"), path.Dir(storedPath)) assert.Equal(t, path.Join(tGroup.ID.String(), "documents"), path.Dir(storedPath))
// Check that the file contents are correct // Check that the file contents are correct
bts, err := bs.Get(storedPath) bts, err := bs.Get(context.Background(), storedPath)
require.NoError(t, err) require.NoError(t, err)
buf, err := io.ReadAll(bts) buf, err := io.ReadAll(bts)
require.NoError(t, err) require.NoError(t, err)

View file

@ -68,7 +68,7 @@ func (r *DocumentRepository) Read(ctx context.Context, id uuid.UUID) (io.ReadClo
return nil, err return nil, err
} }
content, err := r.bs.Get(doc.Path) content, err := r.bs.Get(ctx, doc.Path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -84,7 +84,7 @@ func (r *DocumentRepository) Create(ctx context.Context, gid uuid.UUID, doc Docu
key := r.blobKey(gid, ext) key := r.blobKey(gid, ext)
path, err := r.bs.Put(key, doc.Content) path, err := r.bs.Put(ctx, key, doc.Content)
if err != nil { if err != nil {
return DocumentOut{}, err return DocumentOut{}, err
} }
@ -109,7 +109,7 @@ func (r *DocumentRepository) Delete(ctx context.Context, id uuid.UUID) error {
return err return err
} }
err = r.bs.Delete(doc.Path) err = r.bs.Delete(ctx, doc.Path)
if err != nil { if err != nil {
return err return err
} }

View file

@ -88,7 +88,7 @@ func TestDocumentRepository_CreateUpdateDelete(t *testing.T) {
ensureRead := func() { ensureRead := func() {
// Read Document // Read Document
bts, err := r.bs.Get(got.Path) bts, err := r.bs.Get(tt.args.ctx, got.Path)
require.NoError(t, err) require.NoError(t, err)
buf, err := io.ReadAll(bts) buf, err := io.ReadAll(bts)
require.NoError(t, err) require.NoError(t, err)
@ -107,7 +107,7 @@ func TestDocumentRepository_CreateUpdateDelete(t *testing.T) {
err = r.Delete(tt.args.ctx, got.ID) err = r.Delete(tt.args.ctx, got.ID)
require.NoError(t, err) require.NoError(t, err)
_, err = r.bs.Get(got.Path) _, err = r.bs.Get(tt.args.ctx, got.Path)
require.Error(t, err) require.Error(t, err)
}) })
} }