添加头像
This commit is contained in:
+83
-6
@@ -3,12 +3,17 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"math/rand"
|
||||
"mime/multipart"
|
||||
"strconv"
|
||||
"time"
|
||||
"vote-admin-video-api/api/dao"
|
||||
"vote-admin-video-api/api/dto"
|
||||
"vote-admin-video-api/api/model"
|
||||
"vote-admin-video-api/api/requests"
|
||||
"vote-admin-video-api/api/responses"
|
||||
"vote-admin-video-api/extend/aliyun"
|
||||
"vote-admin-video-api/global"
|
||||
"vote-admin-video-api/utils"
|
||||
)
|
||||
@@ -246,9 +251,10 @@ func (r *Video) PutVideo(c *gin.Context) {
|
||||
}
|
||||
|
||||
videoAuthor := &model.VideoAuthor{
|
||||
VideoId: videoId,
|
||||
AuthorName: author.AuthorName,
|
||||
HospitalId: hospitalId,
|
||||
VideoId: videoId,
|
||||
AuthorName: author.AuthorName,
|
||||
AuthorAvatar: author.AuthorAvatar,
|
||||
HospitalId: hospitalId,
|
||||
}
|
||||
videoAuthor, err = VideoAuthorDao.AddVideoAuthor(tx, videoAuthor)
|
||||
if err != nil {
|
||||
@@ -358,9 +364,10 @@ func (r *Video) AddVideo(c *gin.Context) {
|
||||
}
|
||||
|
||||
videoAuthor := &model.VideoAuthor{
|
||||
VideoId: video.VideoId,
|
||||
AuthorName: author.AuthorName,
|
||||
HospitalId: hospitalId,
|
||||
VideoId: video.VideoId,
|
||||
AuthorName: author.AuthorName,
|
||||
AuthorAvatar: author.AuthorAvatar,
|
||||
HospitalId: hospitalId,
|
||||
}
|
||||
videoAuthor, err = videoAuthorDao.AddVideoAuthor(tx, videoAuthor)
|
||||
if err != nil {
|
||||
@@ -435,3 +442,73 @@ func (r *Video) PutVideoStatus(c *gin.Context) {
|
||||
tx.Commit()
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// UploadAuthorAvatar 上传作者头像
|
||||
func (r *Video) UploadAuthorAvatar(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文件错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := file.Open()
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文件错误", c)
|
||||
return
|
||||
}
|
||||
defer func(f multipart.File) {
|
||||
err := f.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}(f)
|
||||
|
||||
// 读取文件内容到字节切片
|
||||
fileBytes, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文件错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 验证文件类型(只允许图片)
|
||||
fileType := "jpg"
|
||||
if file.Filename != "" {
|
||||
fileType = utils.GetExtension(file.Filename)
|
||||
if fileType == "" {
|
||||
fileType = "jpg"
|
||||
}
|
||||
}
|
||||
|
||||
// 验证是否为图片格式
|
||||
allowedTypes := map[string]bool{
|
||||
"jpg": true,
|
||||
"jpeg": true,
|
||||
"png": true,
|
||||
"gif": true,
|
||||
"webp": true,
|
||||
}
|
||||
if !allowedTypes[fileType] {
|
||||
responses.FailWithMessage("只支持图片格式(jpg、jpeg、png、gif、webp)", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成OSS路径
|
||||
now := time.Now()
|
||||
dateTimeString := now.Format("20060102150405")
|
||||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
ossPath := "static/author_avatar/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType
|
||||
|
||||
// 上传到OSS
|
||||
_, err = aliyun.PutObjectByte(ossPath, fileBytes)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 返回上传后的URL
|
||||
result := make(map[string]interface{})
|
||||
result["url"] = utils.AddOssDomain("/" + ossPath)
|
||||
result["path"] = "/" + ossPath
|
||||
|
||||
responses.OkWithData(result, c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user