the big refactor 2: return of the ID struct
This commit is contained in:
@@ -15,7 +15,7 @@ import (
|
||||
types "git.kapelle.org/niklas/s3browser/internal/types"
|
||||
)
|
||||
|
||||
func InvalidateCache(ctx context.Context, id string) error {
|
||||
func InvalidateCache(ctx context.Context, id types.ID) error {
|
||||
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
||||
if !ok {
|
||||
return fmt.Errorf("Failed to get loader from context")
|
||||
@@ -23,30 +23,20 @@ func InvalidateCache(ctx context.Context, id string) error {
|
||||
|
||||
log.Debug("Invalidate cache for id: ", id)
|
||||
|
||||
path := GetPathFromId(id)
|
||||
parent := id.Parent()
|
||||
|
||||
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["listObjectsRecursive"].Clear(ctx, dataloader.StringKey(path))
|
||||
loader["getFile"].Clear(ctx, id)
|
||||
loader["getFiles"].Clear(ctx, parent)
|
||||
loader["listObjects"].Clear(ctx, parent)
|
||||
loader["listObjectsRecursive"].Clear(ctx, parent)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetPathFromId(id string) string {
|
||||
dir := filepath.Dir(id)
|
||||
|
||||
if dir == "." {
|
||||
return "/"
|
||||
}
|
||||
|
||||
return NomalizeID(dir + "/")
|
||||
}
|
||||
|
||||
func GetFilenameFromID(id string) string {
|
||||
func GetFilenameFromKey(id string) string {
|
||||
return filepath.Base(id)
|
||||
}
|
||||
|
||||
func InvalidateCacheForDir(ctx context.Context, path string) error {
|
||||
func InvalidateCacheForDir(ctx context.Context, path types.ID) error {
|
||||
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
||||
if !ok {
|
||||
return fmt.Errorf("Failed to get loader from context")
|
||||
@@ -58,13 +48,13 @@ func InvalidateCacheForDir(ctx context.Context, path string) error {
|
||||
|
||||
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))
|
||||
loader["getFile"].Clear(ctx, path)
|
||||
loader["listObjects"].Clear(ctx, path)
|
||||
loader["listObjectsRecursive"].Clear(ctx, path)
|
||||
loader["getFiles"].Clear(ctx, path)
|
||||
loader["getDirs"].Clear(ctx, parent)
|
||||
loader["listObjects"].Clear(ctx, parent)
|
||||
loader["listObjectsRecursive"].Clear(ctx, parent)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -88,29 +78,24 @@ 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, "/")
|
||||
func GetParentDir(id types.ID) types.ID {
|
||||
dirs := strings.Split(id.Key, "/")
|
||||
|
||||
cut := 1
|
||||
if strings.HasSuffix(id, "/") {
|
||||
if strings.HasSuffix(id.Key, "/") {
|
||||
cut = 2
|
||||
}
|
||||
|
||||
parent := strings.Join(dirs[:len(dirs)-cut], "/") + "/"
|
||||
parentKey := strings.Join(dirs[:len(dirs)-cut], "/") + "/"
|
||||
|
||||
return NomalizeID(parent)
|
||||
parent := types.ID{
|
||||
Bucket: id.Bucket,
|
||||
Key: parentKey,
|
||||
}
|
||||
|
||||
parent.Normalize()
|
||||
|
||||
return parent
|
||||
}
|
||||
|
||||
func IsAuthenticated(ctx context.Context) bool {
|
||||
|
||||
Reference in New Issue
Block a user