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
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
2021-09-24 13:39:23 +00:00
|
|
|
|
2021-10-20 14:11:18 +00:00
|
|
|
"git.kapelle.org/niklas/s3browser/internal/db"
|
2021-09-24 13:39:23 +00:00
|
|
|
helper "git.kapelle.org/niklas/s3browser/internal/helper"
|
2021-10-14 17:00:11 +00:00
|
|
|
"git.kapelle.org/niklas/s3browser/internal/loader"
|
2021-09-24 13:39:23 +00:00
|
|
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
2021-08-06 23:19:36 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 23:59:32 +00:00
|
|
|
func deleteMutation(ctx context.Context, id types.ID) error {
|
2021-08-06 23:19:36 +00:00
|
|
|
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-26 23:59:32 +00:00
|
|
|
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
|
2021-08-06 23:42:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
ctx.Value("loader").(*loader.Loader).InvalidateCacheForFile(ctx, id)
|
|
|
|
|
|
|
|
return nil
|
2021-08-06 23:19:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 23:59:32 +00:00
|
|
|
func copyMutation(ctx context.Context, src, dest types.ID) (*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
|
2021-09-26 23:59:32 +00:00
|
|
|
if dest.IsDirectory() {
|
2021-08-15 23:40:01 +00:00
|
|
|
// create new dest id
|
|
|
|
// TODO: What if a file with this id already exists?
|
2021-09-26 23:59:32 +00:00
|
|
|
dest.Key += helper.GetFilenameFromKey(src.Key)
|
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-26 23:59:32 +00:00
|
|
|
Bucket: dest.Bucket,
|
|
|
|
Object: dest.Key,
|
2021-08-06 23:19:36 +00:00
|
|
|
}, minio.CopySrcOptions{
|
2021-09-26 23:59:32 +00:00
|
|
|
Bucket: src.Bucket,
|
|
|
|
Object: src.Key,
|
2021-08-06 23:19:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
newID := types.ID{
|
2021-09-26 23:59:32 +00:00
|
|
|
Bucket: info.Bucket,
|
|
|
|
Key: info.Key,
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
newID.Normalize()
|
2021-09-26 23:59:32 +00:00
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
ctx.Value("loader").(*loader.Loader).InvalidateCacheForFile(ctx, newID)
|
2021-08-06 23:42:47 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return &types.File{
|
2021-10-14 17:00:11 +00:00
|
|
|
ID: newID,
|
2021-08-06 23:19:36 +00:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-01 23:33:55 +00:00
|
|
|
func moveDirMutation(ctx context.Context, src, dest types.ID) ([]*types.File, error) {
|
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dest.IsDirectory() {
|
|
|
|
return nil, fmt.Errorf("Dest must be a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
loader, ok := ctx.Value("loader").(*loader.Loader)
|
|
|
|
|
|
|
|
// "move" all file inside dir
|
|
|
|
files, err := loader.GetFilesRecursive(ctx, src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*types.File
|
|
|
|
parent := src.Parent()
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
newID := types.ID{
|
|
|
|
Bucket: dest.Bucket,
|
|
|
|
Key: strings.Replace(file.ID.Key, parent.Key, dest.Key, 1),
|
|
|
|
}
|
|
|
|
newID.Normalize()
|
|
|
|
|
|
|
|
_, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
|
|
|
Bucket: dest.Bucket,
|
|
|
|
Object: newID.Key,
|
|
|
|
}, minio.CopySrcOptions{
|
|
|
|
Bucket: file.ID.Bucket,
|
|
|
|
Object: file.ID.Key,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle error
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteMutation(ctx, file.ID)
|
|
|
|
|
|
|
|
result = append(result, &types.File{
|
|
|
|
ID: newID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveFileMutation(ctx context.Context, src, dest types.ID) (*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
|
2021-09-26 23:59:32 +00:00
|
|
|
if dest.IsDirectory() {
|
2021-08-16 00:04:44 +00:00
|
|
|
// create new dest id
|
|
|
|
// TODO: What if a file with this id already exists?
|
2021-09-26 23:59:32 +00:00
|
|
|
dest.Key += helper.GetFilenameFromKey(src.Key)
|
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-26 23:59:32 +00:00
|
|
|
Bucket: dest.Bucket,
|
|
|
|
Object: dest.Key,
|
2021-08-06 23:19:36 +00:00
|
|
|
}, minio.CopySrcOptions{
|
2021-09-26 23:59:32 +00:00
|
|
|
Bucket: src.Bucket,
|
|
|
|
Object: src.Key,
|
2021-08-06 23:19:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = deleteMutation(ctx, src)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-26 23:59:32 +00:00
|
|
|
newId := types.ID{
|
|
|
|
Bucket: info.Bucket,
|
|
|
|
Key: info.Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
newId.Normalize()
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
ctx.Value("loader").(*loader.Loader).InvalidateCacheForFile(ctx, newId)
|
2021-08-16 12:58:03 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return &types.File{
|
2021-09-26 23:59:32 +00:00
|
|
|
ID: newId,
|
2021-08-06 23:19:36 +00:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
2021-08-16 17:57:48 +00:00
|
|
|
|
2021-09-26 23:59:32 +00:00
|
|
|
func createDirectory(ctx context.Context, id types.ID) (*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")
|
|
|
|
}
|
|
|
|
|
2021-09-26 23:59:32 +00:00
|
|
|
info, err := s3Client.PutObject(ctx, id.Bucket, id.Key, strings.NewReader(""), 0, minio.PutObjectOptions{
|
2021-08-16 17:57:48 +00:00
|
|
|
ContentType: "application/x-directory",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
newID := types.ID{
|
2021-09-26 23:59:32 +00:00
|
|
|
Bucket: info.Bucket,
|
|
|
|
Key: info.Key,
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
newID.Normalize()
|
2021-09-26 23:59:32 +00:00
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
ctx.Value("loader").(*loader.Loader).InvalidateCacheForDir(ctx, newID)
|
2021-08-16 20:16:42 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
return &types.Directory{
|
2021-10-14 17:00:11 +00:00
|
|
|
ID: newID,
|
2021-08-16 17:57:48 +00:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
2021-08-20 19:38:22 +00:00
|
|
|
|
2021-09-26 23:59:32 +00:00
|
|
|
func deleteDirectory(ctx context.Context, id types.ID) error {
|
2021-08-20 19:38:22 +00:00
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
loader, ok := ctx.Value("loader").(*loader.Loader)
|
2021-08-20 19:38:22 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get dataloader from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all files inside the directory
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
files, err := loader.GetFilesRecursive(ctx, id)
|
2021-08-20 19:38:22 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all child files
|
2021-10-14 17:00:11 +00:00
|
|
|
var keysToDel []string
|
|
|
|
for _, file := range files {
|
|
|
|
keysToDel = append(keysToDel, file.ID.Key)
|
|
|
|
}
|
|
|
|
err = helper.DeleteMultiple(ctx, *s3Client, id.Bucket, keysToDel)
|
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-26 23:59:32 +00:00
|
|
|
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
|
2021-08-20 19:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:00:11 +00:00
|
|
|
loader.InvalidateCacheForDir(ctx, id)
|
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-24 13:54:03 +00:00
|
|
|
func login(ctx context.Context, username, password string) (types.LoginResult, error) {
|
2021-09-14 14:22:32 +00:00
|
|
|
|
2021-10-20 14:11:18 +00:00
|
|
|
dbStore := ctx.Value("dbStore").(*db.DB)
|
|
|
|
|
|
|
|
succes, err := dbStore.CheckLogin(ctx, username, password)
|
|
|
|
|
|
|
|
if !succes {
|
2021-09-24 13:54:03 +00:00
|
|
|
return types.LoginResult{
|
2021-09-14 14:22:32 +00:00
|
|
|
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-24 13:54:03 +00:00
|
|
|
return types.LoginResult{
|
2021-09-14 14:22:32 +00:00
|
|
|
Successful: false,
|
|
|
|
}, err
|
2021-09-14 13:16:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:54:03 +00:00
|
|
|
return types.LoginResult{
|
2021-09-14 14:22:32 +00:00
|
|
|
Token: tokenString,
|
|
|
|
Successful: true,
|
|
|
|
}, nil
|
2021-09-14 13:16:37 +00:00
|
|
|
}
|