spark-server/internal/transport/http/handler/file/handler.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

65 lines
1.3 KiB
Go

package filehandler
import (
"github.com/gin-gonic/gin"
uploadlogic "spark-server/internal/module/upload/logic"
"spark-server/internal/pkg/errs"
"spark-server/internal/pkg/reqctx"
"spark-server/internal/transport/http/respond"
)
// Handler is the files HTTP controller.
type Handler struct {
files *uploadlogic.Service
}
func New(files *uploadlogic.Service) *Handler {
return &Handler{files: files}
}
func (h *Handler) Create(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
var in uploadlogic.CreateInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.files.CreateCredential(c.Request.Context(), uid, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Download(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
var in uploadlogic.DownloadInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.files.CreateDownload(c.Request.Context(), uid, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func currentUID(c *gin.Context) (int64, bool) {
rc := reqctx.From(c.Request.Context())
if rc == nil || rc.UserID <= 0 {
respond.JSONError(c, errs.ErrUnauthorized)
return 0, false
}
return rc.UserID, true
}