76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"nl-pms-api/internal/commonservice"
|
|
"nl-pms-api/internal/service"
|
|
)
|
|
|
|
// FileController 文件上传/列表/删除。
|
|
type FileController struct {
|
|
Svc *service.FileService
|
|
}
|
|
|
|
func (h *FileController) Upload(c *gin.Context) {
|
|
fh, err := c.FormFile("file")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "FILE_REQUIRED"})
|
|
return
|
|
}
|
|
f, err := fh.Open()
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "FILE_READ_FAILED"})
|
|
return
|
|
}
|
|
defer f.Close()
|
|
out, err := h.Svc.Upload(
|
|
commonservice.UserID(c),
|
|
commonservice.ParseID(c.PostForm("teamId")),
|
|
c.PostForm("kind"),
|
|
fh.Filename,
|
|
f,
|
|
fh.Size,
|
|
requestHost(c),
|
|
)
|
|
if err != nil {
|
|
writeErr(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, out)
|
|
}
|
|
|
|
func (h *FileController) List(c *gin.Context) {
|
|
total, items, err := h.Svc.List(
|
|
commonservice.UserID(c),
|
|
c.Query("scope"),
|
|
commonservice.ParseID(c.Query("teamId")),
|
|
commonservice.ParseID(c.Query("page")),
|
|
commonservice.ParseID(c.Query("pageSize")),
|
|
requestHost(c),
|
|
)
|
|
if err != nil {
|
|
writeErr(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"total": total, "items": items})
|
|
}
|
|
|
|
func (h *FileController) Delete(c *gin.Context) {
|
|
if err := h.Svc.Delete(commonservice.UserID(c), commonservice.ParseID(c.Param("id"))); err != nil {
|
|
writeErr(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
|
|
func requestHost(c *gin.Context) string {
|
|
scheme := "http"
|
|
if c.Request.TLS != nil {
|
|
scheme = "https"
|
|
}
|
|
return scheme + "://" + c.Request.Host
|
|
}
|