Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/viewTorrentHandler.go

105 lignes
2,4 Kio
Go
Brut Vue normale Historique

package router
2017-05-06 10:36:37 +02:00
import (
"net/http"
"strconv"
2017-05-10 22:09:17 +02:00
"strings"
2017-05-08 19:26:29 +02:00
"time"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/captcha"
"github.com/ewhal/nyaa/service/torrent"
2017-05-09 19:23:21 +02:00
"github.com/ewhal/nyaa/util"
"github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/log"
"github.com/gorilla/mux"
)
func ViewHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
torrent, err := torrentService.GetTorrentById(id)
if err != nil {
NotFoundHandler(w, r)
return
}
b := torrent.ToJSON()
htv := ViewTemplateVariables{b, captcha.Captcha{CaptchaID: captcha.GetID()}, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)}
languages.SetTranslationFromRequest(viewTemplate, r, "en-us")
err = viewTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {
log.Errorf("ViewHandler(): %s", err)
}
2017-05-06 10:36:37 +02:00
}
func PostCommentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
2017-05-10 22:09:17 +02:00
if strings.TrimSpace(r.FormValue("comment")) == "" {
http.Error(w, "comment empty", 406)
}
userCaptcha := captcha.Extract(r)
if !captcha.Authenticate(userCaptcha) {
http.Error(w, "bad captcha", 403)
}
2017-05-08 13:18:52 +02:00
currentUser := GetUser(r)
2017-05-08 06:58:21 +02:00
content := p.Sanitize(r.FormValue("comment"))
idNum, err := strconv.Atoi(id)
userID := currentUser.ID
comment := model.Comment{TorrentID: uint(idNum), UserID: userID, Content: content, CreatedAt: time.Now()}
2017-05-10 20:42:20 +02:00
err = db.ORM.Create(&comment).Error
2017-05-09 19:23:21 +02:00
if err != nil {
util.SendError(w, err, 500)
return
}
url, err := Router.Get("view_torrent").URL("id", id)
if err == nil {
http.Redirect(w, r, url.String(), 302)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
2017-05-10 10:17:21 +02:00
func ReportTorrentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
userCaptcha := captcha.Extract(r)
if !captcha.Authenticate(userCaptcha) {
http.Error(w, "bad captcha", 403)
}
currentUser := GetUser(r)
idNum, err := strconv.Atoi(id)
userID := currentUser.ID
2017-05-10 20:42:20 +02:00
report := model.TorrentReport{
Description: r.FormValue("report_type"),
TorrentID: uint(idNum),
UserID: userID,
2017-05-11 00:10:42 +02:00
CreatedAt: time.Now(),
2017-05-10 20:42:20 +02:00
}
2017-05-10 10:17:21 +02:00
err = db.ORM.Create(&report).Error
if err != nil {
util.SendError(w, err, 500)
return
}
url, err := Router.Get("view_torrent").URL("id", id)
if err == nil {
http.Redirect(w, r, url.String(), 302)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}