Files
go-123pan-pic/internal/handler/upload.go
RainySY 7229dfa1b7 refactor: decouple services from global config and clean up structure
- Remove duplicate UploadBaseURL constant (identical to APIBaseURL)
- Pass parentFileID and customDomain into service constructors instead of
  reading from config.GlobalConfig at call time, eliminating hidden global
  state dependencies in the service layer
- Replace []interface{} response building in HandleList with a typed
  imageResponse struct for compile-time safety
- Extract inline CORS closure from main.go into handler.CORSMiddleware(),
  consistent with how AuthMiddleware is organized
- Remove narrating comments throughout; keep only the non-obvious one
  explaining why DoRawPUT omits auth headers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 14:48:11 +08:00

62 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"log"
"net/http"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"imagehost/internal/service"
)
type UploadHandler struct {
uploadSvc *service.UploadService
}
func NewUploadHandler(svc *service.UploadService) *UploadHandler {
return &UploadHandler{uploadSvc: svc}
}
func (h *UploadHandler) HandleUpload(c *gin.Context) {
fileHeader, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无法收到图片文件: " + err.Error()})
return
}
ext := strings.ToLower(filepath.Ext(fileHeader.Filename))
validExts := map[string]bool{
".jpg": true, ".jpeg": true, ".png": true,
".gif": true, ".webp": true, ".svg": true, ".bmp": true,
}
if !validExts[ext] {
c.JSON(http.StatusUnsupportedMediaType, gin.H{
"code": 415,
"message": "仅支持上传图片格式jpg/png/gif/webp/svg/bmp。",
})
return
}
if !strings.HasPrefix(fileHeader.Header.Get("Content-Type"), "image/") {
c.JSON(http.StatusUnsupportedMediaType, gin.H{"code": 415, "message": "不支持的 Content-Type 类型。"})
return
}
log.Printf("[上传] 接收文件: %s", fileHeader.Filename)
fileInfo, err := h.uploadSvc.UploadFile(fileHeader)
if err != nil {
log.Printf("[上传] 失败: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": fileInfo,
"url": fileInfo.UserSelfURL,
})
}