feat: Enhance texture upload functionality and API response format

- Introduced a new upload endpoint for direct texture file uploads, allowing users to upload textures with validation for size and format.
- Updated existing texture-related API responses to a standardized format, improving consistency across the application.
- Refactored texture service methods to handle file uploads and reuse existing texture URLs based on hash checks.
- Cleaned up Dockerfile and other files by removing unnecessary whitespace.
This commit is contained in:
lan
2025-12-04 20:07:30 +08:00
parent 0bcd9336c4
commit 8858fd1ede
16 changed files with 295 additions and 24 deletions

View File

@@ -152,7 +152,23 @@ func (h *TextureHandler) Search(c *gin.Context) {
return
}
c.JSON(200, model.NewPaginationResponse(TexturesToTextureInfos(textures), total, page, pageSize))
// 返回格式:
// {
// "code": 200,
// "message": "操作成功",
// "data": {
// "list": [...],
// "total": 1,
// "page": 1,
// "per_page": 5
// }
// }
RespondSuccess(c, gin.H{
"list": TexturesToTextureInfos(textures),
"total": total,
"page": page,
"per_page": pageSize,
})
}
// Update 更新材质
@@ -258,7 +274,12 @@ func (h *TextureHandler) GetUserTextures(c *gin.Context) {
return
}
c.JSON(200, model.NewPaginationResponse(TexturesToTextureInfos(textures), total, page, pageSize))
RespondSuccess(c, gin.H{
"list": TexturesToTextureInfos(textures),
"total": total,
"page": page,
"per_page": pageSize,
})
}
// GetUserFavorites 获取用户收藏的材质列表
@@ -278,5 +299,92 @@ func (h *TextureHandler) GetUserFavorites(c *gin.Context) {
return
}
c.JSON(200, model.NewPaginationResponse(TexturesToTextureInfos(textures), total, page, pageSize))
RespondSuccess(c, gin.H{
"list": TexturesToTextureInfos(textures),
"total": total,
"page": page,
"per_page": pageSize,
})
}
// Upload 直接上传材质文件
func (h *TextureHandler) Upload(c *gin.Context) {
userID, ok := GetUserIDFromContext(c)
if !ok {
return
}
// 解析multipart表单
if err := c.Request.ParseMultipartForm(32 << 20); err != nil { // 32MB
RespondBadRequest(c, "解析表单失败", err)
return
}
// 获取文件
file, err := c.FormFile("file")
if err != nil {
RespondBadRequest(c, "获取文件失败", err)
return
}
// 读取文件内容
src, err := file.Open()
if err != nil {
RespondBadRequest(c, "打开文件失败", err)
return
}
defer src.Close()
fileData := make([]byte, file.Size)
if _, err := src.Read(fileData); err != nil {
RespondBadRequest(c, "读取文件失败", err)
return
}
// 获取表单字段
name := c.PostForm("name")
if name == "" {
RespondBadRequest(c, "名称不能为空", nil)
return
}
description := c.PostForm("description")
textureType := c.PostForm("type")
if textureType == "" {
textureType = "SKIN" // 默认值
}
isPublic := c.PostForm("is_public") == "true"
isSlim := c.PostForm("is_slim") == "true"
// 检查上传限制
maxTextures := h.container.UserService.GetMaxTexturesPerUser()
if err := h.container.TextureService.CheckUploadLimit(c.Request.Context(), userID, maxTextures); err != nil {
RespondBadRequest(c, err.Error(), nil)
return
}
// 调用服务上传
texture, err := h.container.TextureService.UploadTexture(
c.Request.Context(),
userID,
name,
description,
textureType,
fileData,
file.Filename,
isPublic,
isSlim,
)
if err != nil {
h.logger.Error("上传材质失败",
zap.Int64("user_id", userID),
zap.String("file_name", file.Filename),
zap.Error(err),
)
RespondBadRequest(c, err.Error(), nil)
return
}
RespondSuccess(c, TextureToTextureInfo(texture))
}