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"
|
2024-02-12 04:52:41 +08:00
|
|
|
"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) {
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
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)
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
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-02-12 04:52:41 +08:00
|
|
|
|
2024-06-22 20:29:59 +08:00
|
|
|
userId, _ := auth.GetUser(c)
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
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) {
|
2024-02-12 04:52:41 +08:00
|
|
|
res, err := fc.FileService.GetFileByID(c.Param("fileID"))
|
2023-12-03 03:47:23 +08:00
|
|
|
if err != nil {
|
2023-12-17 21:05:36 +08:00
|
|
|
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-02-12 04:52:41 +08:00
|
|
|
|
2024-06-22 20:29:59 +08:00
|
|
|
userId, _ := auth.GetUser(c)
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
fquery := schemas.FileQuery{
|
2024-07-15 03:09:09 +08:00
|
|
|
Limit: 500,
|
|
|
|
Page: 1,
|
|
|
|
Order: "asc",
|
|
|
|
Sort: "name",
|
|
|
|
Op: "list",
|
2024-02-12 04:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02-12 04:52:41 +08:00
|
|
|
|
2024-06-22 20:29:59 +08:00
|
|
|
userId, _ := auth.GetUser(c)
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
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-02-12 04:52:41 +08:00
|
|
|
|
2024-06-22 20:29:59 +08:00
|
|
|
userId, _ := auth.GetUser(c)
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
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-02-12 04:52:41 +08:00
|
|
|
|
2024-06-22 20:29:59 +08:00
|
|
|
userId, _ := auth.GetUser(c)
|
2024-02-12 04:52:41 +08:00
|
|
|
|
2024-06-11 21:59:05 +08:00
|
|
|
var payload schemas.DeleteOperation
|
2024-02-12 04:52:41 +08:00
|
|
|
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-07-08 19:12:48 +08:00
|
|
|
func (fc *Controller) UpdateParts(c *gin.Context) {
|
2024-05-26 04:26:05 +08:00
|
|
|
|
2024-07-26 23:50:26 +08:00
|
|
|
userId, _ := auth.GetUser(c)
|
|
|
|
|
2024-07-08 19:12:48 +08:00
|
|
|
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)
|
2024-05-26 04:26:05 +08:00
|
|
|
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)
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|