feat: 新增文件上传服务

- 添加 UploadHandler 处理文件上传
- 添加 UploadService 实现文件存储
- 配置上传文件大小限制和存储路径

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 16:54:41 +08:00
parent b715b8e859
commit 4d4a756f4f
7 changed files with 343 additions and 11 deletions

View File

@@ -0,0 +1,60 @@
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"x-agents/server/internal/service"
)
type UploadHandler struct {
uploadService *service.UploadService
}
func NewUploadHandler(uploadService *service.UploadService) *UploadHandler {
return &UploadHandler{uploadService: uploadService}
}
// Upload 上传文件
func (h *UploadHandler) Upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "No file uploaded"})
return
}
// 检查文件大小(最大 100MB
if file.Size > 100*1024*1024 {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "File too large (max 100MB)"})
return
}
result, err := h.uploadService.Upload(file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
return
}
if !result.Success {
c.JSON(http.StatusInternalServerError, result)
return
}
c.JSON(http.StatusOK, result)
}
// Delete 删除文件
func (h *UploadHandler) Delete(c *gin.Context) {
filename := c.Param("filename")
if filename == "" {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "Filename is required"})
return
}
if err := h.uploadService.DeleteFile(filename); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "message": "File deleted"})
}