80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package s3browser
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"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)
|
|
|
|
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()
|
|
|
|
return nil
|
|
}
|
|
|
|
func getPathFromId(id string) string {
|
|
dir := filepath.Dir(id)
|
|
|
|
if dir == "." {
|
|
return "/"
|
|
}
|
|
|
|
return 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)
|
|
|
|
// FIXME: only clear required ids
|
|
loader["getFile"].ClearAll()
|
|
loader["listObjects"].ClearAll()
|
|
loader["getFiles"].ClearAll()
|
|
loader["getDirs"].ClearAll()
|
|
|
|
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
|
|
}
|