Add cat lib with route
This commit is contained in:
parent
da8af8222d
commit
f81e15f1b8
57
libs/cat.go
Normal file
57
libs/cat.go
Normal file
@ -0,0 +1,57 @@
|
||||
package libs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"personalwebsite/webfilesystem"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Cat struct {
|
||||
fs *webfilesystem.WebFileSystem
|
||||
}
|
||||
|
||||
func NewCatLib(webfs *webfilesystem.WebFileSystem) *Cat {
|
||||
return &Cat{
|
||||
fs: webfs,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cat) Get(filePath string) (string, error) {
|
||||
file, err := c.fs.Read(filePath, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if file.Type != "plaintext" {
|
||||
return "", errors.New("todo")
|
||||
}
|
||||
|
||||
fileData := webfilesystem.PlainTextFileData{}
|
||||
_, err = c.fs.Read(filePath, &fileData)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fileData.Data, nil
|
||||
}
|
||||
|
||||
func (c *Cat) Route(route *gin.RouterGroup) {
|
||||
route.GET("get", func(ctx *gin.Context) {
|
||||
path := ctx.Query("path")
|
||||
if path == "" {
|
||||
ctx.String(http.StatusBadRequest, "TODO") //TODO json error struct
|
||||
return
|
||||
}
|
||||
|
||||
data, err := c.Get(path)
|
||||
if err != nil {
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.String(http.StatusOK, "plaintext", data)
|
||||
})
|
||||
|
||||
}
|
@ -5,11 +5,13 @@ import (
|
||||
)
|
||||
|
||||
type Libs struct {
|
||||
imglib *ImagLib
|
||||
Imglib *ImagLib
|
||||
Cat *Cat
|
||||
}
|
||||
|
||||
func NewLibs(webfs *webfilesystem.WebFileSystem) Libs {
|
||||
return Libs{
|
||||
imglib: NewImgLib(webfs),
|
||||
Imglib: NewImgLib(webfs),
|
||||
Cat: NewCatLib(webfs),
|
||||
}
|
||||
}
|
||||
|
6
main.go
6
main.go
@ -89,6 +89,12 @@ func main() {
|
||||
imgLib.Route(imgLibGroup)
|
||||
}
|
||||
|
||||
catLibGroup := libsGroup.Group("cat")
|
||||
{
|
||||
catLib := libs.NewCatLib(webfs)
|
||||
catLib.Route(catLibGroup)
|
||||
}
|
||||
|
||||
appsStorageGroup := libsGroup.Group("apps")
|
||||
{
|
||||
appsStorage.Route(appsStorageGroup)
|
||||
|
@ -44,6 +44,11 @@ type BinaryFileData struct {
|
||||
Bin []byte `bson:"bin" json:"-"`
|
||||
}
|
||||
|
||||
type PlainTextFileData struct {
|
||||
MongoId primitive.ObjectID `bson:"_id" json:"-"`
|
||||
Data string `bson:"data" json:"data"`
|
||||
}
|
||||
|
||||
// Deprecated
|
||||
func (fs *WebFileSystem) ReadHeader(fileID primitive.ObjectID) (*FileHeader, error) {
|
||||
file := &FileHeader{}
|
||||
|
Loading…
Reference in New Issue
Block a user