aboutsummaryrefslogtreecommitdiff
path: root/controllers/post.go
blob: d6698e3795dc3fac57a9407eafdb7523d4cf6263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package controllers

import (
	"log"
	"paperchan.club/database"
	"paperchan.club/themagicpipe"
	"database/sql"
	"github.com/gofiber/fiber/v2"
	"strings"
	"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()+strings.Join(c.IPs()," ")
	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",
		})
	}
}