-
Notifications
You must be signed in to change notification settings - Fork 29
/
page.go
316 lines (276 loc) · 9.35 KB
/
page.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2022 E99p1ant. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package question
import (
"crypto/md5"
"fmt"
"io"
"mime/multipart"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/flamego/recaptcha"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/wuhan005/govalid"
"github.com/NekoWheel/NekoBox/internal/conf"
"github.com/NekoWheel/NekoBox/internal/context"
"github.com/NekoWheel/NekoBox/internal/db"
"github.com/NekoWheel/NekoBox/internal/dbutil"
"github.com/NekoWheel/NekoBox/internal/form"
"github.com/NekoWheel/NekoBox/internal/mail"
"github.com/NekoWheel/NekoBox/internal/security/censor"
)
func Pager(ctx context.Context) {
domain := ctx.Param("domain")
pageUser, err := db.Users.GetByDomain(ctx.Request().Context(), domain)
if err != nil {
if errors.Is(err, db.ErrUserNotExists) {
ctx.Redirect("/")
return
} else {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to get user by domain")
ctx.SetInternalError()
}
ctx.Success("question/page")
return
}
ctx.Map(pageUser)
pageQuestions, err := db.Questions.GetByUserID(ctx.Request().Context(), pageUser.ID, db.GetQuestionsByUserIDOptions{
Cursor: &dbutil.Cursor{},
FilterAnswered: true,
})
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to get questions by page id")
ctx.SetInternalError()
ctx.Success("question/page")
return
}
answeredCount, err := db.Questions.Count(ctx.Request().Context(), pageUser.ID, db.GetQuestionsCountOptions{
FilterAnswered: true,
})
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to count questions")
ctx.SetInternalError()
ctx.Success("question/page")
return
}
ctx.SetTitle(fmt.Sprintf("%s的提问箱 - NekoBox", pageUser.Name))
ctx.Data["IsOwnPage"] = ctx.IsLogged && ctx.User.ID == pageUser.ID
ctx.Data["PageUser"] = pageUser
ctx.Data["PageQuestions"] = pageQuestions
ctx.Data["CanAsk"] = ctx.IsLogged || pageUser.HarassmentSetting != db.HarassmentSettingTypeRegisterOnly
ctx.Data["AnsweredCount"] = answeredCount
if len(pageQuestions) > 0 {
ctx.Data["PageQuestionCursor"] = pageQuestions[len(pageQuestions)-1].ID
}
}
func List(ctx context.Context) {
ctx.Success("question/list")
}
func ListAPI(ctx context.Context) error {
domain := ctx.Param("domain")
pageSize := ctx.QueryInt("page_size")
cursorValue := ctx.Query("cursor")
pageUser, err := db.Users.GetByDomain(ctx.Request().Context(), domain)
if err != nil {
if errors.Is(err, db.ErrUserNotExists) {
return ctx.JSONError(40400, "用户不存在")
}
return ctx.ServerError()
}
pageQuestions, err := db.Questions.GetByUserID(ctx.Request().Context(), pageUser.ID, db.GetQuestionsByUserIDOptions{
Cursor: &dbutil.Cursor{
Value: cursorValue,
PageSize: pageSize,
},
FilterAnswered: true,
})
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to get questions by page id")
return ctx.ServerError()
}
return ctx.JSON(pageQuestions)
}
func New(ctx context.Context, f form.NewQuestion, pageUser *db.User, recaptcha recaptcha.RecaptchaV3) {
if !ctx.IsLogged && pageUser.HarassmentSetting == db.HarassmentSettingTypeRegisterOnly {
ctx.SetErrorFlash("提问箱的主人设置了仅注册用户才能提问,请先登录。")
ctx.Redirect(fmt.Sprintf("/login?to=%s", ctx.Request().Request.RequestURI))
return
}
var receiveReplyEmail string
if f.ReceiveReplyViaEmail != "" {
// Check the email address is valid.
if errs, ok := govalid.Check(struct {
Email string `valid:"required;email" label:"邮箱地址"`
}{
Email: f.ReceiveReplyEmail,
}); !ok {
ctx.SetError(errs[0], f)
ctx.Success("question/list")
return
}
receiveReplyEmail = f.ReceiveReplyEmail
}
if ctx.HasError() {
ctx.Success("question/list")
return
}
// Check recaptcha code.
resp, err := recaptcha.Verify(f.Recaptcha, ctx.Request().Request.RemoteAddr)
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to check recaptcha")
ctx.SetInternalErrorFlash()
ctx.Redirect("/_/" + pageUser.Domain)
return
}
if !resp.Success {
ctx.SetErrorFlash("验证码错误")
ctx.Redirect("/_/" + pageUser.Domain)
return
}
content := f.Content
// 🚨 Content security check.
censorResponse, err := censor.Text(ctx.Request().Context(), content)
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to censor text")
}
if err == nil && !censorResponse.Pass {
errorMessage := censorResponse.ErrorMessage()
ctx.SetError(errors.New(errorMessage), f)
ctx.Success("question/list")
return
}
// ⚠️ Here is the aliyun CDN origin IP header.
// A security problem may occur if the CDN is enabled and users can modify the header.
fromIP := ctx.Request().Header.Get("Ali-CDN-Real-IP")
if fromIP == "" {
fromIP = ctx.Request().Header.Get("CF-Connecting-IP")
}
if fromIP == "" {
fromIP = ctx.Request().Header.Get("X-Real-IP")
}
// Try to get current logged user.
var askerUserID uint
if ctx.IsLogged {
askerUserID = ctx.User.ID
}
question, err := db.Questions.Create(ctx.Request().Context(), db.CreateQuestionOptions{
FromIP: fromIP,
UserID: pageUser.ID,
Content: content,
ReceiveReplyEmail: receiveReplyEmail,
AskerUserID: askerUserID,
})
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to create new question")
ctx.SetInternalError(f)
ctx.Success("question/list")
return
}
if len(f.Images) > 0 {
image := f.Images[0]
if err := uploadImage(ctx, uploadImageOptions{
Type: db.UploadImageQuestionTypeAsk,
Image: image,
QuestionID: question.ID,
UploaderUserID: askerUserID,
}); err != nil {
if errors.Is(err, ErrUploadImageSizeTooLarge) {
ctx.SetErrorFlash("图片文件大小不能大于 5Mb")
ctx.Success("question/list")
return
} else {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to upload image")
ctx.SetError(errors.New("上传图片失败"), f)
ctx.Success("question/list")
return
}
}
}
// Update censor result.
if err := db.Questions.UpdateCensor(ctx.Request().Context(), question.ID, db.UpdateQuestionCensorOptions{
ContentCensorMetadata: censorResponse.ToJSON(),
}); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to update question censor result")
}
go func() {
if pageUser.Notify == db.NotifyTypeEmail {
// Send notification to page user.
if err := mail.SendNewQuestionMail(pageUser.Email, pageUser.Domain, question.ID, question.Content); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to send new question mail to user")
}
}
}()
ctx.SetSuccessFlash("发送问题成功!")
ctx.Redirect("/_/" + pageUser.Domain)
}
type uploadImageOptions struct {
Type db.UploadImageQuestionType
Image *multipart.FileHeader
QuestionID uint
UploaderUserID uint
IsDeletingPrevious bool
}
var ErrUploadImageSizeTooLarge = errors.New("图片文件大小不能大于 5Mb")
func uploadImage(ctx context.Context, opts uploadImageOptions) error {
image := opts.Image
fileName := image.Filename
fileExt := filepath.Ext(fileName)
fileSize := image.Size
if fileSize > 1024*1024*5 { // 5Mib
return ErrUploadImageSizeTooLarge
}
now := time.Now()
fileKey := fmt.Sprintf("%d/%d/%d%s", now.Year(), now.Month(), now.UnixNano(), fileExt)
uploadImageFile, err := image.Open()
if err != nil {
return errors.Wrap(err, "open image")
}
defer func() { _ = uploadImageFile.Close() }()
hasher := md5.New()
reader := io.TeeReader(uploadImageFile, hasher)
r2Resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: conf.Upload.ImageEndpoint,
HostnameImmutable: true,
Source: aws.EndpointSourceCustom,
}, nil
})
cfg, err := config.LoadDefaultConfig(ctx.Request().Context(),
config.WithEndpointResolverWithOptions(r2Resolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(conf.Upload.ImageAccessID, conf.Upload.ImageAccessSecret, "")),
config.WithRegion("auto"),
)
if err != nil {
return errors.Wrap(err, "load config")
}
client := s3.NewFromConfig(cfg)
if _, err := client.PutObject(ctx.Request().Context(), &s3.PutObjectInput{
Bucket: aws.String(conf.Upload.ImageBucket),
Key: aws.String(fileKey),
Body: reader,
ContentLength: aws.Int64(fileSize),
}); err != nil {
return errors.Wrap(err, "put object")
}
fileMd5 := fmt.Sprintf("%x", hasher.Sum(nil))
_, err = db.UploadImgaes.Create(ctx.Request().Context(), db.CreateUploadImageOptions{
Type: opts.Type,
QuestionID: opts.QuestionID,
UploaderUserID: opts.UploaderUserID,
Name: fileName,
FileSize: fileSize,
Md5: fileMd5,
Key: fileKey,
IsDeletingPrevious: opts.IsDeletingPrevious,
})
if err != nil {
return errors.Wrap(err, "create upload image")
}
return nil
}