From f81e15f1b8232bd43a35be76f7a338d7babd3b72 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 10 May 2023 01:19:06 +0300 Subject: [PATCH] Add cat lib with route --- libs/cat.go | 57 ++++++++++++++++++++++++++++++++++ libs/libs.go | 6 ++-- main.go | 6 ++++ webfilesystem/webfilesystem.go | 5 +++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 libs/cat.go diff --git a/libs/cat.go b/libs/cat.go new file mode 100644 index 0000000..5c0a64c --- /dev/null +++ b/libs/cat.go @@ -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) + }) + +} diff --git a/libs/libs.go b/libs/libs.go index f916e33..e7e94ba 100644 --- a/libs/libs.go +++ b/libs/libs.go @@ -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), } } diff --git a/main.go b/main.go index 335d137..88264bc 100644 --- a/main.go +++ b/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) diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index e93f8fa..3c702c6 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -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{}