feat: 实现用户头像上传功能
- 创建头像上传API接口 (POST /api/v1/users/:id/avatar) - 实现完整的头像上传逻辑,包含权限验证和文件处理 - 添加头像图片处理功能,支持自动压缩和居中裁剪 - 完善静态文件服务,支持头像访问 - 创建完整的API测试用例 - 更新任务进度文档 任务11已完成,项目完成率提升至37.5%
This commit is contained in:
@ -14,12 +14,6 @@ import "user.api"
|
||||
import "photo.api"
|
||||
import "category.api"
|
||||
|
||||
// JWT 认证配置
|
||||
@server (
|
||||
jwt: Auth
|
||||
)
|
||||
service photography-api { // 健康检查接口 (无需认证)}
|
||||
|
||||
// 健康检查接口 (无需认证)
|
||||
@server (
|
||||
group: health
|
||||
|
||||
@ -70,6 +70,21 @@ type DeleteUserResponse {
|
||||
BaseResponse
|
||||
}
|
||||
|
||||
// 上传头像请求
|
||||
type UploadAvatarRequest {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
// 上传头像响应
|
||||
type UploadAvatarResponse {
|
||||
BaseResponse
|
||||
Data UploadAvatarData `json:"data"`
|
||||
}
|
||||
|
||||
type UploadAvatarData {
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
}
|
||||
|
||||
// 用户管理接口组
|
||||
@server(
|
||||
group: user
|
||||
@ -96,4 +111,8 @@ service photography-api {
|
||||
@doc "删除用户"
|
||||
@handler deleteUser
|
||||
delete /:id (DeleteUserRequest) returns (DeleteUserResponse)
|
||||
|
||||
@doc "上传用户头像"
|
||||
@handler uploadAvatar
|
||||
post /:id/avatar (UploadAvatarRequest) returns (UploadAvatarResponse)
|
||||
}
|
||||
@ -30,9 +30,9 @@ func main() {
|
||||
// 添加静态文件服务
|
||||
server.AddRoute(rest.Route{
|
||||
Method: http.MethodGet,
|
||||
Path: "/uploads/:path",
|
||||
Path: "/uploads/*",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
http.StripPrefix("/uploads/", http.FileServer(http.Dir(c.FileUpload.UploadDir))).ServeHTTP(w, r)
|
||||
http.StripPrefix("/uploads/", http.FileServer(http.Dir("uploads"))).ServeHTTP(w, r)
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@ -17,11 +17,6 @@ import (
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{},
|
||||
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
@ -158,6 +153,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/:id",
|
||||
Handler: user.DeleteUserHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 上传用户头像
|
||||
Method: http.MethodPost,
|
||||
Path: "/:id/avatar",
|
||||
Handler: user.UploadAvatarHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
|
||||
rest.WithPrefix("/api/v1/users"),
|
||||
|
||||
29
backend/internal/handler/user/uploadavatarhandler.go
Normal file
29
backend/internal/handler/user/uploadavatarhandler.go
Normal file
@ -0,0 +1,29 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"photography-backend/internal/logic/user"
|
||||
"photography-backend/internal/svc"
|
||||
"photography-backend/internal/types"
|
||||
)
|
||||
|
||||
// 上传用户头像
|
||||
func UploadAvatarHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UploadAvatarRequest
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewUploadAvatarLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UploadAvatar(&req, r)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
157
backend/internal/logic/user/uploadavatarlogic.go
Normal file
157
backend/internal/logic/user/uploadavatarlogic.go
Normal file
@ -0,0 +1,157 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"photography-backend/internal/svc"
|
||||
"photography-backend/internal/types"
|
||||
"photography-backend/pkg/errorx"
|
||||
"photography-backend/pkg/utils/file"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UploadAvatarLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 上传用户头像
|
||||
func NewUploadAvatarLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadAvatarLogic {
|
||||
return &UploadAvatarLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UploadAvatarLogic) UploadAvatar(req *types.UploadAvatarRequest, r *http.Request) (resp *types.UploadAvatarResponse, err error) {
|
||||
// 1. 验证用户权限
|
||||
userId := l.ctx.Value("userId").(int64)
|
||||
if userId != req.Id {
|
||||
return nil, errorx.New(errorx.Forbidden, "您只能更新自己的头像")
|
||||
}
|
||||
|
||||
// 2. 检查用户是否存在
|
||||
user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errorx.New(errorx.UserNotFound, "用户不存在")
|
||||
}
|
||||
|
||||
// 3. 解析多部分表单
|
||||
err = r.ParseMultipartForm(10 << 20) // 10MB
|
||||
if err != nil {
|
||||
return nil, errorx.New(errorx.ParamError, "解析表单失败")
|
||||
}
|
||||
|
||||
// 4. 获取上传的文件
|
||||
uploadedFile, header, err := r.FormFile("avatar")
|
||||
if err != nil {
|
||||
return nil, errorx.New(errorx.ParamError, "获取上传文件失败: " + err.Error())
|
||||
}
|
||||
defer uploadedFile.Close()
|
||||
|
||||
// 5. 验证文件类型 (头像应该是图片)
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
if !strings.HasPrefix(contentType, "image/") {
|
||||
return nil, errorx.New(errorx.ParamError, "头像必须是图片文件")
|
||||
}
|
||||
|
||||
// 6. 验证文件大小 (限制为5MB)
|
||||
if header.Size > 5*1024*1024 {
|
||||
return nil, errorx.New(errorx.ParamError, "头像文件大小不能超过5MB")
|
||||
}
|
||||
|
||||
// 7. 生成头像文件名和路径
|
||||
ext := filepath.Ext(header.Filename)
|
||||
if ext == "" {
|
||||
// 根据Content-Type推断扩展名
|
||||
switch contentType {
|
||||
case "image/jpeg":
|
||||
ext = ".jpg"
|
||||
case "image/png":
|
||||
ext = ".png"
|
||||
case "image/gif":
|
||||
ext = ".gif"
|
||||
case "image/webp":
|
||||
ext = ".webp"
|
||||
default:
|
||||
ext = ".jpg"
|
||||
}
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("avatar_%d_%d%s", req.Id, time.Now().Unix(), ext)
|
||||
avatarDir := "uploads/avatars"
|
||||
|
||||
// 8. 确保头像目录存在
|
||||
if err := os.MkdirAll(avatarDir, 0755); err != nil {
|
||||
return nil, errorx.New(errorx.ServerError, "创建头像目录失败: " + err.Error())
|
||||
}
|
||||
|
||||
avatarPath := filepath.Join(avatarDir, filename)
|
||||
|
||||
// 9. 保存原始头像文件
|
||||
destFile, err := os.Create(avatarPath)
|
||||
if err != nil {
|
||||
return nil, errorx.New(errorx.ServerError, "创建头像文件失败: " + err.Error())
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
_, err = io.Copy(destFile, uploadedFile)
|
||||
if err != nil {
|
||||
return nil, errorx.New(errorx.ServerError, "保存头像文件失败: " + err.Error())
|
||||
}
|
||||
|
||||
// 10. 生成压缩版本的头像 (150x150像素)
|
||||
compressedPath := filepath.Join(avatarDir, fmt.Sprintf("avatar_%d_%d_150x150%s", req.Id, time.Now().Unix(), ext))
|
||||
err = file.ResizeImage(avatarPath, compressedPath, 150, 150)
|
||||
if err != nil {
|
||||
l.Errorf("头像压缩失败: %v", err)
|
||||
// 压缩失败不影响主流程,使用原图
|
||||
compressedPath = avatarPath
|
||||
}
|
||||
|
||||
// 11. 删除旧的头像文件 (如果存在)
|
||||
if user.Avatar != "" && user.Avatar != compressedPath {
|
||||
oldAvatarPath := user.Avatar
|
||||
if strings.HasPrefix(oldAvatarPath, "/uploads/") {
|
||||
oldAvatarPath = strings.TrimPrefix(oldAvatarPath, "/")
|
||||
}
|
||||
if _, err := os.Stat(oldAvatarPath); err == nil {
|
||||
os.Remove(oldAvatarPath)
|
||||
l.Infof("删除旧头像文件: %s", oldAvatarPath)
|
||||
}
|
||||
}
|
||||
|
||||
// 12. 更新用户头像URL
|
||||
avatarUrl := "/" + compressedPath
|
||||
user.Avatar = avatarUrl
|
||||
|
||||
err = l.svcCtx.UserModel.Update(l.ctx, user)
|
||||
if err != nil {
|
||||
// 更新失败,删除已上传的文件
|
||||
os.Remove(avatarPath)
|
||||
if avatarPath != compressedPath {
|
||||
os.Remove(compressedPath)
|
||||
}
|
||||
return nil, errorx.New(errorx.ServerError, "更新用户头像失败: " + err.Error())
|
||||
}
|
||||
|
||||
return &types.UploadAvatarResponse{
|
||||
BaseResponse: types.BaseResponse{
|
||||
Code: 200,
|
||||
Message: "头像上传成功",
|
||||
},
|
||||
Data: types.UploadAvatarData{
|
||||
AvatarUrl: avatarUrl,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -215,6 +215,19 @@ type UpdateUserResponse struct {
|
||||
Data User `json:"data"`
|
||||
}
|
||||
|
||||
type UploadAvatarData struct {
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
}
|
||||
|
||||
type UploadAvatarRequest struct {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
type UploadAvatarResponse struct {
|
||||
BaseResponse
|
||||
Data UploadAvatarData `json:"data"`
|
||||
}
|
||||
|
||||
type UploadPhotoRequest struct {
|
||||
Title string `json:"title" validate:"required"`
|
||||
Description string `json:"description,optional"`
|
||||
|
||||
@ -201,4 +201,44 @@ func GetImageDimensions(filePath string) (width, height int, err error) {
|
||||
}
|
||||
|
||||
return img.Width, img.Height, nil
|
||||
}
|
||||
|
||||
// ResizeImage 调整图片尺寸 (用于头像处理)
|
||||
func ResizeImage(srcPath, destPath string, width, height int) error {
|
||||
// 打开原图
|
||||
src, err := imaging.Open(srcPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开图片失败: %v", err)
|
||||
}
|
||||
|
||||
// 调整图片尺寸 (正方形裁剪)
|
||||
resized := imaging.Fill(src, width, height, imaging.Center, imaging.Lanczos)
|
||||
|
||||
// 保存调整后的图片
|
||||
err = imaging.Save(resized, destPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("保存调整后的图片失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateAvatar 创建头像(包含压缩和格式转换)
|
||||
func CreateAvatar(srcPath, destPath string, size int) error {
|
||||
// 打开原图
|
||||
src, err := imaging.Open(srcPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开图片失败: %v", err)
|
||||
}
|
||||
|
||||
// 创建正方形头像 (居中裁剪)
|
||||
avatar := imaging.Fill(src, size, size, imaging.Center, imaging.Lanczos)
|
||||
|
||||
// 保存为JPEG格式 (压缩优化)
|
||||
err = imaging.Save(avatar, destPath, imaging.JPEGQuality(85))
|
||||
if err != nil {
|
||||
return fmt.Errorf("保存头像失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
98
backend/test_avatar_upload.http
Normal file
98
backend/test_avatar_upload.http
Normal file
@ -0,0 +1,98 @@
|
||||
### 用户头像上传测试用例
|
||||
|
||||
### 1. 用户登录获取Token
|
||||
POST {{host}}/api/v1/auth/login
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "admin123"
|
||||
}
|
||||
|
||||
### 2. 上传用户头像 (正常场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/avatar.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 3. 上传头像 - 文件过大 (错误场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="large_image.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/large_image.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 4. 上传头像 - 非图片文件 (错误场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="document.txt"
|
||||
Content-Type: text/plain
|
||||
|
||||
This is not an image file
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 5. 上传头像 - 未认证用户 (错误场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/avatar.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 6. 上传其他用户头像 - 权限不足 (错误场景)
|
||||
POST {{host}}/api/v1/users/999/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/avatar.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 7. 获取用户信息查看头像URL
|
||||
GET {{host}}/api/v1/users/1
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### 8. 访问头像静态文件
|
||||
GET {{host}}/uploads/avatars/avatar_1_1641234567_150x150.jpg
|
||||
|
||||
### 测试环境变量
|
||||
@host = http://localhost:8080
|
||||
@token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
|
||||
|
||||
### 测试说明
|
||||
# 1. 确保在项目根目录下有 test_images 文件夹
|
||||
# 2. 准备以下测试文件:
|
||||
# - test_images/avatar.jpg (正常大小的头像图片, < 5MB)
|
||||
# - test_images/large_image.jpg (超过5MB的图片)
|
||||
# 3. 替换 {{token}} 为实际的JWT令牌
|
||||
# 4. 替换用户ID为实际存在的用户ID
|
||||
|
||||
### 预期结果
|
||||
# 1. 正常上传应返回 200 状态码和头像URL
|
||||
# 2. 文件过大应返回 400 错误
|
||||
# 3. 非图片文件应返回 400 错误
|
||||
# 4. 未认证用户应返回 401 错误
|
||||
# 5. 权限不足应返回 403 错误
|
||||
# 6. 头像文件应保存到 uploads/avatars/ 目录
|
||||
# 7. 应生成压缩版本 (150x150像素)
|
||||
# 8. 用户信息中的 avatar 字段应更新为新的头像URL
|
||||
Reference in New Issue
Block a user