2021-09-24 13:39:23 +00:00
|
|
|
package gql
|
2021-08-06 23:19:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-08-15 23:40:01 +00:00
|
|
|
"strings"
|
2021-08-06 23:19:36 +00:00
|
|
|
|
2021-08-20 19:38:22 +00:00
|
|
|
"github.com/graph-gophers/dataloader"
|
2021-08-06 23:19:36 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
2021-09-24 13:39:23 +00:00
|
|
|
|
|
|
|
helper "git.kapelle.org/niklas/s3browser/internal/helper"
|
|
|
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
2021-08-06 23:19:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func deleteMutation(ctx context.Context, id string) error {
|
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: it is posible to remove multiple objects with a single call.
|
|
|
|
// Is it better to batch this?
|
2021-09-24 13:39:23 +00:00
|
|
|
err := s3Client.RemoveObject(ctx, "dev", id, minio.RemoveObjectOptions{})
|
2021-08-06 23:42:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate cache
|
2021-09-24 13:39:23 +00:00
|
|
|
return helper.InvalidateCache(ctx, helper.NomalizeID(id))
|
2021-08-06 23:19:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func copyMutation(ctx context.Context, src, dest string) (*types.File, error) {
|
2021-08-06 23:19:36 +00:00
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
2021-08-15 23:40:01 +00:00
|
|
|
// Check if dest is a file or a dir
|
|
|
|
if strings.HasSuffix(dest, "/") {
|
|
|
|
// create new dest id
|
|
|
|
// TODO: What if a file with this id already exists?
|
2021-09-24 13:39:23 +00:00
|
|
|
dest += helper.GetFilenameFromID(src)
|
2021-08-15 23:40:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 23:19:36 +00:00
|
|
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
2021-09-24 13:39:23 +00:00
|
|
|
Bucket: "dev",
|
2021-08-06 23:19:36 +00:00
|
|
|
Object: dest,
|
|
|
|
}, minio.CopySrcOptions{
|
2021-09-24 13:39:23 +00:00
|
|
|
Bucket: "dev",
|
2021-08-06 23:19:36 +00:00
|
|
|
Object: src,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:42:47 +00:00
|
|
|
// Invalidate cache
|
2021-08-14 12:13:49 +00:00
|
|
|
// TODO: check error
|
2021-09-24 13:39:23 +00:00
|
|
|
helper.InvalidateCache(ctx, helper.NomalizeID(info.Key))
|
2021-08-06 23:42:47 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return &types.File{
|
2021-08-06 23:19:36 +00:00
|
|
|
ID: info.Key,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func moveMutation(ctx context.Context, src, dest string) (*types.File, error) {
|
2021-08-06 23:19:36 +00:00
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:04:44 +00:00
|
|
|
// Check if dest is a file or a dir
|
|
|
|
if strings.HasSuffix(dest, "/") {
|
|
|
|
// create new dest id
|
|
|
|
// TODO: What if a file with this id already exists?
|
2021-09-24 13:39:23 +00:00
|
|
|
dest += helper.GetFilenameFromID(src)
|
2021-08-16 00:04:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 23:19:36 +00:00
|
|
|
// There is no (spoon) move. Only copy and delete
|
|
|
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
2021-09-24 13:39:23 +00:00
|
|
|
Bucket: "dev",
|
2021-08-06 23:19:36 +00:00
|
|
|
Object: dest,
|
|
|
|
}, minio.CopySrcOptions{
|
2021-09-24 13:39:23 +00:00
|
|
|
Bucket: "dev",
|
2021-08-06 23:19:36 +00:00
|
|
|
Object: src,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = deleteMutation(ctx, src)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
helper.InvalidateCache(ctx, helper.NomalizeID(info.Key))
|
2021-08-16 12:58:03 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return &types.File{
|
2021-08-06 23:19:36 +00:00
|
|
|
ID: info.Key,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
2021-08-16 17:57:48 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
func createDirectory(ctx context.Context, path string) (*types.Directory, error) {
|
2021-08-16 17:57:48 +00:00
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, "/") {
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
info, err := s3Client.PutObject(ctx, "dev", path, strings.NewReader(""), 0, minio.PutObjectOptions{
|
2021-08-16 17:57:48 +00:00
|
|
|
ContentType: "application/x-directory",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-16 20:16:42 +00:00
|
|
|
// Invalidate cache
|
|
|
|
// TODO: check error
|
2021-09-24 13:39:23 +00:00
|
|
|
helper.InvalidateCacheForDir(ctx, helper.NomalizeID(info.Key))
|
2021-08-16 20:16:42 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return &types.Directory{
|
2021-08-16 17:57:48 +00:00
|
|
|
ID: info.Key,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
2021-08-20 19:38:22 +00:00
|
|
|
|
|
|
|
func deleteDirectory(ctx context.Context, path string) error {
|
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get dataloader from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, "/") {
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all files inside the directory
|
2021-09-24 13:39:23 +00:00
|
|
|
thunk := loader["listObjectsRecursive"].Load(ctx, dataloader.StringKey(helper.NomalizeID(path)))
|
2021-08-20 19:38:22 +00:00
|
|
|
|
|
|
|
result, err := thunk()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
files, ok := result.([]minio.ObjectInfo)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get parse result from listObjects")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all child files
|
2021-09-24 13:39:23 +00:00
|
|
|
err = helper.DeleteMultiple(ctx, *s3Client, files)
|
2021-08-20 19:38:22 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the dir had no children it exists as an object (object with "/" at the end).
|
|
|
|
// If it exists as an object and had children it will get delete once the last child has been deleted
|
|
|
|
// If it had no children we have to delete it manualy
|
|
|
|
// This is at least the behavior when working with minio as s3 backend
|
|
|
|
// TODO: check if this is normal behavior when working with s3
|
|
|
|
if len(files) == 0 {
|
2021-09-24 13:39:23 +00:00
|
|
|
err := s3Client.RemoveObject(ctx, "dev", path, minio.RemoveObjectOptions{})
|
2021-08-20 19:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Invalidate cache
|
2021-09-24 13:39:23 +00:00
|
|
|
helper.InvalidateCacheForDir(ctx, helper.NomalizeID(path))
|
2021-08-20 19:38:22 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-14 13:16:37 +00:00
|
|
|
|
|
|
|
//login Checks for valid username password combination. Returns singed jwt string
|
2021-09-14 14:22:32 +00:00
|
|
|
func login(ctx context.Context, username, password string) (LoginResult, error) {
|
|
|
|
|
|
|
|
// TODO: replace with propper user management
|
|
|
|
if username != "admin" && password != "hunter2" {
|
|
|
|
return LoginResult{
|
|
|
|
Successful: false,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
token := helper.CreateJWT(helper.CreateClaims(username))
|
2021-09-14 13:16:37 +00:00
|
|
|
|
|
|
|
tokenString, err := token.SignedString([]byte("TODO"))
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-14 14:22:32 +00:00
|
|
|
return LoginResult{
|
|
|
|
Successful: false,
|
|
|
|
}, err
|
2021-09-14 13:16:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 14:22:32 +00:00
|
|
|
return LoginResult{
|
|
|
|
Token: tokenString,
|
|
|
|
Successful: true,
|
|
|
|
}, nil
|
2021-09-14 13:16:37 +00:00
|
|
|
}
|