2025-10-04 20:48:50 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/JACKYMYPERSON/hldrCenter/config"
|
|
|
|
|
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// UploadImageHandler 处理图片上传到OSS
|
2025-10-04 21:18:35 +08:00
|
|
|
|
func UploadImageHandler(cfg *config.Config) gin.HandlerFunc {
|
|
|
|
|
|
// 闭包:内部函数可以访问 cfg 参数
|
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
|
// 获取上传的图片文件
|
|
|
|
|
|
fileHeader, err := c.FormFile("image")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
|
|
"code": 400,
|
|
|
|
|
|
"message": "获取图片失败,请重新上传",
|
|
|
|
|
|
"error": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 打开文件
|
|
|
|
|
|
file, err := fileHeader.Open()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"code": 500,
|
|
|
|
|
|
"message": "打开图片文件失败",
|
|
|
|
|
|
"error": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
defer file.Close()
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 检查文件大小(使用配置中的max_file_size)
|
|
|
|
|
|
if fileHeader.Size > cfg.Upload.MaxFileSize {
|
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
|
|
"code": 400,
|
|
|
|
|
|
"message": fmt.Sprintf("图片大小不能超过 %dMB", cfg.Upload.MaxFileSize>>20),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 检查文件类型(使用配置中的allow_image_types)
|
|
|
|
|
|
fileType := fileHeader.Header.Get("Content-Type")
|
|
|
|
|
|
if !strings.Contains(cfg.Upload.AllowImageTypes, fileType) {
|
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
|
|
"code": 400,
|
|
|
|
|
|
"message": fmt.Sprintf("不支持的图片类型,仅允许:%s", cfg.Upload.AllowImageTypes),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 初始化OSS客户端(使用配置中的OSS参数)
|
|
|
|
|
|
client, err := oss.New(
|
|
|
|
|
|
cfg.OSS.Endpoint,
|
|
|
|
|
|
cfg.OSS.AccessKeyID,
|
|
|
|
|
|
cfg.OSS.AccessKeySecret,
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"code": 500,
|
|
|
|
|
|
"message": "初始化OSS客户端失败",
|
|
|
|
|
|
"error": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 获取Bucket
|
|
|
|
|
|
bucket, err := client.Bucket(cfg.OSS.BucketName)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"code": 500,
|
|
|
|
|
|
"message": "获取Bucket失败",
|
|
|
|
|
|
"error": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 生成唯一文件名
|
|
|
|
|
|
timestamp := time.Now().Format("20060102150405")
|
|
|
|
|
|
filename := fmt.Sprintf("%s_%s", timestamp, fileHeader.Filename)
|
|
|
|
|
|
objectKey := cfg.OSS.ObjectPrefix + filename // OSS中的完整对象键
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 上传文件到OSS
|
|
|
|
|
|
err = bucket.PutObject(objectKey, file)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"code": 500,
|
|
|
|
|
|
"message": "上传图片到OSS失败",
|
|
|
|
|
|
"error": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 生成图片访问URL
|
|
|
|
|
|
host := strings.TrimPrefix(cfg.OSS.Endpoint, "https://")
|
|
|
|
|
|
imageURL := fmt.Sprintf("https://%s.%s/%s", cfg.OSS.BucketName, host, objectKey)
|
2025-10-04 20:48:50 +08:00
|
|
|
|
|
2025-10-04 21:18:35 +08:00
|
|
|
|
// 返回成功响应
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 200,
|
|
|
|
|
|
"message": "图片上传成功",
|
|
|
|
|
|
"data": gin.H{"url": imageURL},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-10-04 20:48:50 +08:00
|
|
|
|
}
|