teldrive/pkg/controller/file.go

270 lines
5.6 KiB
Go
Raw Normal View History

2023-12-03 03:47:23 +08:00
package controller
import (
"net/http"
"github.com/gin-gonic/gin"
2024-08-31 23:50:17 +08:00
"github.com/tgdrive/teldrive/internal/auth"
"github.com/tgdrive/teldrive/pkg/httputil"
"github.com/tgdrive/teldrive/pkg/schemas"
2023-12-03 03:47:23 +08:00
)
func (fc *Controller) CreateFile(c *gin.Context) {
var fileIn schemas.FileIn
if err := c.ShouldBindJSON(&fileIn); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
res, err := fc.FileService.CreateFile(c, userId, &fileIn)
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusCreated, res)
}
func (fc *Controller) UpdateFile(c *gin.Context) {
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
var fileUpdate schemas.FileUpdate
if err := c.ShouldBindJSON(&fileUpdate); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
2024-07-26 23:50:26 +08:00
res, err := fc.FileService.UpdateFile(c.Param("fileID"), userId, &fileUpdate)
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
func (fc *Controller) GetFileByID(c *gin.Context) {
res, err := fc.FileService.GetFileByID(c.Param("fileID"))
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, http.StatusNotFound, err.Error)
2023-12-03 03:47:23 +08:00
return
}
c.JSON(http.StatusOK, res)
}
func (fc *Controller) ListFiles(c *gin.Context) {
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
fquery := schemas.FileQuery{
2024-12-13 21:40:33 +08:00
Limit: 500,
Page: 1,
Order: "asc",
Sort: "name",
Op: "list",
SearchType: "text",
}
if err := c.ShouldBindQuery(&fquery); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
res, err := fc.FileService.ListFiles(userId, &fquery)
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
func (fc *Controller) MakeDirectory(c *gin.Context) {
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
var payload schemas.MkDir
if err := c.ShouldBindJSON(&payload); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
res, err := fc.FileService.MakeDirectory(userId, &payload)
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
func (fc *Controller) CopyFile(c *gin.Context) {
res, err := fc.FileService.CopyFile(c)
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
func (fc *Controller) MoveFiles(c *gin.Context) {
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
var payload schemas.FileOperation
if err := c.ShouldBindJSON(&payload); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
res, err := fc.FileService.MoveFiles(userId, &payload)
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
func (fc *Controller) DeleteFiles(c *gin.Context) {
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
2024-06-11 21:59:05 +08:00
var payload schemas.DeleteOperation
if err := c.ShouldBindJSON(&payload); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
res, err := fc.FileService.DeleteFiles(userId, &payload)
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
2024-09-15 02:00:32 +08:00
func (fc *Controller) CreateShare(c *gin.Context) {
userId, _ := auth.GetUser(c)
var payload schemas.FileShareIn
if err := c.ShouldBindJSON(&payload); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
err := fc.FileService.CreateShare(c.Param("fileID"), userId, &payload)
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.Status(http.StatusCreated)
}
func (fc *Controller) EditShare(c *gin.Context) {
userId, _ := auth.GetUser(c)
var payload schemas.FileShareIn
if err := c.ShouldBindJSON(&payload); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
err := fc.FileService.UpdateShare(c.Param("shareID"), userId, &payload)
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.Status(http.StatusOK)
}
func (fc *Controller) DeleteShare(c *gin.Context) {
userId, _ := auth.GetUser(c)
err := fc.FileService.DeleteShare(c.Param("fileID"), userId)
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.Status(http.StatusNoContent)
}
func (fc *Controller) GetShareByFileId(c *gin.Context) {
userId, _ := auth.GetUser(c)
res, err := fc.FileService.GetShareByFileId(c.Param("fileID"), userId)
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
func (fc *Controller) UpdateParts(c *gin.Context) {
2024-07-26 23:50:26 +08:00
userId, _ := auth.GetUser(c)
var payload schemas.PartUpdate
if err := c.ShouldBindJSON(&payload); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
2024-07-26 23:50:26 +08:00
res, err := fc.FileService.UpdateParts(c, c.Param("fileID"), userId, &payload)
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
2023-12-03 03:47:23 +08:00
func (fc *Controller) MoveDirectory(c *gin.Context) {
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
var payload schemas.DirMove
if err := c.ShouldBindJSON(&payload); err != nil {
httputil.NewError(c, http.StatusBadRequest, err)
return
}
res, err := fc.FileService.MoveDirectory(userId, &payload)
2023-12-03 03:47:23 +08:00
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
2024-04-19 04:46:47 +08:00
func (fc *Controller) GetCategoryStats(c *gin.Context) {
2024-06-22 20:29:59 +08:00
userId, _ := auth.GetUser(c)
2024-04-19 04:46:47 +08:00
res, err := fc.FileService.GetCategoryStats(userId)
if err != nil {
httputil.NewError(c, err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
}
2023-12-03 03:47:23 +08:00
func (fc *Controller) GetFileStream(c *gin.Context) {
2024-09-15 02:00:32 +08:00
fc.FileService.GetFileStream(c, false, nil)
2024-06-22 20:29:59 +08:00
}
func (fc *Controller) GetFileDownload(c *gin.Context) {
2024-09-15 02:00:32 +08:00
fc.FileService.GetFileStream(c, true, nil)
2023-12-03 03:47:23 +08:00
}