mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-01-07 12:17:02 +08:00
minio: preview image cache
This commit is contained in:
parent
8a6f78e475
commit
1a86449035
@ -127,8 +127,6 @@ api:
|
|||||||
# apiURL is the address of the api, the access address of the app, use s3 must be configured
|
# apiURL is the address of the api, the access address of the app, use s3 must be configured
|
||||||
# minio.endpoint can be configured as an intranet address,
|
# minio.endpoint can be configured as an intranet address,
|
||||||
# minio.signEndpoint is minio public network address
|
# minio.signEndpoint is minio public network address
|
||||||
# minio.thumbnailApi is the address of the thumbnail service, the access address of the app, https://github.com/OpenIMSDK/minio-ext
|
|
||||||
# minio.thumbnailUseSignEndpoint whether the url parameter for generating thumbnails uses a signed address, the default is fine
|
|
||||||
object:
|
object:
|
||||||
enable: "minio"
|
enable: "minio"
|
||||||
apiURL: "http://127.0.0.1:10002"
|
apiURL: "http://127.0.0.1:10002"
|
||||||
@ -139,8 +137,6 @@ object:
|
|||||||
secretAccessKey: "openIM123"
|
secretAccessKey: "openIM123"
|
||||||
sessionToken: ""
|
sessionToken: ""
|
||||||
signEndpoint: "http://127.0.0.1:10005"
|
signEndpoint: "http://127.0.0.1:10005"
|
||||||
thumbnailApi: "http://127.0.0.1:10003"
|
|
||||||
thumbnailUseSignEndpoint: false
|
|
||||||
cos:
|
cos:
|
||||||
bucketURL: "https://temp-1252357374.cos.ap-chengdu.myqcloud.com"
|
bucketURL: "https://temp-1252357374.cos.ap-chengdu.myqcloud.com"
|
||||||
secretID: ""
|
secretID: ""
|
||||||
|
|||||||
@ -114,14 +114,12 @@ type configStruct struct {
|
|||||||
Enable string `yaml:"enable"`
|
Enable string `yaml:"enable"`
|
||||||
ApiURL string `yaml:"apiURL"`
|
ApiURL string `yaml:"apiURL"`
|
||||||
Minio struct {
|
Minio struct {
|
||||||
Bucket string `yaml:"bucket"`
|
Bucket string `yaml:"bucket"`
|
||||||
Endpoint string `yaml:"endpoint"`
|
Endpoint string `yaml:"endpoint"`
|
||||||
AccessKeyID string `yaml:"accessKeyID"`
|
AccessKeyID string `yaml:"accessKeyID"`
|
||||||
SecretAccessKey string `yaml:"secretAccessKey"`
|
SecretAccessKey string `yaml:"secretAccessKey"`
|
||||||
SessionToken string `yaml:"sessionToken"`
|
SessionToken string `yaml:"sessionToken"`
|
||||||
SignEndpoint string `yaml:"signEndpoint"`
|
SignEndpoint string `yaml:"signEndpoint"`
|
||||||
ThumbnailApi string `yaml:"thumbnailApi"`
|
|
||||||
ThumbnailUseSignEndpoint bool `yaml:"thumbnailUseSignEndpoint"`
|
|
||||||
} `yaml:"minio"`
|
} `yaml:"minio"`
|
||||||
Cos struct {
|
Cos struct {
|
||||||
BucketURL string `yaml:"bucketURL"`
|
BucketURL string `yaml:"bucketURL"`
|
||||||
|
|||||||
@ -256,7 +256,7 @@ func (c *Controller) IsNotFound(err error) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
func (c *Controller) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
||||||
if opt.Image != nil || opt.Video != nil {
|
if opt.Image != nil {
|
||||||
opt.Filename = ""
|
opt.Filename = ""
|
||||||
opt.ContentType = ""
|
opt.ContentType = ""
|
||||||
}
|
}
|
||||||
|
|||||||
@ -262,7 +262,6 @@ func (c *Cos) ListUploadedParts(ctx context.Context, uploadID string, name strin
|
|||||||
|
|
||||||
func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
||||||
var imageMogr string
|
var imageMogr string
|
||||||
//snapshot := make(url.Values)
|
|
||||||
var option cos.PresignedURLOptions
|
var option cos.PresignedURLOptions
|
||||||
if opt != nil {
|
if opt != nil {
|
||||||
query := make(url.Values)
|
query := make(url.Values)
|
||||||
@ -292,30 +291,6 @@ func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration,
|
|||||||
imageMogr = "&imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1"
|
imageMogr = "&imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if opt.Video != nil {
|
|
||||||
// 对象存储/桶存储列表/数据处理/媒体处理 开启
|
|
||||||
// https://cloud.tencent.com/document/product/436/55671
|
|
||||||
query.Set("ci-process", "snapshot")
|
|
||||||
sec := float64(opt.Video.Time/time.Millisecond) / 1000
|
|
||||||
if sec < 0 {
|
|
||||||
sec = 0
|
|
||||||
}
|
|
||||||
query.Set("time", strconv.FormatFloat(sec, 'f', 3, 64))
|
|
||||||
switch opt.Video.Format {
|
|
||||||
case
|
|
||||||
videoSnapshotImagePng,
|
|
||||||
videoSnapshotImageJpg:
|
|
||||||
default:
|
|
||||||
opt.Video.Format = videoSnapshotImageJpg
|
|
||||||
}
|
|
||||||
query.Set("format", opt.Video.Format)
|
|
||||||
if opt.Video.Width > 0 {
|
|
||||||
query.Set("width", strconv.Itoa(opt.Video.Width))
|
|
||||||
}
|
|
||||||
if opt.Video.Height > 0 {
|
|
||||||
query.Set("height", strconv.Itoa(opt.Video.Height))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if opt.ContentType != "" {
|
if opt.ContentType != "" {
|
||||||
query.Set("response-content-type", opt.ContentType)
|
query.Set("response-content-type", opt.ContentType)
|
||||||
}
|
}
|
||||||
|
|||||||
105
pkg/common/db/s3/minio/image.go
Normal file
105
pkg/common/db/s3/minio/image.go
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
package minio
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "golang.org/x/image/bmp"
|
||||||
|
_ "golang.org/x/image/tiff"
|
||||||
|
_ "golang.org/x/image/webp"
|
||||||
|
"image"
|
||||||
|
_ "image/gif"
|
||||||
|
_ "image/jpeg"
|
||||||
|
_ "image/png"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
formatPng = "png"
|
||||||
|
formatJpeg = "jpeg"
|
||||||
|
formatJpg = "jpg"
|
||||||
|
formatGif = "gif"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ImageStat(reader io.Reader) (image.Image, string, error) {
|
||||||
|
return image.Decode(reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ImageWidthHeight(img image.Image) (int, int) {
|
||||||
|
bounds := img.Bounds().Max
|
||||||
|
return bounds.X, bounds.Y
|
||||||
|
}
|
||||||
|
|
||||||
|
func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
||||||
|
bounds := img.Bounds()
|
||||||
|
imgWidth := bounds.Max.X
|
||||||
|
imgHeight := bounds.Max.Y
|
||||||
|
|
||||||
|
// 计算缩放比例
|
||||||
|
scaleWidth := float64(maxWidth) / float64(imgWidth)
|
||||||
|
scaleHeight := float64(maxHeight) / float64(imgHeight)
|
||||||
|
|
||||||
|
// 如果都为0,则不缩放,返回原始图片
|
||||||
|
if maxWidth == 0 && maxHeight == 0 {
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果宽度和高度都大于0,则选择较小的缩放比例,以保持宽高比
|
||||||
|
if maxWidth > 0 && maxHeight > 0 {
|
||||||
|
scale := scaleWidth
|
||||||
|
if scaleHeight < scaleWidth {
|
||||||
|
scale = scaleHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算缩略图尺寸
|
||||||
|
thumbnailWidth := int(float64(imgWidth) * scale)
|
||||||
|
thumbnailHeight := int(float64(imgHeight) * scale)
|
||||||
|
|
||||||
|
// 使用"image"库的Resample方法生成缩略图
|
||||||
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
||||||
|
for y := 0; y < thumbnailHeight; y++ {
|
||||||
|
for x := 0; x < thumbnailWidth; x++ {
|
||||||
|
srcX := int(float64(x) / scale)
|
||||||
|
srcY := int(float64(y) / scale)
|
||||||
|
thumbnail.Set(x, y, img.At(srcX, srcY))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return thumbnail
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果只指定了宽度或高度,则根据最大不超过的规则生成缩略图
|
||||||
|
if maxWidth > 0 {
|
||||||
|
thumbnailWidth := maxWidth
|
||||||
|
thumbnailHeight := int(float64(imgHeight) * scaleWidth)
|
||||||
|
|
||||||
|
// 使用"image"库的Resample方法生成缩略图
|
||||||
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
||||||
|
for y := 0; y < thumbnailHeight; y++ {
|
||||||
|
for x := 0; x < thumbnailWidth; x++ {
|
||||||
|
srcX := int(float64(x) / scaleWidth)
|
||||||
|
srcY := int(float64(y) / scaleWidth)
|
||||||
|
thumbnail.Set(x, y, img.At(srcX, srcY))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return thumbnail
|
||||||
|
}
|
||||||
|
|
||||||
|
if maxHeight > 0 {
|
||||||
|
thumbnailWidth := int(float64(imgWidth) * scaleHeight)
|
||||||
|
thumbnailHeight := maxHeight
|
||||||
|
|
||||||
|
// 使用"image"库的Resample方法生成缩略图
|
||||||
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
||||||
|
for y := 0; y < thumbnailHeight; y++ {
|
||||||
|
for x := 0; x < thumbnailWidth; x++ {
|
||||||
|
srcX := int(float64(x) / scaleHeight)
|
||||||
|
srcY := int(float64(y) / scaleHeight)
|
||||||
|
thumbnail.Set(x, y, img.At(srcX, srcY))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return thumbnail
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认情况下,返回原始图片
|
||||||
|
return img
|
||||||
|
}
|
||||||
@ -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 {
|
|
||||||
client = m.sign
|
|
||||||
} else if m.imageUseSignAddr {
|
|
||||||
client = m.sign
|
|
||||||
} else {
|
|
||||||
client = m.core.Client
|
|
||||||
}
|
|
||||||
u, err := client.PresignedGetObject(ctx, m.bucket, name, expire, reqParams)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if opt.Image == nil && opt.Video == nil {
|
if fileInfo.Size > maxImageSize {
|
||||||
return u.String(), nil
|
return "", errors.New("file size too large")
|
||||||
}
|
}
|
||||||
if m.imageApi == "" {
|
objectInfoPath := path.Join(pathInfo, fileInfo.ETag, "image.json")
|
||||||
return "", errs.ErrInternalServer.Wrap("minio: thumbnail not configured")
|
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 {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
query := make(url.Values)
|
if opt.Image.Width > info.Width || opt.Image.Width <= 0 {
|
||||||
query.Set("url", u.String())
|
opt.Image.Width = info.Width
|
||||||
if opt.Image != nil {
|
|
||||||
query.Set("type", "image")
|
|
||||||
query.Set("width", strconv.Itoa(opt.Image.Width))
|
|
||||||
query.Set("height", strconv.Itoa(opt.Image.Height))
|
|
||||||
query.Set("format", opt.Image.Format)
|
|
||||||
}
|
}
|
||||||
if opt.Video != nil {
|
if opt.Image.Height > info.Height || opt.Image.Height <= 0 {
|
||||||
query.Set("type", "video")
|
opt.Image.Height = info.Height
|
||||||
query.Set("time", strconv.Itoa(int(opt.Video.Time/time.Millisecond)))
|
|
||||||
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
|
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
|
||||||
|
}
|
||||||
|
if img == 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thumbnail := resizeImage(img, opt.Image.Width, opt.Image.Height)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return m.presignedGetObject(ctx, cacheKey, expire, reqParams)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Minio) getObjectData(ctx context.Context, name string, limit int64) ([]byte, error) {
|
||||||
|
object, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer object.Close()
|
||||||
|
if limit < 0 {
|
||||||
|
return io.ReadAll(object)
|
||||||
|
}
|
||||||
|
return io.ReadAll(io.LimitReader(object, 1024))
|
||||||
}
|
}
|
||||||
|
|||||||
8
pkg/common/db/s3/minio/struct.go
Normal file
8
pkg/common/db/s3/minio/struct.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package minio
|
||||||
|
|
||||||
|
type minioImageInfo struct {
|
||||||
|
NotImage bool `json:"notImage,omitempty"`
|
||||||
|
Width int `json:"width,omitempty"`
|
||||||
|
Height int `json:"height,omitempty"`
|
||||||
|
Format string `json:"format,omitempty"`
|
||||||
|
}
|
||||||
@ -299,29 +299,6 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration,
|
|||||||
process += ",format," + format
|
process += ",format," + format
|
||||||
opts = append(opts, oss.Process(process))
|
opts = append(opts, oss.Process(process))
|
||||||
}
|
}
|
||||||
if opt.Video != nil {
|
|
||||||
// 文档地址: https://help.aliyun.com/zh/oss/user-guide/video-snapshots?spm=a2c4g.11186623.0.0.23f743b0BR5WxX
|
|
||||||
// x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast
|
|
||||||
millisecond := int(opt.Video.Time / time.Millisecond)
|
|
||||||
if millisecond < 0 {
|
|
||||||
millisecond = 0
|
|
||||||
}
|
|
||||||
switch opt.Video.Format {
|
|
||||||
case videoSnapshotImageJpg, videoSnapshotImagePng:
|
|
||||||
default:
|
|
||||||
opt.Video.Format = videoSnapshotImageJpg
|
|
||||||
}
|
|
||||||
process := "video/snapshot,t_" + strconv.Itoa(millisecond) + ",f_" + opt.Video.Format
|
|
||||||
if opt.Video.Width > 0 {
|
|
||||||
process += ",w_" + strconv.Itoa(opt.Video.Width)
|
|
||||||
}
|
|
||||||
if opt.Video.Height > 0 {
|
|
||||||
process += ",h_" + strconv.Itoa(opt.Video.Height)
|
|
||||||
}
|
|
||||||
process += ",ar_auto,m_fast"
|
|
||||||
fmt.Println(process)
|
|
||||||
opts = append(opts, oss.Process(process))
|
|
||||||
}
|
|
||||||
if opt.ContentType != "" {
|
if opt.ContentType != "" {
|
||||||
opts = append(opts, oss.ResponseContentType(opt.ContentType))
|
opts = append(opts, oss.ResponseContentType(opt.ContentType))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -122,18 +122,10 @@ type Image struct {
|
|||||||
Height int `json:"height"`
|
Height int `json:"height"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Video struct {
|
|
||||||
Width int `json:"width"`
|
|
||||||
Height int `json:"height"`
|
|
||||||
Time time.Duration `json:"time"`
|
|
||||||
Format string `json:"format"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AccessURLOption struct {
|
type AccessURLOption struct {
|
||||||
ContentType string `json:"contentType"`
|
ContentType string `json:"contentType"`
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Image *Image `json:"image"`
|
Image *Image `json:"image"`
|
||||||
Video *Video `json:"video"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Interface interface {
|
type Interface interface {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user