personal-website/webfilesystem/routes.go

260 lines
6.0 KiB
Go

package webfilesystem
import (
"net/http"
"path"
"personalwebsite/errormessage"
"github.com/gin-gonic/gin"
)
func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) {
fs.PublicRoutes(route)
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
// }
parentPath := ctx.Query("parentPath")
if parentPath == "" {
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
err := fs.UploadFile(dst, path.Join(parentPath, file.Filename))
if err != nil {
ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{
Message: err.Error(),
})
return
}
// webFsCollection.CreateMiniatures("./test-img/", file.Filename)
// webfs.CreateFile(&img, "/home/user/")
ctx.Status(http.StatusCreated)
})
//TODO Support Object 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)
// })
// 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")
// })
route.GET("init", func(ctx *gin.Context) {
err := fs.InitFS()
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "Inited")
})
route.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
_, _, err := fs.CreateDirectory(path)
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
}
files, err := fs.ListDir(path)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
ctx.JSON(http.StatusOK, &files)
})
route.GET("read", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := fs.Read(path, nil)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
ctx.JSON(http.StatusOK, &file)
})
route.GET("validate", func(ctx *gin.Context) {
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)
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
}
err := fs.Remove(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
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)
})
}
func (fs *WebFileSystem) PublicRoutes(route *gin.RouterGroup) {
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
}
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),
})
})
}