From 2fe3b4972f02be8b0c6143325d541ddda4b91559 Mon Sep 17 00:00:00 2001 From: vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> Date: Wed, 19 Jun 2024 17:44:29 +0200 Subject: Initial version --- controllers/post.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 controllers/post.go (limited to 'controllers/post.go') diff --git a/controllers/post.go b/controllers/post.go new file mode 100644 index 0000000..4ec49c4 --- /dev/null +++ b/controllers/post.go @@ -0,0 +1,51 @@ +package controllers + +import ( + "log" + "paperchan.club/database" + "paperchan.club/themagicpipe" + "database/sql" + "github.com/gofiber/fiber/v2" + "strconv" +) + +// data received by /api/post +type PostApi struct { + Picture string `json:"picture" xml:"picture" form:"picture"` + Thread string `json:"thread" xml:"thread" form:"thread"` +} + +func Publish(c *fiber.Ctx) error { + p := new(PostApi) + if err := c.BodyParser(p); err != nil { + return c.JSON(fiber.Map{ + "status": "error", + }) + } + picture := p.Picture + var thread sql.NullInt32 + if parsed, err := strconv.ParseInt(p.Thread, 10, 32); err != nil { + thread.Valid = false + } else { + thread.Int32 = int32(parsed) + thread.Valid = true + } + ip := c.IP() + fixedPic, err := themagicpipe.DataURLConverter(picture) + if err != nil { + return c.JSON(fiber.Map{ + "status": "error", + }) + } + db := database.DB + if _, err := db.Exec("INSERT INTO \"post\" (picture, ip_address, thread) VALUES ($1, $2, $3)", fixedPic, ip, thread); err == nil { + return c.JSON(fiber.Map{ + "status": "ok", + }) + } else { + log.Println(err) + return c.JSON(fiber.Map{ + "status": "database error", + }) + } +} -- cgit v1.2.3