-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.go
179 lines (141 loc) · 3.31 KB
/
thread.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package models
import (
"errors"
"html"
"github.com/microcosm-cc/bluemonday"
"github.com/eirka/eirka-libs/config"
"github.com/eirka/eirka-libs/db"
e "github.com/eirka/eirka-libs/errors"
"github.com/eirka/eirka-libs/validate"
)
// ThreadModel holds the request input
type ThreadModel struct {
UID uint
Ib uint
IP string
Title string
Comment string
Filename string
Thumbnail string
MD5 string
SHA string
OrigWidth int
OrigHeight int
ThumbWidth int
ThumbHeight int
}
// IsValid will check struct validity
func (m *ThreadModel) IsValid() bool {
if m.UID == 0 {
return false
}
if m.Ib == 0 {
return false
}
if m.IP == "" {
return false
}
if m.Title == "" {
return false
}
if m.Comment == "" {
return false
}
if m.Filename == "" {
return false
}
if m.Thumbnail == "" {
return false
}
if m.MD5 == "" {
return false
}
if m.OrigWidth == 0 {
return false
}
if m.OrigHeight == 0 {
return false
}
if m.ThumbWidth == 0 {
return false
}
if m.ThumbHeight == 0 {
return false
}
return true
}
// ValidateInput will make sure all the parameters are valid
func (m *ThreadModel) ValidateInput() (err error) {
// Initialize bluemonday
p := bluemonday.StrictPolicy()
// sanitize html and xss
m.Title = html.UnescapeString(p.Sanitize(m.Title))
// Validate title input
title := validate.Validate{Input: m.Title, Max: config.Settings.Limits.TitleMaxLength, Min: config.Settings.Limits.TitleMinLength}
if title.IsEmpty() {
return e.ErrNoTitle
} else if title.MinLength() {
return e.ErrTitleShort
} else if title.MaxLength() {
return e.ErrTitleLong
}
// sanitize html and xss
m.Comment = html.UnescapeString(p.Sanitize(m.Comment))
// Validate comment input
comment := validate.Validate{Input: m.Comment, Max: config.Settings.Limits.CommentMaxLength, Min: config.Settings.Limits.CommentMinLength}
if comment.IsEmpty() {
return e.ErrNoComment
} else if comment.MinLength() {
return e.ErrCommentShort
} else if comment.MaxLength() {
return e.ErrCommentLong
}
return
}
// Post will add the thread to the database with a transaction
func (m *ThreadModel) Post() (err error) {
// check model validity
if !m.IsValid() {
return errors.New("ThreadModel is not valid")
}
// Get transaction handle
tx, err := db.GetTransaction()
if err != nil {
return
}
defer tx.Rollback()
// insert into threads table
e1, err := tx.Exec("INSERT INTO threads (ib_id,thread_title) VALUES (?,?)",
m.Ib, m.Title)
if err != nil {
return
}
// Get new thread id
tID, err := e1.LastInsertId()
if err != nil {
return
}
// insert into posts table
e2, err := tx.Exec("INSERT INTO posts (thread_id,user_id,post_time,post_ip,post_text) VALUES (?,?,NOW(),?,?)",
tID, m.UID, m.IP, m.Comment)
if err != nil {
return
}
// Get new post id
pID, err := e2.LastInsertId()
if err != nil {
return
}
// insert into images table
_, err = tx.Exec("INSERT INTO images (post_id,image_file,image_thumbnail,image_hash,image_sha,image_orig_height,image_orig_width,image_tn_height,image_tn_width) VALUES (?,?,?,?,?,?,?,?,?)",
pID, m.Filename, m.Thumbnail, m.MD5, m.SHA, m.OrigHeight, m.OrigWidth, m.ThumbHeight, m.ThumbWidth)
if err != nil {
return
}
// Commit transaction
err = tx.Commit()
if err != nil {
return
}
return
}