2021-08-06 23:19:36 +00:00
|
|
|
package s3browser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-08-06 23:42:47 +00:00
|
|
|
"path/filepath"
|
2021-08-06 23:19:36 +00:00
|
|
|
|
2021-08-06 23:42:47 +00:00
|
|
|
"github.com/graph-gophers/dataloader"
|
2021-08-06 23:19:36 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-08-06 23:42:47 +00:00
|
|
|
err := s3Client.RemoveObject(ctx, bucketName, id, minio.RemoveObjectOptions{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate cache
|
|
|
|
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to get loader from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
|
|
|
|
loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
|
|
|
|
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(id)))
|
|
|
|
|
|
|
|
return nil
|
2021-08-06 23:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyMutation(ctx context.Context, src, dest string) (*File, error) {
|
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
|
|
|
Bucket: bucketName,
|
|
|
|
Object: dest,
|
|
|
|
}, minio.CopySrcOptions{
|
|
|
|
Bucket: bucketName,
|
|
|
|
Object: src,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:42:47 +00:00
|
|
|
// Invalidate cache
|
|
|
|
|
|
|
|
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
|
|
|
|
|
|
|
// TODO: Do we want to error when the operation
|
|
|
|
// has succeeded but the cache invalidation has failed ?
|
|
|
|
if ok {
|
|
|
|
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
|
|
|
|
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
|
|
|
|
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:19:36 +00:00
|
|
|
return &File{
|
|
|
|
ID: info.Key,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveMutation(ctx context.Context, src, dest string) (*File, error) {
|
|
|
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no (spoon) move. Only copy and delete
|
|
|
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
|
|
|
Bucket: bucketName,
|
|
|
|
Object: dest,
|
|
|
|
}, minio.CopySrcOptions{
|
|
|
|
Bucket: bucketName,
|
|
|
|
Object: src,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = deleteMutation(ctx, src)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &File{
|
|
|
|
ID: info.Key,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|