package s3browser import ( "context" "fmt" "strings" "github.com/graph-gophers/dataloader" "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? err := s3Client.RemoveObject(ctx, bucketName, id, minio.RemoveObjectOptions{}) if err != nil { return err } // Invalidate cache return invalidateCache(ctx, nomalizeID(id)) } 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") } // 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? dest += getFilenameFromID(src) } info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{ Bucket: bucketName, Object: dest, }, minio.CopySrcOptions{ Bucket: bucketName, Object: src, }) if err != nil { return nil, err } // Invalidate cache // TODO: check error invalidateCache(ctx, nomalizeID(info.Key)) 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") } // 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? dest += getFilenameFromID(src) } // 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 } invalidateCache(ctx, nomalizeID(info.Key)) return &File{ ID: info.Key, }, nil } func createDirectory(ctx context.Context, path string) (*Directory, error) { s3Client, ok := ctx.Value("s3Client").(*minio.Client) if !ok { return nil, fmt.Errorf("Failed to get s3Client from context") } if !strings.HasSuffix(path, "/") { path += "/" } info, err := s3Client.PutObject(ctx, bucketName, path, strings.NewReader(""), 0, minio.PutObjectOptions{ ContentType: "application/x-directory", }) if err != nil { return nil, err } // Invalidate cache // TODO: check error invalidateCacheForDir(ctx, nomalizeID(info.Key)) return &Directory{ ID: info.Key, }, nil } 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 thunk := loader["listObjectsRecursive"].Load(ctx, dataloader.StringKey(nomalizeID(path))) 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 err = deleteMultiple(ctx, *s3Client, files) 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 { err := s3Client.RemoveObject(ctx, bucketName, path, minio.RemoveObjectOptions{}) if err != nil { return err } } //Invalidate cache invalidateCacheForDir(ctx, nomalizeID(path)) return nil }