150 lines
3.4 KiB
Go
150 lines
3.4 KiB
Go
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
|
|
// }
|
|
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, parentPath)
|
|
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")
|
|
// })
|
|
|
|
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.NewReadDeprecated(path)
|
|
// 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
|
|
}
|
|
err := fs.Remove(path)
|
|
if err != nil {
|
|
ctx.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusOK)
|
|
})
|
|
}
|