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) {
err := fs.V3InitFS()
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 00:14:30 +00:00
_, _, err := fs.V3CreateDirectory(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 00:14:30 +00:00
files, err := fs.V3List(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)
})
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-07 00:14:30 +00:00
file, err := fs.NewReadDeprecated(path)
2023-05-05 14:44:03 +00:00
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("delete", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
2023-05-07 00:14:30 +00:00
err := fs.V3Remove(path)
2023-05-05 14:44:03 +00:00
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
}