|
|
|
@ -32,6 +32,8 @@ type minioClient struct {
|
|
|
|
|
BucketName string
|
|
|
|
|
Source string
|
|
|
|
|
EndPoint string
|
|
|
|
|
Protocol string
|
|
|
|
|
FrontBase string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MinIO) Upload(ctx context.Context, fileHeader *multipart.FileHeader) (*dto.AttachmentDTO, error) {
|
|
|
|
@ -41,7 +43,7 @@ func (m *MinIO) Upload(ctx context.Context, fileHeader *multipart.FileHeader) (*
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd, err := newURLFileDescriptor(
|
|
|
|
|
withBaseURL(minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName),
|
|
|
|
|
withBaseURL(minioClientInstance.Protocol+minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName),
|
|
|
|
|
withSubURLPath(minioClientInstance.Source),
|
|
|
|
|
withShouldRenameURLOption(commonRenamePredicateFunc(ctx, consts.AttachmentTypeMinIO)),
|
|
|
|
|
withOriginalNameURLOption(fileHeader.Filename),
|
|
|
|
@ -103,14 +105,17 @@ func (m *MinIO) GetFilePath(ctx context.Context, relativePath string) (string, e
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
base := minioClientInstance.EndPoint + "/" + minioClientInstance.BucketName
|
|
|
|
|
base := minioClientInstance.Protocol + minioClientInstance.EndPoint + "/" + minioClientInstance.BucketName
|
|
|
|
|
if minioClientInstance.FrontBase != "" {
|
|
|
|
|
base = minioClientInstance.FrontBase
|
|
|
|
|
}
|
|
|
|
|
fullPath, _ := url.JoinPath(base, relativePath)
|
|
|
|
|
fullPath, _ = url.PathUnescape(fullPath)
|
|
|
|
|
return fullPath, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
|
|
|
|
|
getClientProperty := func(propertyValue *string, property property.Property, e error) error {
|
|
|
|
|
getClientProperty := func(propertyValue *string, property property.Property, allowEmpty bool, e error) error {
|
|
|
|
|
if e != nil {
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
@ -122,25 +127,37 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
|
|
|
|
|
if !ok {
|
|
|
|
|
return xerr.WithStatus(nil, xerr.StatusBadRequest).WithErrMsgf("wrong property type")
|
|
|
|
|
}
|
|
|
|
|
if strValue == "" {
|
|
|
|
|
if !allowEmpty && strValue == "" {
|
|
|
|
|
return xerr.WithStatus(nil, xerr.StatusInternalServerError).WithMsg("property not found: " + property.KeyValue)
|
|
|
|
|
}
|
|
|
|
|
*propertyValue = strValue
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var endPoint, bucketName, accessKey, accessSecret, source, region string
|
|
|
|
|
err := getClientProperty(&endPoint, property.MinioEndpoint, nil)
|
|
|
|
|
err = getClientProperty(&bucketName, property.MinioBucketName, err)
|
|
|
|
|
err = getClientProperty(&accessKey, property.MinioAccessKey, err)
|
|
|
|
|
err = getClientProperty(&accessSecret, property.MinioAccessSecret, err)
|
|
|
|
|
err = getClientProperty(&source, property.MinioSource, err)
|
|
|
|
|
err = getClientProperty(®ion, property.MinioRegion, err)
|
|
|
|
|
var endPoint, bucketName, accessKey, accessSecret, protocol, source, region, frontBase string
|
|
|
|
|
err := getClientProperty(&endPoint, property.MinioEndpoint, false, nil)
|
|
|
|
|
err = getClientProperty(&bucketName, property.MinioBucketName, false, err)
|
|
|
|
|
err = getClientProperty(&accessKey, property.MinioAccessKey, false, err)
|
|
|
|
|
err = getClientProperty(&accessSecret, property.MinioAccessSecret, false, err)
|
|
|
|
|
err = getClientProperty(&protocol, property.MinioProtocol, false, err)
|
|
|
|
|
err = getClientProperty(&source, property.MinioSource, true, err)
|
|
|
|
|
err = getClientProperty(®ion, property.MinioRegion, true, err)
|
|
|
|
|
err = getClientProperty(&frontBase, property.MinioFrontBase, true, err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
secure := func() bool {
|
|
|
|
|
switch protocol {
|
|
|
|
|
case "https://":
|
|
|
|
|
return true
|
|
|
|
|
case "http://":
|
|
|
|
|
return false
|
|
|
|
|
default:
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
client, err := minio.New(endPoint, &minio.Options{
|
|
|
|
|
Creds: credentials.NewStaticV4(accessKey, accessSecret, ""),
|
|
|
|
|
Secure: true,
|
|
|
|
|
Secure: secure,
|
|
|
|
|
Region: region,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
@ -153,5 +170,7 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
|
|
|
|
|
minioClientInstance.BucketName = bucketName
|
|
|
|
|
minioClientInstance.Source = source
|
|
|
|
|
minioClientInstance.EndPoint = endPoint
|
|
|
|
|
minioClientInstance.Protocol = protocol
|
|
|
|
|
minioClientInstance.FrontBase = frontBase
|
|
|
|
|
return minioClientInstance, nil
|
|
|
|
|
}
|
|
|
|
|