From 33237b65640fac834f858f77b1e429996cee085e Mon Sep 17 00:00:00 2001 From: Niklas Date: Sat, 14 Aug 2021 14:13:49 +0200 Subject: [PATCH] moved stuff to helper --- internal/graphqlTypes.go | 10 +++++----- internal/helper.go | 32 ++++++++++++++++++++++++++++++++ internal/httpServer.go | 6 +----- internal/mutations.go | 25 +++---------------------- 4 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 internal/helper.go diff --git a/internal/graphqlTypes.go b/internal/graphqlTypes.go index 9aef8c3..f8c0792 100644 --- a/internal/graphqlTypes.go +++ b/internal/graphqlTypes.go @@ -140,11 +140,7 @@ func graphqlTypes() { return nil, fmt.Errorf("Failed to parse Source for parent resolve") } - basename := filepath.Dir(source.ID) - - if basename == "." { - basename = "/" - } + basename := getPathFromId(source.ID) return Directory{ ID: basename, @@ -213,6 +209,10 @@ func loadFile(p graphql.ResolveParams) (*File, error) { thunk := loader["getFile"].Load(p.Context, dataloader.StringKey(source.ID)) result, err := thunk() + if err != nil { + return nil, err + } + file, ok := result.(*File) if !ok { diff --git a/internal/helper.go b/internal/helper.go new file mode 100644 index 0000000..e27200f --- /dev/null +++ b/internal/helper.go @@ -0,0 +1,32 @@ +package s3browser + +import ( + "context" + "fmt" + "path/filepath" + + "github.com/graph-gophers/dataloader" +) + +func invalidateCache(ctx context.Context, id string) error { + loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader) + if !ok { + return fmt.Errorf("Failed to get loader from context") + } + + loader["getFile"].Clear(ctx, dataloader.StringKey(id)) + loader["listObjects"].Clear(ctx, dataloader.StringKey(id)) + loader["getFiles"].Clear(ctx, dataloader.StringKey(getPathFromId(id))) + + return nil +} + +func getPathFromId(id string) string { + dir := filepath.Dir(id) + + if dir == "." { + return "/" + } + + return dir + "/" +} diff --git a/internal/httpServer.go b/internal/httpServer.go index 6981b59..bc688ce 100644 --- a/internal/httpServer.go +++ b/internal/httpServer.go @@ -8,7 +8,6 @@ import ( "net/http" "path/filepath" - "github.com/graph-gophers/dataloader" "github.com/graphql-go/graphql" "github.com/graphql-go/graphql/gqlerrors" "github.com/graphql-go/handler" @@ -95,7 +94,6 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) { func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) { s3Client := ctx.Value("s3Client").(*minio.Client) - loader := ctx.Value("loader").(map[string]*dataloader.Loader) id := r.URL.Query().Get("id") @@ -115,9 +113,7 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) } // Invalidate cache - loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key)) - loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key)) - loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key))) + invalidateCache(ctx, info.Key) rw.WriteHeader(http.StatusCreated) } diff --git a/internal/mutations.go b/internal/mutations.go index 1216fae..aaeb81f 100644 --- a/internal/mutations.go +++ b/internal/mutations.go @@ -3,9 +3,7 @@ package s3browser import ( "context" "fmt" - "path/filepath" - "github.com/graph-gophers/dataloader" "github.com/minio/minio-go/v7" ) @@ -25,16 +23,7 @@ func deleteMutation(ctx context.Context, id string) error { } // Invalidate cache - loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader) - if !ok { - return fmt.Errorf("Failed to get loader from context") - } - - loader["getFile"].Clear(ctx, dataloader.StringKey(id)) - loader["listObjects"].Clear(ctx, dataloader.StringKey(id)) - loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(id))) - - return nil + return invalidateCache(ctx, id) } func copyMutation(ctx context.Context, src, dest string) (*File, error) { @@ -57,16 +46,8 @@ func copyMutation(ctx context.Context, src, dest string) (*File, error) { } // Invalidate cache - - loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader) - - // TODO: Do we want to error when the operation - // has succeeded but the cache invalidation has failed ? - if ok { - loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key)) - loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key)) - loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key))) - } + // TODO: check error + invalidateCache(ctx, info.Key) return &File{ ID: info.Key,