package helper import ( "context" "fmt" "path/filepath" "strings" "time" "github.com/golang-jwt/jwt" "github.com/graph-gophers/dataloader" "github.com/minio/minio-go/v7" log "github.com/sirupsen/logrus" errors "git.kapelle.org/niklas/s3browser/internal/errors" types "git.kapelle.org/niklas/s3browser/internal/types" ) 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, "dev", 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) } func IsAuth(ctx context.Context) (bool, error) { token, ok := ctx.Value("jwt").(*jwt.Token) if !ok { return false, errors.ExtendError("UNAUTHORIZED", "Unauthorized") } if token.Valid { return true, nil } else { return false, errors.ExtendError("UNAUTHORIZED", "Unauthorized") } } func CreateJWT(claims *types.JWTClaims) *jwt.Token { claims.ExpiresAt = time.Now().Add(time.Hour * 24).Unix() return jwt.NewWithClaims(jwt.SigningMethodHS256, claims) } func CreateClaims(username string) *types.JWTClaims { return &types.JWTClaims{ StandardClaims: jwt.StandardClaims{ Subject: username, }, } }