The Go module name has been changed from `carrot_bbs` to `with_you`, which requires all import paths to be updated throughout the codebase and by external consumers. BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports and dependencies must be updated from carrot_bbs/* to with_you/*.
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"with_you/internal/pkg/response"
|
|
"with_you/internal/service"
|
|
)
|
|
|
|
// UploadHandler 上传处理器
|
|
type UploadHandler struct {
|
|
uploadService *service.UploadService
|
|
}
|
|
|
|
// NewUploadHandler 创建上传处理器
|
|
func NewUploadHandler(uploadService *service.UploadService) *UploadHandler {
|
|
return &UploadHandler{
|
|
uploadService: uploadService,
|
|
}
|
|
}
|
|
|
|
// UploadImage 上传图片
|
|
func (h *UploadHandler) UploadImage(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
file, err := c.FormFile("image")
|
|
if err != nil {
|
|
response.BadRequest(c, "image file is required")
|
|
return
|
|
}
|
|
|
|
url, previewURL, previewURLLarge, err := h.uploadService.UploadImage(c.Request.Context(), file)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to upload image")
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{
|
|
"url": url,
|
|
"preview_url": previewURL,
|
|
"preview_url_large": previewURLLarge,
|
|
})
|
|
}
|
|
|
|
// UploadAvatar 上传头像
|
|
func (h *UploadHandler) UploadAvatar(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
file, err := c.FormFile("image")
|
|
|
|
if err != nil {
|
|
response.BadRequest(c, "avatar file is required")
|
|
return
|
|
}
|
|
|
|
url, err := h.uploadService.UploadAvatar(c.Request.Context(), userID, file)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to upload avatar")
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{"url": url})
|
|
}
|
|
|
|
// UploadCover 上传头图(个人主页封面)
|
|
func (h *UploadHandler) UploadCover(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
file, err := c.FormFile("image")
|
|
if err != nil {
|
|
response.BadRequest(c, "image file is required")
|
|
return
|
|
}
|
|
|
|
url, err := h.uploadService.UploadCover(c.Request.Context(), userID, file)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to upload cover")
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{"url": url})
|
|
}
|