personal-website/webfilesystem/routes.go

256 lines
5.8 KiB
Go
Raw Normal View History

2023-05-05 14:44:03 +00:00
package webfilesystem
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) {
2023-05-16 12:12:55 +00:00
fs.PublicRoutes(route)
2023-05-05 14:44:03 +00:00
route.POST("/upload", func(ctx *gin.Context) { //TODO To PUT request
// fileName := ctx.Query("fileName")
// if fileName == "" {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
2023-05-07 00:14:30 +00:00
parentPath := ctx.Query("parentPath")
if parentPath == "" {
2023-05-05 14:44:03 +00:00
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
// single file
file, _ := ctx.FormFile("file")
if file == nil {
ctx.String(http.StatusBadRequest, "file is nil")
return
}
// generateMins := c.Param("generateMins")
// log.Println(file.Filename)
// Upload the file to specific dst.
dst := "./test-img/" + file.Filename
ctx.SaveUploadedFile(file, dst)
//TODO: Not Save to disk
2023-05-07 00:14:30 +00:00
err := fs.UploadFile(dst, parentPath)
2023-05-05 14:44:03 +00:00
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
}
// webFsCollection.CreateMiniatures("./test-img/", file.Filename)
// webfs.CreateFile(&img, "/home/user/")
ctx.Status(http.StatusCreated)
})
2023-05-11 01:45:57 +00:00
//TODO Support links
route.POST("/createlink", func(ctx *gin.Context) { //TODO To PUT request
sourcePath := ctx.Query("sourcePath")
if sourcePath == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
targetPath := ctx.Query("targetPath")
if sourcePath == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
err := fs.CreateObjectLink(sourcePath, targetPath)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error()) //TODO
return
}
ctx.Status(http.StatusCreated)
})
2023-05-05 14:44:03 +00:00
// route.GET("writeFile", func(ctx *gin.Context) {
// parentPath := ctx.Query("parentPath")
// if parentPath == "" {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
// file := WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: "pp",
// Type: "test",
// }
// err := fs.NewCreateFile(&file, parentPath)
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
// return
// }
// ctx.JSON(http.StatusOK, "OK")
// })
2023-05-07 00:14:30 +00:00
route.GET("init", func(ctx *gin.Context) {
2023-05-07 01:00:22 +00:00
err := fs.InitFS()
2023-05-07 00:14:30 +00:00
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "Inited")
})
2023-05-05 14:44:03 +00:00
route.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
2023-05-07 01:00:22 +00:00
_, _, err := fs.CreateDirectory(path)
2023-05-05 14:44:03 +00:00
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "OK")
})
route.GET("list", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
2023-05-07 01:00:22 +00:00
files, err := fs.ListDir(path)
2023-05-05 14:44:03 +00:00
if err != nil {
2023-05-07 00:14:30 +00:00
ctx.String(http.StatusInternalServerError, err.Error()) //TODO json error struct
2023-05-05 14:44:03 +00:00
return
}
ctx.JSON(http.StatusOK, &files)
})
2023-05-09 03:17:34 +00:00
route.GET("read", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
2023-05-05 14:44:03 +00:00
2023-05-09 03:17:34 +00:00
file, err := fs.Read(path, nil)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
2023-05-05 14:44:03 +00:00
2023-05-09 03:17:34 +00:00
ctx.JSON(http.StatusOK, &file)
})
2023-05-05 14:44:03 +00:00
route.GET("validate", func(ctx *gin.Context) {
2023-05-10 12:39:49 +00:00
err := fs.validate()
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
route.GET("move", func(ctx *gin.Context) {
sourcePath := ctx.Query("sourcePath")
if sourcePath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
targetPath := ctx.Query("targetPath")
if targetPath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
err := fs.Move(sourcePath, targetPath)
2023-05-05 14:44:03 +00:00
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
route.GET("delete", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
2023-05-07 01:00:22 +00:00
err := fs.Remove(path)
2023-05-05 14:44:03 +00:00
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
2023-05-16 12:12:55 +00:00
route.GET("readObjectLink", func(ctx *gin.Context) {
linkPath := ctx.Query("linkPath")
if linkPath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
// file, err := fs.Read(linkPath, nil)
ctx.Status(http.StatusOK)
})
route.GET("createPathLink", func(ctx *gin.Context) {
sourcePath := ctx.Query("sourcePath")
if sourcePath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
linkPath := ctx.Query("linkPath")
if linkPath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
_, _, err := fs.CreatePathLink(sourcePath, linkPath)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
2023-05-05 14:44:03 +00:00
}
func (fs *WebFileSystem) PublicRoutes(route *gin.RouterGroup) {
2023-05-16 12:12:55 +00:00
route.GET("readPathLink", func(ctx *gin.Context) {
linkPath := ctx.Query("linkPath")
if linkPath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
linkData := PathLinkFileData{}
linkHeader, err := fs.Read(linkPath, &linkData)
if err != nil {
ctx.Status(http.StatusBadRequest) //TODO
return
}
if linkHeader.Type != "pathlink" {
ctx.Status(http.StatusBadRequest)
return
}
2023-05-16 12:12:55 +00:00
FileHeader, err := fs.Read(linkData.Path, nil)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.JSON(http.StatusOK, FrontEndFile{
Name: FileHeader.Name,
Type: FileHeader.Type,
ParentPath: fs.GetParentPath(linkData.Path),
})
})
}