From 387383f19dec7382cd33d2d811195fd6fa8bb60a Mon Sep 17 00:00:00 2001 From: Kevin Lin Date: Sun, 4 Feb 2024 11:14:37 -0800 Subject: [PATCH] backend: Context in blob store interface signature --- backend/internal/core/blobstore/local.go | 7 ++++--- backend/internal/core/blobstore/store.go | 7 ++++--- .../core/services/service_items_attachments_test.go | 2 +- backend/internal/data/repo/repo_documents.go | 6 +++--- backend/internal/data/repo/repo_documents_test.go | 4 ++-- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/backend/internal/core/blobstore/local.go b/backend/internal/core/blobstore/local.go index 1563529..750986d 100644 --- a/backend/internal/core/blobstore/local.go +++ b/backend/internal/core/blobstore/local.go @@ -1,6 +1,7 @@ package blobstore import ( + "context" "io" "os" "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)) } -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)) parent := filepath.Dir(path) @@ -55,7 +56,7 @@ func (l *localBlobStore) Put(key string, content io.Reader) (string, error) { 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)) } diff --git a/backend/internal/core/blobstore/store.go b/backend/internal/core/blobstore/store.go index e1bac10..feeb725 100644 --- a/backend/internal/core/blobstore/store.go +++ b/backend/internal/core/blobstore/store.go @@ -2,6 +2,7 @@ package blobstore import ( + "context" "io" ) @@ -12,13 +13,13 @@ import ( type BlobStore interface { // 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. - 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 // that can be used for future R/W. // // Note that the returned key may be identical to that supplied in the original request; // 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(key string) error + Delete(ctx context.Context, key string) error } diff --git a/backend/internal/core/services/service_items_attachments_test.go b/backend/internal/core/services/service_items_attachments_test.go index a0e63e3..e89f780 100644 --- a/backend/internal/core/services/service_items_attachments_test.go +++ b/backend/internal/core/services/service_items_attachments_test.go @@ -61,7 +61,7 @@ func TestItemService_AddAttachment(t *testing.T) { assert.Equal(t, path.Join(tGroup.ID.String(), "documents"), path.Dir(storedPath)) // Check that the file contents are correct - bts, err := bs.Get(storedPath) + bts, err := bs.Get(context.Background(), storedPath) require.NoError(t, err) buf, err := io.ReadAll(bts) require.NoError(t, err) diff --git a/backend/internal/data/repo/repo_documents.go b/backend/internal/data/repo/repo_documents.go index cacf30f..3a06c6e 100644 --- a/backend/internal/data/repo/repo_documents.go +++ b/backend/internal/data/repo/repo_documents.go @@ -68,7 +68,7 @@ func (r *DocumentRepository) Read(ctx context.Context, id uuid.UUID) (io.ReadClo return nil, err } - content, err := r.bs.Get(doc.Path) + content, err := r.bs.Get(ctx, doc.Path) if err != nil { return nil, err } @@ -84,7 +84,7 @@ func (r *DocumentRepository) Create(ctx context.Context, gid uuid.UUID, doc Docu 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 { return DocumentOut{}, err } @@ -109,7 +109,7 @@ func (r *DocumentRepository) Delete(ctx context.Context, id uuid.UUID) error { return err } - err = r.bs.Delete(doc.Path) + err = r.bs.Delete(ctx, doc.Path) if err != nil { return err } diff --git a/backend/internal/data/repo/repo_documents_test.go b/backend/internal/data/repo/repo_documents_test.go index 4a3295c..36baad0 100644 --- a/backend/internal/data/repo/repo_documents_test.go +++ b/backend/internal/data/repo/repo_documents_test.go @@ -88,7 +88,7 @@ func TestDocumentRepository_CreateUpdateDelete(t *testing.T) { ensureRead := func() { // Read Document - bts, err := r.bs.Get(got.Path) + bts, err := r.bs.Get(tt.args.ctx, got.Path) require.NoError(t, err) buf, err := io.ReadAll(bts) require.NoError(t, err) @@ -107,7 +107,7 @@ func TestDocumentRepository_CreateUpdateDelete(t *testing.T) { err = r.Delete(tt.args.ctx, got.ID) require.NoError(t, err) - _, err = r.bs.Get(got.Path) + _, err = r.bs.Get(tt.args.ctx, got.Path) require.Error(t, err) }) }