Files
X-Agents/server/internal/handler/upload_handler.go
DESKTOP-72TV0V4\caoxiaozhu 4d4a756f4f feat: 新增文件上传服务
- 添加 UploadHandler 处理文件上传
- 添加 UploadService 实现文件存储
- 配置上传文件大小限制和存储路径

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 16:54:41 +08:00

61 lines
1.4 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 (
"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"})
}