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")
|
return nil, fmt.Errorf("Failed to parse Source for parent resolve")
|
||||||
}
|
}
|
||||||
|
|
||||||
basename := filepath.Dir(source.ID)
|
basename := getPathFromId(source.ID)
|
||||||
|
|
||||||
if basename == "." {
|
|
||||||
basename = "/"
|
|
||||||
}
|
|
||||||
|
|
||||||
return Directory{
|
return Directory{
|
||||||
ID: basename,
|
ID: basename,
|
||||||
@ -213,6 +209,10 @@ func loadFile(p graphql.ResolveParams) (*File, error) {
|
|||||||
thunk := loader["getFile"].Load(p.Context, dataloader.StringKey(source.ID))
|
thunk := loader["getFile"].Load(p.Context, dataloader.StringKey(source.ID))
|
||||||
result, err := thunk()
|
result, err := thunk()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
file, ok := result.(*File)
|
file, ok := result.(*File)
|
||||||
|
|
||||||
if !ok {
|
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"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/graph-gophers/dataloader"
|
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
"github.com/graphql-go/graphql/gqlerrors"
|
"github.com/graphql-go/graphql/gqlerrors"
|
||||||
"github.com/graphql-go/handler"
|
"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) {
|
func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
||||||
s3Client := ctx.Value("s3Client").(*minio.Client)
|
s3Client := ctx.Value("s3Client").(*minio.Client)
|
||||||
loader := ctx.Value("loader").(map[string]*dataloader.Loader)
|
|
||||||
|
|
||||||
id := r.URL.Query().Get("id")
|
id := r.URL.Query().Get("id")
|
||||||
|
|
||||||
@ -115,9 +113,7 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invalidate cache
|
// Invalidate cache
|
||||||
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
|
invalidateCache(ctx, info.Key)
|
||||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
|
|
||||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
|
|
||||||
|
|
||||||
rw.WriteHeader(http.StatusCreated)
|
rw.WriteHeader(http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,7 @@ package s3browser
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/graph-gophers/dataloader"
|
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,16 +23,7 @@ func deleteMutation(ctx context.Context, id string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invalidate cache
|
// Invalidate cache
|
||||||
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
return invalidateCache(ctx, id)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyMutation(ctx context.Context, src, dest string) (*File, error) {
|
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
|
// Invalidate cache
|
||||||
|
// TODO: check error
|
||||||
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
invalidateCache(ctx, info.Key)
|
||||||
|
|
||||||
// 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)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return &File{
|
return &File{
|
||||||
ID: info.Key,
|
ID: info.Key,
|
||||||
|
Loading…
Reference in New Issue
Block a user