2c8344faf9
They are in different folders and all loaded in controllers/router.go. This means that you only have to create a folder with a router.go file and import this folder in main router.go to add a handler.
54 lignes
1,4 Kio
Go
54 lignes
1,4 Kio
Go
package torrentController
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/NyaaPantsu/nyaa/config"
|
|
"github.com/NyaaPantsu/nyaa/controllers/router"
|
|
"github.com/NyaaPantsu/nyaa/models/comments"
|
|
"github.com/NyaaPantsu/nyaa/models/torrents"
|
|
"github.com/NyaaPantsu/nyaa/utils/captcha"
|
|
msg "github.com/NyaaPantsu/nyaa/utils/messages"
|
|
"github.com/NyaaPantsu/nyaa/utils/sanitize"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// PostCommentHandler : Controller for posting a comment
|
|
func PostCommentHandler(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 32)
|
|
|
|
torrent, err := torrents.FindByID(uint(id))
|
|
if err != nil {
|
|
c.Status(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
currentUser := router.GetUser(c)
|
|
messages := msg.GetMessages(c)
|
|
|
|
if currentUser.NeedsCaptcha() {
|
|
userCaptcha := captcha.Extract(c)
|
|
if !captcha.Authenticate(userCaptcha) {
|
|
messages.AddErrorT("errors", "bad_captcha")
|
|
}
|
|
}
|
|
content := sanitize.Sanitize(c.PostForm("comment"), "comment")
|
|
|
|
if strings.TrimSpace(content) == "" {
|
|
messages.AddErrorT("errors", "comment_empty")
|
|
}
|
|
if len(content) > config.Get().CommentLength {
|
|
messages.AddErrorT("errors", "comment_toolong")
|
|
}
|
|
if !messages.HasErrors() {
|
|
|
|
_, err := comments.Create(content, torrent, currentUser)
|
|
if err != nil {
|
|
messages.Error(err)
|
|
}
|
|
}
|
|
url := "/view/" + strconv.FormatUint(uint64(torrent.ID), 10)
|
|
c.Redirect(302, url)
|
|
}
|