Add cat lib with route

This commit is contained in:
cyber-dream 2023-05-10 01:19:06 +03:00
parent da8af8222d
commit f81e15f1b8
4 changed files with 72 additions and 2 deletions

57
libs/cat.go Normal file
View 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)
})
}

View File

@ -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),
}
}

View File

@ -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)

View File

@ -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{}