aboutsummaryrefslogtreecommitdiff
path: root/src/server/handlers/post.go
blob: 2c014ca115921c189a837ba543829cb85f81716f (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package handlers

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"image"
	_ "image/gif"
	_ "image/jpeg"
	"image/png"
	"strconv"

	"golang.org/x/image/draw"

	"net/http"
	"server/database"
	"server/helper"
)

const thumbnail_size = 128

func makeThumbnail(source image.Image) ([]byte, error) {
	dest := image.NewRGBA(image.Rect(0, 0, thumbnail_size, thumbnail_size))
	draw.NearestNeighbor.Scale(dest, dest.Rect, source, source.Bounds(), draw.Over, nil)

	var buffer bytes.Buffer
	err := png.Encode(&buffer, dest)
	if err != nil {
		return nil, err
	}
	return buffer.Bytes(), nil
}

type postRequest struct {
	Title    string
	Contents string
	Subreact string
	File     string
}

type postResponse struct {
	Uid int `json:"uid"`
}

func Post(writer http.ResponseWriter, request *http.Request) {
	if request.Method != "POST" {
		helper.WriteErrorJson("expected GET method", writer, http.StatusBadRequest)
		return
	}

	claims := helper.GetValidClaims(writer, request)
	if claims == nil {
		return
	}
	user_uid, err := strconv.Atoi(claims.Subject) // TODO UID
	if err != nil {
		helper.WriteInternalErrorJson(err, writer)
		return
	}

	var post_request postRequest
	err = json.NewDecoder(request.Body).Decode(&post_request)
	if err != nil {
		helper.WriteErrorJson(err.Error(), writer, http.StatusBadRequest)
		return
	}

	post_request.Title = helper.CleanTitle(post_request.Title)
	if !helper.IsValidRange(post_request.Title, "title", 8, 128, writer) {
		return
	}

	post_request.Contents = helper.CleanContents(post_request.Contents)
	if !helper.IsValidRange(post_request.Contents, "contents", 8, 2048, writer) {
		return
	}

	if !helper.IsValidSubreact(post_request.Subreact, writer) {
		return
	}

	image_blob, err := base64.StdEncoding.DecodeString(post_request.File)
	if err != nil {
		helper.WriteErrorJson("failed to decode image from base64", writer, http.StatusBadRequest)
		return
	}
	image, format, err := image.Decode(bytes.NewReader(image_blob))
	if err != nil {
		helper.WriteErrorJson("failed to decode image", writer, http.StatusBadRequest)
		return
	}
	if !helper.IsImageSupported(image, format, writer) {
		return
	}

	thumbnail_blob, err := makeThumbnail(image)
	if err != nil {
		helper.WriteInternalErrorJson(err, writer)
		return
	}

	image_uid, err := database.WritePost(user_uid,
		nil,
		post_request.Title,
		post_request.Contents,
		post_request.Subreact,
		image_blob,
		thumbnail_blob)
	if err != nil {
		helper.WriteInternalErrorJson(err, writer)
		return
	}

	resp, err := json.Marshal(postResponse{Uid: image_uid})
	if err != nil {
		helper.WriteInternalErrorJson(err, writer)
		return
	}

	writer.Write(resp)
}