83 lines
1.7 KiB
Go
83 lines
1.7 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) CreateBatch(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in uploadlogic.BatchCreateInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.files.CreateCredentials(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
|
|
}
|