From 393a39e19d32d9f10081a6d48f40fd4e71632204 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 ++++--- backend/internal/data/repo/repo_documents.go | 6 +++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/backend/internal/core/blobstore/local.go b/backend/internal/core/blobstore/local.go index 15635290..750986df 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 e1bac103..feeb725a 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/data/repo/repo_documents.go b/backend/internal/data/repo/repo_documents.go index cacf30f5..3a06c6e8 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 }