/
create.go
88 lines (76 loc) · 2.48 KB
/
create.go
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
package post
import (
"mime/multipart"
"strconv"
"github.com/allocamelus/allocamelus/internal/pkg/errtools"
"github.com/allocamelus/allocamelus/internal/pkg/fileutil"
"github.com/allocamelus/allocamelus/internal/post"
"github.com/allocamelus/allocamelus/internal/post/media"
"github.com/allocamelus/allocamelus/internal/router/handlers/api/apierr"
"github.com/allocamelus/allocamelus/internal/router/handlers/api/shared"
"github.com/allocamelus/allocamelus/internal/user/session"
"github.com/allocamelus/allocamelus/pkg/fiberutil"
"github.com/allocamelus/allocamelus/pkg/logger"
"github.com/gofiber/fiber/v2"
)
type createRequest struct {
Publish bool `json:"publish" form:"publish"`
Content string `json:"content" form:"content"`
Images []*multipart.FileHeader `form:"images[]"`
ImageAlts []string `form:"imageAlts[]"`
}
// TODO: Rate limiting
// Create post handler
func Create(c *fiber.Ctx) error {
sUser := session.Context(c)
if !sUser.Perms.CanPost() {
return post403(c, errtools.ErrInsufficientPerms.Error())
}
request := new(createRequest)
if err := c.BodyParser(request); err != nil {
return apierr.ErrInvalidRequestParams(c)
}
form, err := c.MultipartForm()
if err != nil {
return apierr.ErrInvalidRequestParams(c)
}
request.Images = form.File["images[]"]
newPost := post.New(sUser.UserID, request.Content, request.Publish)
if err := newPost.ContentValid(); err != nil {
return post403(c, err.Error())
}
if err := newPost.Insert(); logger.Error(err) {
return apierr.ErrSomethingWentWrong(c)
}
imageAltLen := len(request.ImageAlts)
for k, v := range request.Images {
if err := media.ValidateMpFileHeader(v); err != nil {
if err == fileutil.ErrSomethingWentWrong {
return apierr.ErrSomethingWentWrong(c)
}
return apierr.Err422(c, shared.SuccessErrResp{Error: err.Error()})
}
var alt string
if imageAltLen > k {
// Truncate alt to 512
altLen := len(request.ImageAlts[k])
if altLen > 512 {
altLen = 512
}
alt = request.ImageAlts[k][:altLen]
} else {
alt = "Image #" + strconv.Itoa(k+1) + " For Post:" + strconv.Itoa(int(newPost.ID))
}
err := media.TransformAndSave(newPost.ID, v, alt)
if logger.Error(err) {
return apierr.ErrSomethingWentWrong(c)
}
}
return fiberutil.JSON(c, 200, shared.SuccessIDErrResp{
Success: true,
ID: newPost.ID,
})
}
func post403(c *fiber.Ctx, err string) error {
return apierr.Err403(c, shared.SuccessErrResp{Error: err})
}