diff --git a/internal/handler/upload.go b/internal/handler/upload.go new file mode 100644 index 0000000..a9f33ce --- /dev/null +++ b/internal/handler/upload.go @@ -0,0 +1,41 @@ +package handler + +import ( + "gotest/internal/service" + + "github.com/gin-gonic/gin" +) + +// UploadHandler 文件上传处理器 +type UploadHandler struct { + uploadService *service.UploadService +} + +// NewUploadHandler 创建上传处理器 +func NewUploadHandler(uploadService *service.UploadService) *UploadHandler { + return &UploadHandler{uploadService: uploadService} +} + +// Upload 文件上传 POST /api/v1/upload +func (h *UploadHandler) Upload(c *gin.Context) { + file, err := c.FormFile("file") + if err != nil { + FailBadRequest(c, "请选择要上传的文件") + return + } + + src, err := file.Open() + if err != nil { + FailInternal(c, "文件读取失败") + return + } + defer src.Close() + + url, err := h.uploadService.Upload(file.Filename, file.Header.Get("Content-Type"), file.Size, src) + if err != nil { + FailBadRequest(c, err.Error()) + return + } + + Success(c, gin.H{"url": url}) +}