package s3browser import ( "context" "fmt" "path/filepath" "strings" "github.com/graph-gophers/dataloader" "github.com/minio/minio-go/v7" log "github.com/sirupsen/logrus" ) 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") } log.Debug("Invalidate cache for id: ", id) path := getPathFromId(id) 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)) return nil } func getPathFromId(id string) string { dir := filepath.Dir(id) if dir == "." { return "/" } return nomalizeID(dir + "/") } func getFilenameFromID(id string) string { return filepath.Base(id) } func invalidateCacheForDir(ctx context.Context, path string) error { loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader) if !ok { return fmt.Errorf("Failed to get loader from context") } log.Debug("Invalidate cache for dir: ", path) 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 } func deleteMultiple(ctx context.Context, s3Client minio.Client, ids []minio.ObjectInfo) error { log.Debug("Delte multiple") objectsCh := make(chan minio.ObjectInfo, 1) go func() { defer close(objectsCh) for _, id := range ids { objectsCh <- id } }() for err := range s3Client.RemoveObjects(ctx, bucketName, objectsCh, minio.RemoveObjectsOptions{}) { log.Error("Failed to delete object ", err.ObjectName, " because: ", err.Err.Error()) // TODO: error handel } 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) }