package customize
import (
"context"
"custom-go/pkg/plugins"
"custom-go/pkg/types"
"custom-go/pkg/utils"
"custom-go/pkg/wgpb"
"fmt"
"net/url"
"time"
"github.com/graphql-go/graphql"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var (
fields = graphql.Fields{
"presignedURL": &graphql.Field{
Type: graphql.String,
Description: "生成S3的临时地址",
Args: graphql.FieldConfigArgument{
"fileName": &graphql.ArgumentConfig{
Type: graphql.String,
},
"providerName": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
_ = plugins.GetGraphqlContext(params)
providerName, _ := params.Args["providerName"].(string)
fileName, _ := params.Args["fileName"].(string)
provider := types.GetS3ConfigByProvider(providerName)
client, err := NewMinioClient(provider)
if err != nil {
return nil, err
}
reqParams := make(url.Values)
reqParams.Set("response-content-disposition", fmt.Sprintf("attachment; filename=\"%s\"", fileName))
// Generates a presigned url which expires in a day.
presignedURL, err := client.PresignedGetObject(context.TODO(), utils.GetConfigurationVal(provider.BucketName), fileName, time.Second*24*60*60, reqParams)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s://%s%s?%s", presignedURL.Scheme, presignedURL.Host, presignedURL.Path, presignedURL.RawQuery)
return url, nil
},
},
}
rootQuery = graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
S3_schema, _ = graphql.NewSchema(graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)})
)
func NewMinioClient(s3Upload *wgpb.S3UploadConfiguration) (client *minio.Client, err error) {
client, err = minio.New(utils.GetConfigurationVal(s3Upload.Endpoint), &minio.Options{
Creds: credentials.NewStaticV4(utils.GetConfigurationVal(s3Upload.AccessKeyID), utils.GetConfigurationVal(s3Upload.SecretAccessKey), ""),
Secure: s3Upload.UseSSL,
})
return
}