|
|
@ -15,16 +15,24 @@
|
|
|
|
package minio
|
|
|
|
package minio
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/OpenIMSDK/tools/errs"
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/tools/log"
|
|
|
|
"github.com/OpenIMSDK/tools/log"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/signer"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/signer"
|
|
|
|
|
|
|
|
"image"
|
|
|
|
|
|
|
|
"image/gif"
|
|
|
|
|
|
|
|
"image/jpeg"
|
|
|
|
|
|
|
|
"image/png"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
@ -46,6 +54,13 @@ const (
|
|
|
|
maxNumSize = 10000
|
|
|
|
maxNumSize = 10000
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
maxImageWidth = 1024
|
|
|
|
|
|
|
|
maxImageHeight = 1024
|
|
|
|
|
|
|
|
maxImageSize = 1024 * 1024 * 50
|
|
|
|
|
|
|
|
pathInfo = "/minio/thumbnail"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func NewMinio() (s3.Interface, error) {
|
|
|
|
func NewMinio() (s3.Interface, error) {
|
|
|
|
conf := config.Config.Object.Minio
|
|
|
|
conf := config.Config.Object.Minio
|
|
|
|
u, err := url.Parse(conf.Endpoint)
|
|
|
|
u, err := url.Parse(conf.Endpoint)
|
|
|
@ -60,21 +75,12 @@ func NewMinio() (s3.Interface, error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imageApi := conf.ThumbnailApi
|
|
|
|
|
|
|
|
if imageApi != "" {
|
|
|
|
|
|
|
|
if imageApi[len(imageApi)-1] != '/' {
|
|
|
|
|
|
|
|
imageApi += "/"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
imageApi += "image?"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &Minio{
|
|
|
|
m := &Minio{
|
|
|
|
bucket: conf.Bucket,
|
|
|
|
bucket: conf.Bucket,
|
|
|
|
bucketURL: conf.Endpoint + "/" + conf.Bucket + "/",
|
|
|
|
bucketURL: conf.Endpoint + "/" + conf.Bucket + "/",
|
|
|
|
imageApi: imageApi,
|
|
|
|
core: &minio.Core{Client: client},
|
|
|
|
imageUseSignAddr: conf.ThumbnailUseSignEndpoint,
|
|
|
|
lock: &sync.Mutex{},
|
|
|
|
core: &minio.Core{Client: client},
|
|
|
|
init: false,
|
|
|
|
lock: &sync.Mutex{},
|
|
|
|
|
|
|
|
init: false,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conf.SignEndpoint == "" {
|
|
|
|
if conf.SignEndpoint == "" {
|
|
|
|
m.sign = m.core.Client
|
|
|
|
m.sign = m.core.Client
|
|
|
@ -101,16 +107,14 @@ func NewMinio() (s3.Interface, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Minio struct {
|
|
|
|
type Minio struct {
|
|
|
|
bucket string
|
|
|
|
bucket string
|
|
|
|
bucketURL string
|
|
|
|
bucketURL string
|
|
|
|
imageApi string
|
|
|
|
location string
|
|
|
|
imageUseSignAddr bool
|
|
|
|
opts *minio.Options
|
|
|
|
location string
|
|
|
|
core *minio.Core
|
|
|
|
opts *minio.Options
|
|
|
|
sign *minio.Client
|
|
|
|
core *minio.Core
|
|
|
|
lock sync.Locker
|
|
|
|
sign *minio.Client
|
|
|
|
init bool
|
|
|
|
lock sync.Locker
|
|
|
|
|
|
|
|
init bool
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Minio) initMinio(ctx context.Context) error {
|
|
|
|
func (m *Minio) initMinio(ctx context.Context) error {
|
|
|
@ -354,6 +358,19 @@ func (m *Minio) ListUploadedParts(ctx context.Context, uploadID string, name str
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Minio) presignedGetObject(ctx context.Context, name string, expire time.Duration, query url.Values) (string, error) {
|
|
|
|
|
|
|
|
if expire <= 0 {
|
|
|
|
|
|
|
|
expire = time.Hour * 24 * 365 * 99 // 99 years
|
|
|
|
|
|
|
|
} else if expire < time.Second {
|
|
|
|
|
|
|
|
expire = time.Second
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rawURL, err := m.sign.PresignedGetObject(ctx, m.bucket, name, expire, query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawURL.String(), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
|
|
|
func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
|
|
|
if err := m.initMinio(ctx); err != nil {
|
|
|
|
if err := m.initMinio(ctx); err != nil {
|
|
|
|
return "", err
|
|
|
|
return "", err
|
|
|
@ -367,43 +384,120 @@ func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration
|
|
|
|
reqParams.Set("response-content-disposition", `attachment; filename=`+strconv.Quote(opt.Filename))
|
|
|
|
reqParams.Set("response-content-disposition", `attachment; filename=`+strconv.Quote(opt.Filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if expire <= 0 {
|
|
|
|
if opt.Image == nil || (opt.Image.Width < 0 && opt.Image.Height < 0 && opt.Image.Format == "") || (opt.Image.Width > maxImageWidth || opt.Image.Height > maxImageHeight) {
|
|
|
|
expire = time.Hour * 24 * 365 * 99 // 99 years
|
|
|
|
return m.presignedGetObject(ctx, name, expire, reqParams)
|
|
|
|
} else if expire < time.Second {
|
|
|
|
|
|
|
|
expire = time.Second
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var client *minio.Client
|
|
|
|
fileInfo, err := m.StatObject(ctx, name)
|
|
|
|
if opt.Image == nil && opt.Video == nil {
|
|
|
|
if err != nil {
|
|
|
|
client = m.sign
|
|
|
|
return "", err
|
|
|
|
} else if m.imageUseSignAddr {
|
|
|
|
}
|
|
|
|
client = m.sign
|
|
|
|
if fileInfo.Size > maxImageSize {
|
|
|
|
|
|
|
|
return "", errors.New("file size too large")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
objectInfoPath := path.Join(pathInfo, fileInfo.ETag, "image.json")
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
img image.Image
|
|
|
|
|
|
|
|
info minioImageInfo
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := m.getObjectData(ctx, objectInfoPath, 1024)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &info); err != nil {
|
|
|
|
|
|
|
|
return "", fmt.Errorf("unmarshal minio image info.json error: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.NotImage {
|
|
|
|
|
|
|
|
return "", errors.New("not image")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if m.IsNotFound(err) {
|
|
|
|
|
|
|
|
reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
imageInfo, format, err := ImageStat(reader)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
info.NotImage = false
|
|
|
|
|
|
|
|
info.Format = format
|
|
|
|
|
|
|
|
info.Width, info.Height = ImageWidthHeight(imageInfo)
|
|
|
|
|
|
|
|
img = imageInfo
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
info.NotImage = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(&info)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.core.Client.PutObject(ctx, m.bucket, objectInfoPath, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
client = m.core.Client
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u, err := client.PresignedGetObject(ctx, m.bucket, name, expire, reqParams)
|
|
|
|
if opt.Image.Width > info.Width || opt.Image.Width <= 0 {
|
|
|
|
if err != nil {
|
|
|
|
opt.Image.Width = info.Width
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.Image.Height > info.Height || opt.Image.Height <= 0 {
|
|
|
|
|
|
|
|
opt.Image.Height = info.Height
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.Image.Format = strings.ToLower(opt.Image.Format)
|
|
|
|
|
|
|
|
if opt.Image.Format == formatJpg {
|
|
|
|
|
|
|
|
opt.Image.Format = formatJpeg
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch opt.Image.Format {
|
|
|
|
|
|
|
|
case formatPng:
|
|
|
|
|
|
|
|
case formatJpeg:
|
|
|
|
|
|
|
|
case formatGif:
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
if info.Format == formatGif {
|
|
|
|
|
|
|
|
opt.Image.Format = formatGif
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
opt.Image.Format = formatJpeg
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
reqParams.Set("response-content-type", "image/"+opt.Image.Format)
|
|
|
|
|
|
|
|
if opt.Image.Width == info.Width && opt.Image.Height == info.Height && opt.Image.Format == info.Format {
|
|
|
|
|
|
|
|
return m.presignedGetObject(ctx, name, expire, reqParams)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheKey := filepath.Join(pathInfo, fileInfo.ETag, fmt.Sprintf("image_w%d_h%d.%s", opt.Image.Width, opt.Image.Height, opt.Image.Format))
|
|
|
|
|
|
|
|
if _, err := m.core.Client.StatObject(ctx, m.bucket, cacheKey, minio.StatObjectOptions{}); err == nil {
|
|
|
|
|
|
|
|
return m.presignedGetObject(ctx, cacheKey, expire, reqParams)
|
|
|
|
|
|
|
|
} else if !m.IsNotFound(err) {
|
|
|
|
return "", err
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opt.Image == nil && opt.Video == nil {
|
|
|
|
if img == nil {
|
|
|
|
return u.String(), nil
|
|
|
|
reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
img, _, err = ImageStat(reader)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.imageApi == "" {
|
|
|
|
thumbnail := resizeImage(img, opt.Image.Width, opt.Image.Height)
|
|
|
|
return "", errs.ErrInternalServer.Wrap("minio: thumbnail not configured")
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
switch opt.Image.Format {
|
|
|
|
|
|
|
|
case formatPng:
|
|
|
|
|
|
|
|
err = png.Encode(buf, thumbnail)
|
|
|
|
|
|
|
|
case formatJpeg:
|
|
|
|
|
|
|
|
err = jpeg.Encode(buf, thumbnail, nil)
|
|
|
|
|
|
|
|
case formatGif:
|
|
|
|
|
|
|
|
err = gif.Encode(buf, thumbnail, nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.core.Client.PutObject(ctx, m.bucket, cacheKey, buf, int64(buf.Len()), minio.PutObjectOptions{}); err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query := make(url.Values)
|
|
|
|
return m.presignedGetObject(ctx, cacheKey, expire, reqParams)
|
|
|
|
query.Set("url", u.String())
|
|
|
|
}
|
|
|
|
if opt.Image != nil {
|
|
|
|
|
|
|
|
query.Set("type", "image")
|
|
|
|
func (m *Minio) getObjectData(ctx context.Context, name string, limit int64) ([]byte, error) {
|
|
|
|
query.Set("width", strconv.Itoa(opt.Image.Width))
|
|
|
|
object, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
|
|
|
query.Set("height", strconv.Itoa(opt.Image.Height))
|
|
|
|
if err != nil {
|
|
|
|
query.Set("format", opt.Image.Format)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opt.Video != nil {
|
|
|
|
defer object.Close()
|
|
|
|
query.Set("type", "video")
|
|
|
|
if limit < 0 {
|
|
|
|
query.Set("time", strconv.Itoa(int(opt.Video.Time/time.Millisecond)))
|
|
|
|
return io.ReadAll(object)
|
|
|
|
query.Set("width", strconv.Itoa(opt.Video.Width))
|
|
|
|
|
|
|
|
query.Set("height", strconv.Itoa(opt.Video.Height))
|
|
|
|
|
|
|
|
query.Set("format", opt.Video.Format)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m.imageApi + query.Encode(), nil
|
|
|
|
return io.ReadAll(io.LimitReader(object, 1024))
|
|
|
|
}
|
|
|
|
}
|
|
|
|