2021-09-24 13:39:23 +00:00
|
|
|
package helper
|
2021-08-14 12:13:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2021-09-03 13:03:32 +00:00
|
|
|
"strings"
|
2021-09-23 23:27:19 +00:00
|
|
|
"time"
|
2021-08-14 12:13:49 +00:00
|
|
|
|
2021-09-14 14:51:01 +00:00
|
|
|
"github.com/golang-jwt/jwt"
|
2021-08-14 12:13:49 +00:00
|
|
|
"github.com/graph-gophers/dataloader"
|
2021-08-20 19:38:22 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
2021-08-16 20:40:10 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-09-24 13:39:23 +00:00
|
|
|
|
|
|
|
errors "git.kapelle.org/niklas/s3browser/internal/errors"
|
|
|
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
2021-08-14 12:13:49 +00:00
|
|
|
)
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func InvalidateCache(ctx context.Context, id string) error {
|
2021-08-14 12:13:49 +00:00
|
|
|
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get loader from context")
|
|
|
|
}
|
|
|
|
|
2021-08-16 20:40:10 +00:00
|
|
|
log.Debug("Invalidate cache for id: ", id)
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
path := GetPathFromId(id)
|
2021-08-16 20:40:10 +00:00
|
|
|
|
2021-09-03 13:03:32 +00:00
|
|
|
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))
|
2021-08-14 12:13:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func GetPathFromId(id string) string {
|
2021-08-14 12:13:49 +00:00
|
|
|
dir := filepath.Dir(id)
|
|
|
|
|
|
|
|
if dir == "." {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return NomalizeID(dir + "/")
|
2021-08-14 12:13:49 +00:00
|
|
|
}
|
2021-08-15 23:40:01 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func GetFilenameFromID(id string) string {
|
2021-08-15 23:40:01 +00:00
|
|
|
return filepath.Base(id)
|
|
|
|
}
|
2021-08-16 20:16:42 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func InvalidateCacheForDir(ctx context.Context, path string) error {
|
2021-08-16 20:16:42 +00:00
|
|
|
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)
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
parent := GetParentDir(path)
|
2021-09-03 13:03:32 +00:00
|
|
|
|
|
|
|
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))
|
2021-08-16 20:16:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-20 19:38:22 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func DeleteMultiple(ctx context.Context, s3Client minio.Client, ids []minio.ObjectInfo) error {
|
2021-08-20 19:38:22 +00:00
|
|
|
log.Debug("Delte multiple")
|
|
|
|
objectsCh := make(chan minio.ObjectInfo, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(objectsCh)
|
|
|
|
for _, id := range ids {
|
|
|
|
objectsCh <- id
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
for err := range s3Client.RemoveObjects(ctx, "dev", objectsCh, minio.RemoveObjectsOptions{}) {
|
2021-08-20 19:38:22 +00:00
|
|
|
log.Error("Failed to delete object ", err.ObjectName, " because: ", err.Err.Error())
|
|
|
|
// TODO: error handel
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-03 13:03:32 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
// NomalizeID makes sure there is a leading "/" in the id
|
|
|
|
func NomalizeID(id string) string {
|
2021-09-03 13:03:32 +00:00
|
|
|
if !strings.HasPrefix(id, "/") {
|
|
|
|
if id == "." {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
id = "/" + id
|
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func GetParentDir(id string) string {
|
2021-09-03 13:03:32 +00:00
|
|
|
dirs := strings.Split(id, "/")
|
|
|
|
|
|
|
|
cut := 1
|
|
|
|
if strings.HasSuffix(id, "/") {
|
|
|
|
cut = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := strings.Join(dirs[:len(dirs)-cut], "/") + "/"
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return NomalizeID(parent)
|
2021-09-03 13:03:32 +00:00
|
|
|
}
|
2021-09-14 14:51:01 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func IsAuth(ctx context.Context) (bool, error) {
|
2021-09-14 14:51:01 +00:00
|
|
|
token, ok := ctx.Value("jwt").(*jwt.Token)
|
|
|
|
|
|
|
|
if !ok {
|
2021-09-24 13:39:23 +00:00
|
|
|
return false, errors.ExtendError("UNAUTHORIZED", "Unauthorized")
|
2021-09-14 14:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if token.Valid {
|
|
|
|
return true, nil
|
|
|
|
} else {
|
2021-09-24 13:39:23 +00:00
|
|
|
return false, errors.ExtendError("UNAUTHORIZED", "Unauthorized")
|
2021-09-14 14:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 23:27:19 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func CreateJWT(claims *types.JWTClaims) *jwt.Token {
|
2021-09-23 23:27:19 +00:00
|
|
|
|
|
|
|
claims.ExpiresAt = time.Now().Add(time.Hour * 24).Unix()
|
|
|
|
|
|
|
|
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func CreateClaims(username string) *types.JWTClaims {
|
|
|
|
return &types.JWTClaims{
|
2021-09-23 23:27:19 +00:00
|
|
|
StandardClaims: jwt.StandardClaims{
|
|
|
|
Subject: username,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|