teldrive/routes/file.go

106 lines
1.8 KiB
Go
Raw Normal View History

2023-08-07 03:32:46 +08:00
package routes
import (
"net/http"
2023-08-16 05:53:02 +08:00
"github.com/divyam234/teldrive/database"
"github.com/divyam234/teldrive/services"
2023-08-07 03:32:46 +08:00
"github.com/gin-gonic/gin"
)
func addFileRoutes(rg *gin.RouterGroup) {
r := rg.Group("/files")
2023-09-20 03:20:44 +08:00
fileService := services.FileService{Db: database.DB}
2023-08-07 03:32:46 +08:00
2023-09-20 03:20:44 +08:00
r.GET("", Authmiddleware, func(c *gin.Context) {
2023-08-07 03:32:46 +08:00
res, err := fileService.ListFiles(c)
if err != nil {
c.AbortWithError(err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
})
2023-09-20 03:20:44 +08:00
r.POST("", Authmiddleware, func(c *gin.Context) {
2023-08-07 03:32:46 +08:00
res, err := fileService.CreateFile(c)
if err != nil {
c.AbortWithError(err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
})
2023-09-20 03:20:44 +08:00
r.GET("/:fileID", Authmiddleware, func(c *gin.Context) {
2023-08-07 03:32:46 +08:00
res, err := fileService.GetFileByID(c)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.JSON(http.StatusOK, res)
})
2023-09-20 03:20:44 +08:00
r.PATCH("/:fileID", Authmiddleware, func(c *gin.Context) {
2023-08-07 03:32:46 +08:00
res, err := fileService.UpdateFile(c)
if err != nil {
c.AbortWithError(err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
})
r.GET("/:fileID/:fileName", func(c *gin.Context) {
2023-08-14 04:58:06 +08:00
fileService.GetFileStream(c)
})
2023-09-20 03:20:44 +08:00
r.POST("/movefiles", Authmiddleware, func(c *gin.Context) {
2023-08-14 04:58:06 +08:00
res, err := fileService.MoveFiles(c)
if err != nil {
c.AbortWithError(err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
})
2023-09-20 03:20:44 +08:00
r.POST("/makedir", Authmiddleware, func(c *gin.Context) {
res, err := fileService.MakeDirectory(c)
if err != nil {
c.AbortWithError(err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
})
2023-09-20 03:20:44 +08:00
r.POST("/deletefiles", Authmiddleware, func(c *gin.Context) {
2023-08-14 04:58:06 +08:00
res, err := fileService.DeleteFiles(c)
if err != nil {
c.AbortWithError(err.Code, err.Error)
return
}
c.JSON(http.StatusOK, res)
2023-08-07 03:32:46 +08:00
})
}