teldrive/pkg/controller/file.go

205 lines
4.2 KiB
Go
Raw Normal View History

2023-12-03 03:47:23 +08:00
package controller
import (
"net/http"
2024-06-22 20:29:59 +08:00
"github.com/divyam234/teldrive/internal/auth"
2023-12-03 03:47:23 +08:00
"github.com/divyam234/teldrive/pkg/httputil"
"github.com/divyam234/teldrive/pkg/schemas"
2023-12-03 03:47:23 +08:00
"github.com/gin-gonic/gin"
)
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{
Limit: 500,
Page: 1,
Order: "asc",
Sort: "name",
Op: "list",
}
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)
}
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-06-22 20:29:59 +08:00
fc.FileService.GetFileStream(c, false)
}
func (fc *Controller) GetFileDownload(c *gin.Context) {
fc.FileService.GetFileStream(c, true)
2023-12-03 03:47:23 +08:00
}