2017-03-12 19:37:53 +00:00
|
|
|
package board
|
|
|
|
|
|
|
|
import (
|
2017-03-13 17:45:23 +00:00
|
|
|
"bytes"
|
2017-03-12 19:37:53 +00:00
|
|
|
"fmt"
|
2017-03-13 17:45:23 +00:00
|
|
|
"github.com/nfnt/resize"
|
2017-03-12 19:37:53 +00:00
|
|
|
"github.com/pressly/chi"
|
|
|
|
"github.com/tidwall/buntdb"
|
|
|
|
"go.rls.moe/nyx/http/errw"
|
|
|
|
"go.rls.moe/nyx/http/middle"
|
|
|
|
"go.rls.moe/nyx/resources"
|
2017-03-13 17:45:23 +00:00
|
|
|
"image"
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
"image/png"
|
2017-03-12 19:37:53 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleNewReply(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2017-03-13 17:45:23 +00:00
|
|
|
err = r.ParseMultipartForm(10 * 1024 * 1024)
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2017-03-12 19:37:53 +00:00
|
|
|
|
2017-03-13 15:04:00 +00:00
|
|
|
if !resources.VerifyCaptcha(r) {
|
|
|
|
http.Redirect(w, r,
|
|
|
|
fmt.Sprintf("/%s/%s/thread.html?err=wrong_captcha",
|
|
|
|
chi.URLParam(r, "board"), chi.URLParam(r, "thread")),
|
|
|
|
http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 19:37:53 +00:00
|
|
|
var reply = &resources.Reply{}
|
|
|
|
|
|
|
|
reply.Board = chi.URLParam(r, "board")
|
|
|
|
tid, err := strconv.Atoi(chi.URLParam(r, "thread"))
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2017-03-13 15:04:00 +00:00
|
|
|
reply.Thread = tid
|
2017-03-12 19:37:53 +00:00
|
|
|
reply.Text = r.FormValue("text")
|
2017-03-13 15:04:00 +00:00
|
|
|
if len(reply.Text) > 10000 {
|
2017-03-12 19:37:53 +00:00
|
|
|
errw.ErrorWriter(errw.MakeErrorWithTitle("I'm sorry but I can't do that", "These are too many characters"), w, r)
|
|
|
|
return
|
|
|
|
}
|
2017-03-13 17:45:23 +00:00
|
|
|
if len(reply.Text) < 5 {
|
2017-03-12 19:37:53 +00:00
|
|
|
errw.ErrorWriter(errw.MakeErrorWithTitle("I'm sorry but I can't do that", "These are not enough characters"), w, r)
|
|
|
|
return
|
|
|
|
}
|
2017-03-13 15:04:00 +00:00
|
|
|
|
|
|
|
if score, err := resources.SpamScore(reply.Text); err != nil || !resources.CaptchaPass(score) {
|
|
|
|
http.Redirect(w, r,
|
|
|
|
fmt.Sprintf("/%s/%s/thread.html?err=trollthrottle",
|
|
|
|
chi.URLParam(r, "board"), chi.URLParam(r, "thread")),
|
|
|
|
http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-13 17:45:23 +00:00
|
|
|
{
|
|
|
|
file, _, err := r.FormFile("image")
|
|
|
|
if err != nil && err != http.ErrMissingFile {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
} else if err != http.ErrMissingFile {
|
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
thumb := resize.Thumbnail(128, 128, img, resize.Lanczos3)
|
|
|
|
imgBuf := bytes.NewBuffer([]byte{})
|
|
|
|
err = png.Encode(imgBuf, img)
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Image has size ", len(imgBuf.Bytes()))
|
|
|
|
reply.Image = imgBuf.Bytes()
|
|
|
|
imgBuf = bytes.NewBuffer([]byte{})
|
|
|
|
err = png.Encode(imgBuf, thumb)
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reply.Thumbnail = imgBuf.Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 19:37:53 +00:00
|
|
|
reply.Metadata = map[string]string{}
|
|
|
|
if r.FormValue("tripcode") != "" {
|
|
|
|
reply.Metadata["trip"] = resources.CalcTripCode(r.FormValue("tripcode"))
|
|
|
|
} else {
|
|
|
|
reply.Metadata["trip"] = "Anonymous"
|
|
|
|
}
|
|
|
|
|
|
|
|
db := middle.GetDB(r)
|
|
|
|
if err = db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
thread, err := resources.GetThread(tx, r.Host, reply.Board, reply.Thread)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return resources.NewReply(tx, r.Host, reply.Board, thread, reply, false)
|
|
|
|
}); err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/%s/%d/thread.html", chi.URLParam(r, "board"), reply.Thread), http.StatusSeeOther)
|
|
|
|
}
|