Files
mygo/internal/handler/file.go
T
ld b988b4b15e fix(file)!: stream uploads through staged storage
- fix: replace multipart form parsing with streaming multipart reads and
  apply request body limits when max_upload_size is configured.
- refactor: route uploads through staging paths before promotion to
  long-term data paths, keeping incomplete uploads out of durable
  storage records.
- test: cover oversized uploads, unlimited uploads, staged cleanup, and
  local storage promotion boundaries.
- docs: document the staged upload model and multipart parent_id query
  parameter.
2026-07-05 17:18:19 +08:00

268 lines
6.6 KiB
Go

package handler
import (
"errors"
"fmt"
"io"
"log/slog"
"mime"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"github.com/gin-gonic/gin"
"github.com/dhao2001/mygo/internal/api"
"github.com/dhao2001/mygo/internal/middleware"
"github.com/dhao2001/mygo/internal/service"
)
const (
jsonUploadBodyLimit = 64 << 10
multipartUploadOverhead = 1 << 20
multipartFileField = "file"
)
// FileHandler handles file management endpoints.
type FileHandler struct {
fileService *service.FileService
}
// NewFileHandler creates a FileHandler.
func NewFileHandler(fileService *service.FileService) *FileHandler {
return &FileHandler{fileService: fileService}
}
type createDirRequest struct {
Name string `json:"name" binding:"required"`
ParentID *string `json:"parent_id"`
}
type updateFileRequest struct {
Name string `json:"name"`
ParentID *string `json:"parent_id"`
}
// Upload handles POST /api/v1/files.
// If the content type is multipart/form-data, it uploads a file.
// If the content type is application/json, it creates a directory.
func (h *FileHandler) Upload(c *gin.Context) {
userID := middleware.MustGetUserID(c)
contentType := c.GetHeader("Content-Type")
// Directory creation via JSON.
mediaType, _, _ := mime.ParseMediaType(contentType)
if mediaType == "application/json" {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, jsonUploadBodyLimit)
var req createDirRequest
if err := c.ShouldBindJSON(&req); err != nil {
slog.DebugContext(c.Request.Context(), "create dir bind failed", "error", err)
if isRequestBodyTooLarge(err) {
api.Error(c, http.StatusRequestEntityTooLarge, "request body is too large")
return
}
api.Error(c, http.StatusBadRequest, "invalid request body")
return
}
dir, err := h.fileService.CreateDir(c.Request.Context(), userID, req.ParentID, req.Name)
if err != nil {
api.RespondError(c, err)
return
}
c.JSON(http.StatusCreated, dir)
return
}
// File upload via multipart.
if mediaType != "multipart/form-data" {
api.Error(c, http.StatusBadRequest, "unsupported content type")
return
}
if maxUploadSize := h.fileService.MaxUploadSize(); maxUploadSize > 0 {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxUploadSize+multipartUploadOverhead)
}
var parentID *string
parentIDStr := c.Query("parent_id")
if parentIDStr != "" {
parentID = &parentIDStr
}
reader, err := c.Request.MultipartReader()
if err != nil {
slog.DebugContext(c.Request.Context(), "create multipart reader failed", "error", err)
if isRequestBodyTooLarge(err) {
api.Error(c, http.StatusRequestEntityTooLarge, "request body is too large")
return
}
api.Error(c, http.StatusBadRequest, "invalid multipart form")
return
}
part, fileName, err := nextUploadFilePart(reader)
if err != nil {
slog.DebugContext(c.Request.Context(), "read multipart file part failed", "error", err)
if isRequestBodyTooLarge(err) {
api.Error(c, http.StatusRequestEntityTooLarge, "request body is too large")
return
}
api.Error(c, http.StatusBadRequest, err.Error())
return
}
defer part.Close()
info, err := h.fileService.Upload(c.Request.Context(), userID, parentID, fileName, part)
if err != nil {
api.RespondError(c, err)
return
}
c.JSON(http.StatusCreated, info)
}
func nextUploadFilePart(reader *multipart.Reader) (*multipart.Part, string, error) {
part, err := reader.NextPart()
if err != nil {
if err == io.EOF {
return nil, "", errors.New("missing file field")
}
return nil, "", err
}
if part.FormName() != multipartFileField {
part.Close()
return nil, "", fmt.Errorf("unexpected multipart field %q", part.FormName())
}
fileName := part.FileName()
if fileName == "" {
part.Close()
return nil, "", errors.New("missing file name")
}
return part, fileName, nil
}
func isRequestBodyTooLarge(err error) bool {
var maxBytesErr *http.MaxBytesError
return errors.As(err, &maxBytesErr)
}
// List handles GET /api/v1/files.
func (h *FileHandler) List(c *gin.Context) {
userID := middleware.MustGetUserID(c)
parentIDStr := c.Query("parent_id")
var parentID *string
if parentIDStr != "" {
parentID = &parentIDStr
}
offset, err := strconv.Atoi(c.DefaultQuery("offset", "0"))
if err != nil || offset < 0 {
api.Error(c, http.StatusBadRequest, "invalid offset")
return
}
limit, err := strconv.Atoi(c.DefaultQuery("limit", "50"))
if err != nil || limit < 1 {
api.Error(c, http.StatusBadRequest, "invalid limit")
return
}
if limit > 200 {
limit = 200
}
list, err := h.fileService.List(c.Request.Context(), userID, parentID, offset, limit)
if err != nil {
api.RespondError(c, err)
return
}
c.JSON(http.StatusOK, list)
}
// Get handles GET /api/v1/files/:id.
func (h *FileHandler) Get(c *gin.Context) {
userID := middleware.MustGetUserID(c)
fileID := c.Param("id")
info, err := h.fileService.Get(c.Request.Context(), userID, fileID)
if err != nil {
api.RespondError(c, err)
return
}
c.JSON(http.StatusOK, info)
}
// Download handles GET /api/v1/files/:id/content.
func (h *FileHandler) Download(c *gin.Context) {
userID := middleware.MustGetUserID(c)
fileID := c.Param("id")
reader, info, err := h.fileService.Download(c.Request.Context(), userID, fileID)
if err != nil {
api.RespondError(c, err)
return
}
defer reader.Close()
mimeType := info.MimeType
if mimeType == "" {
mimeType = "application/octet-stream"
}
c.Header("Content-Type", mimeType)
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", url.PathEscape(info.Name)))
c.Header("Content-Length", strconv.FormatInt(info.Size, 10))
c.Status(http.StatusOK)
io.Copy(c.Writer, reader)
}
// Update handles PUT /api/v1/files/:id.
func (h *FileHandler) Update(c *gin.Context) {
userID := middleware.MustGetUserID(c)
fileID := c.Param("id")
var req updateFileRequest
if err := c.ShouldBindJSON(&req); err != nil {
slog.DebugContext(c.Request.Context(), "update file bind failed", "error", err)
api.Error(c, http.StatusBadRequest, "invalid request body")
return
}
// Validate at least one field is provided.
if req.Name == "" && req.ParentID == nil {
api.Error(c, http.StatusBadRequest, "at least one of name or parent_id must be provided")
return
}
info, err := h.fileService.Update(c.Request.Context(), userID, fileID, req.Name, req.ParentID)
if err != nil {
api.RespondError(c, err)
return
}
c.JSON(http.StatusOK, info)
}
// Delete handles DELETE /api/v1/files/:id.
func (h *FileHandler) Delete(c *gin.Context) {
userID := middleware.MustGetUserID(c)
fileID := c.Param("id")
if err := h.fileService.Delete(c.Request.Context(), userID, fileID); err != nil {
api.RespondError(c, err)
return
}
c.Status(http.StatusNoContent)
}