personal-website/webfilesystem/routes.go

150 lines
3.4 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) Route(route *gin.RouterGroup) {
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)
})
// 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) {
err := fs.Validate()
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)
})
}