diff --git a/internal/mutations.go b/internal/mutations.go index 0e82feb..6cd680a 100644 --- a/internal/mutations.go +++ b/internal/mutations.go @@ -103,3 +103,28 @@ func moveMutation(ctx context.Context, src, dest string) (*File, error) { }, 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 + } + + return &Directory{ + ID: info.Key, + }, nil + +} diff --git a/internal/schema.go b/internal/schema.go index e423bec..b5f5a32 100644 --- a/internal/schema.go +++ b/internal/schema.go @@ -146,6 +146,24 @@ func graphqlSchema() (graphql.Schema, error) { return moveMutation(p.Context, src, dest) }, }, + "createDir": &graphql.Field{ + Type: graphql.NewNonNull(graphqlDirType), + Args: graphql.FieldConfigArgument{ + "path": &graphql.ArgumentConfig{ + Type: graphql.NewNonNull(graphql.ID), + }, + }, + Resolve: func(p graphql.ResolveParams) (interface{}, error) { + path, ok := p.Args["path"].(string) + if !ok { + return nil, fmt.Errorf("Failed to parse args") + } + + log.Debug("mutation 'createDir': ", path) + + return createDirectory(p.Context, path) + }, + }, } rootQuery := graphql.ObjectConfig{