s3browser-backend/internal/helper.go

142 lines
3.2 KiB
Go
Raw Normal View History

2021-08-14 12:13:49 +00:00
package s3browser
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-08-14 12:13:49 +00:00
)
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")
}
2021-08-16 20:40:10 +00:00
log.Debug("Invalidate cache for id: ", id)
2021-09-03 13:03:32 +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
}
func getPathFromId(id string) string {
dir := filepath.Dir(id)
if dir == "." {
return "/"
}
2021-09-03 13:03:32 +00:00
return nomalizeID(dir + "/")
2021-08-14 12:13:49 +00:00
}
2021-08-15 23:40:01 +00:00
func getFilenameFromID(id string) string {
return filepath.Base(id)
}
2021-08-16 20:16:42 +00:00
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)
2021-09-03 13:03:32 +00:00
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))
2021-08-16 20:16:42 +00:00
return nil
}
2021-08-20 19:38:22 +00:00
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
}
2021-09-03 13:03:32 +00:00
// 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)
}
2021-09-14 14:51:01 +00:00
func isAuth(ctx context.Context) (bool, error) {
token, ok := ctx.Value("jwt").(*jwt.Token)
if !ok {
2021-09-18 21:50:00 +00:00
return false, extendError("UNAUTHORIZED", "Unauthorized")
2021-09-14 14:51:01 +00:00
}
if token.Valid {
return true, nil
} else {
2021-09-18 21:50:00 +00:00
return false, extendError("UNAUTHORIZED", "Unauthorized")
2021-09-14 14:51:01 +00:00
}
}
2021-09-23 23:27:19 +00:00
func createJWT(claims *JWTClaims) *jwt.Token {
claims.ExpiresAt = time.Now().Add(time.Hour * 24).Unix()
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
}
func createClaims(username string) *JWTClaims {
return &JWTClaims{
StandardClaims: jwt.StandardClaims{
Subject: username,
},
}
}