用DDD分项目模块
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package upload
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/upload/internal/logic/upload"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/upload/internal/svc"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func UploadImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
l := upload.NewUploadImageLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UploadImage()
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
132
server/internal/upload/internal/logic/upload/uploadimagelogic.go
Normal file
132
server/internal/upload/internal/logic/upload/uploadimagelogic.go
Normal file
@@ -0,0 +1,132 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package upload
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/upload/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/upload/internal/types"
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UploadImageLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUploadImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadImageLogic {
|
||||
return &UploadImageLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UploadImageLogic) UploadImage() (resp *types.UploadImageResp, err error) {
|
||||
// 获取上传的图片文件
|
||||
fileHeader, err := c.FormFile("image")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "获取图片失败,请重新上传",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 打开文件
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
"message": "打开图片文件失败",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 检查文件大小(使用配置中的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
|
||||
}
|
||||
|
||||
// 检查文件类型(使用配置中的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
|
||||
}
|
||||
|
||||
// 初始化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
|
||||
}
|
||||
|
||||
// 获取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
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
timestamp := time.Now().Format("20060102150405")
|
||||
filename := fmt.Sprintf("%s_%s", timestamp, fileHeader.Filename)
|
||||
objectKey := cfg.OSS.ObjectPrefix + filename // OSS中的完整对象键
|
||||
|
||||
// 上传文件到OSS
|
||||
err = bucket.PutObject(objectKey, file)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
"message": "上传图片到OSS失败",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 生成图片访问URL
|
||||
host := strings.TrimPrefix(cfg.OSS.Endpoint, "https://")
|
||||
imageURL := fmt.Sprintf("https://%s.%s/%s", cfg.OSS.BucketName, host, objectKey)
|
||||
|
||||
// 返回成功响应
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"message": "图片上传成功",
|
||||
"data": gin.H{"url": imageURL},
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
12
server/internal/upload/internal/types/types.go
Normal file
12
server/internal/upload/internal/types/types.go
Normal file
@@ -0,0 +1,12 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.1
|
||||
|
||||
package types
|
||||
|
||||
type UploadImageResp struct {
|
||||
Code int `json:"code"` // 业务状态码
|
||||
Message string `json:"message"` // 提示信息
|
||||
Data struct {
|
||||
Url string `json:"url"` // 图片访问地址
|
||||
} `json:"data"`
|
||||
}
|
||||
Reference in New Issue
Block a user