Compare commits
2 Commits
8296b21883
...
9616450cff
| Author | SHA1 | Date | |
|---|---|---|---|
| 9616450cff | |||
| 63b93f5895 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/internal/static
|
||||
9
internal/debug.go
Normal file
9
internal/debug.go
Normal file
@@ -0,0 +1,9 @@
|
||||
//go:build !prod
|
||||
// +build !prod
|
||||
|
||||
package s3browser
|
||||
|
||||
// Since we dont have the static directory when developing we replace the function with an empty one
|
||||
func initStatic() {
|
||||
// NOOP
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package s3browser
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
@@ -160,7 +159,7 @@ func graphqlTypes() {
|
||||
|
||||
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
|
||||
|
||||
thunk := loader["getFiles"].Load(p.Context, dataloader.StringKey(source.ID))
|
||||
thunk := loader["getFiles"].Load(p.Context, dataloader.StringKey(nomalizeID(source.ID)))
|
||||
return thunk()
|
||||
},
|
||||
})
|
||||
@@ -174,7 +173,7 @@ func graphqlTypes() {
|
||||
}
|
||||
|
||||
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
|
||||
thunk := loader["getDirs"].Load(p.Context, dataloader.StringKey(source.ID))
|
||||
thunk := loader["getDirs"].Load(p.Context, dataloader.StringKey(nomalizeID(source.ID)))
|
||||
|
||||
return thunk()
|
||||
},
|
||||
@@ -188,10 +187,8 @@ func graphqlTypes() {
|
||||
return nil, fmt.Errorf("Failed to parse Source for directories resolve")
|
||||
}
|
||||
|
||||
dirs := strings.Split(source.ID, "/")
|
||||
|
||||
return Directory{
|
||||
ID: strings.Join(dirs[:len(dirs)-2], "/") + "/",
|
||||
ID: getParentDir(source.ID),
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
@@ -206,7 +203,7 @@ func loadFile(p graphql.ResolveParams) (*File, error) {
|
||||
|
||||
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
|
||||
|
||||
thunk := loader["getFile"].Load(p.Context, dataloader.StringKey(source.ID))
|
||||
thunk := loader["getFile"].Load(p.Context, dataloader.StringKey(nomalizeID(source.ID)))
|
||||
result, err := thunk()
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
"github.com/minio/minio-go/v7"
|
||||
@@ -18,13 +19,12 @@ func invalidateCache(ctx context.Context, id string) error {
|
||||
|
||||
log.Debug("Invalidate cache for id: ", id)
|
||||
|
||||
path := getPathFromId(id)
|
||||
|
||||
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(getPathFromId(id)))
|
||||
|
||||
// FIXME: this is the only way i can invalidate the cache without it breaking stuff
|
||||
// loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
|
||||
loader["listObjects"].ClearAll()
|
||||
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["listObjectsRecursive"].Clear(ctx, dataloader.StringKey(path))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func getPathFromId(id string) string {
|
||||
return "/"
|
||||
}
|
||||
|
||||
return dir + "/"
|
||||
return nomalizeID(dir + "/")
|
||||
}
|
||||
|
||||
func getFilenameFromID(id string) string {
|
||||
@@ -50,11 +50,17 @@ func invalidateCacheForDir(ctx context.Context, path string) error {
|
||||
|
||||
log.Debug("Invalidate cache for dir: ", path)
|
||||
|
||||
// FIXME: only clear required ids
|
||||
loader["getFile"].ClearAll()
|
||||
loader["listObjects"].ClearAll()
|
||||
loader["getFiles"].ClearAll()
|
||||
loader["getDirs"].ClearAll()
|
||||
parent := getParentDir(path)
|
||||
|
||||
log.Debug("Cache clear dir: ", path, " parent: ", parent)
|
||||
|
||||
loader["getFile"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["listObjectsRecursive"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["getDirs"].Clear(ctx, dataloader.StringKey(parent))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(parent))
|
||||
loader["listObjectsRecursive"].Clear(ctx, dataloader.StringKey(parent))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -77,3 +83,28 @@ func deleteMultiple(ctx context.Context, s3Client minio.Client, ids []minio.Obje
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// nomalizeID makes sure there is a leading "/" in the id
|
||||
func nomalizeID(id string) string {
|
||||
if !strings.HasPrefix(id, "/") {
|
||||
if id == "." {
|
||||
return "/"
|
||||
}
|
||||
id = "/" + id
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
func getParentDir(id string) string {
|
||||
dirs := strings.Split(id, "/")
|
||||
|
||||
cut := 1
|
||||
if strings.HasSuffix(id, "/") {
|
||||
cut = 2
|
||||
}
|
||||
|
||||
parent := strings.Join(dirs[:len(dirs)-cut], "/") + "/"
|
||||
|
||||
return nomalizeID(parent)
|
||||
}
|
||||
|
||||
@@ -50,6 +50,9 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error {
|
||||
}
|
||||
})
|
||||
|
||||
// Init the embedded static files
|
||||
initStatic()
|
||||
|
||||
return http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ func deleteMutation(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
// Invalidate cache
|
||||
return invalidateCache(ctx, id)
|
||||
return invalidateCache(ctx, nomalizeID(id))
|
||||
}
|
||||
|
||||
func copyMutation(ctx context.Context, src, dest string) (*File, error) {
|
||||
@@ -56,7 +56,7 @@ func copyMutation(ctx context.Context, src, dest string) (*File, error) {
|
||||
|
||||
// Invalidate cache
|
||||
// TODO: check error
|
||||
invalidateCache(ctx, info.Key)
|
||||
invalidateCache(ctx, nomalizeID(info.Key))
|
||||
|
||||
return &File{
|
||||
ID: info.Key,
|
||||
@@ -97,7 +97,7 @@ func moveMutation(ctx context.Context, src, dest string) (*File, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
invalidateCache(ctx, info.Key)
|
||||
invalidateCache(ctx, nomalizeID(info.Key))
|
||||
|
||||
return &File{
|
||||
ID: info.Key,
|
||||
@@ -126,7 +126,7 @@ func createDirectory(ctx context.Context, path string) (*Directory, error) {
|
||||
|
||||
// Invalidate cache
|
||||
// TODO: check error
|
||||
invalidateCacheForDir(ctx, info.Key)
|
||||
invalidateCacheForDir(ctx, nomalizeID(info.Key))
|
||||
|
||||
return &Directory{
|
||||
ID: info.Key,
|
||||
@@ -152,7 +152,7 @@ func deleteDirectory(ctx context.Context, path string) error {
|
||||
}
|
||||
|
||||
// Get all files inside the directory
|
||||
thunk := loader["listObjectsRecursive"].Load(ctx, dataloader.StringKey(path))
|
||||
thunk := loader["listObjectsRecursive"].Load(ctx, dataloader.StringKey(nomalizeID(path)))
|
||||
|
||||
result, err := thunk()
|
||||
|
||||
@@ -185,7 +185,7 @@ func deleteDirectory(ctx context.Context, path string) error {
|
||||
}
|
||||
|
||||
//Invalidate cache
|
||||
invalidateCacheForDir(ctx, path)
|
||||
invalidateCacheForDir(ctx, nomalizeID(path))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
32
internal/staticFiles.go
Normal file
32
internal/staticFiles.go
Normal file
@@ -0,0 +1,32 @@
|
||||
//go:build prod
|
||||
// +build prod
|
||||
|
||||
package s3browser
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// content holds our static web server content.
|
||||
//go:embed static
|
||||
var staticFiles embed.FS
|
||||
|
||||
type spaFileSystem struct {
|
||||
root http.FileSystem
|
||||
}
|
||||
|
||||
func (spa *spaFileSystem) Open(name string) (http.File, error) {
|
||||
f, err := spa.root.Open(name)
|
||||
if os.IsNotExist(err) {
|
||||
return spa.root.Open("index.html")
|
||||
}
|
||||
return f, err
|
||||
}
|
||||
|
||||
func initStatic() {
|
||||
staticFS, _ := fs.Sub(staticFiles, "static")
|
||||
http.Handle("/", http.FileServer(&spaFileSystem{http.FS(staticFS)}))
|
||||
}
|
||||
Reference in New Issue
Block a user