moved stuff to helper
This commit is contained in:
parent
447a0647b2
commit
33237b6564
@ -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 {
|
||||
|
32
internal/helper.go
Normal file
32
internal/helper.go
Normal file
@ -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 + "/"
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user