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/controllers/upload/upload.go
akuma06 2c8344faf9 Controllers are now separated
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.
2017-07-16 17:14:21 +02:00

79 lignes
2,2 Kio
Go

package uploadController
import (
"errors"
"net/http"
"strconv"
"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/models/torrents"
"github.com/NyaaPantsu/nyaa/templates"
"github.com/NyaaPantsu/nyaa/utils/captcha"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
"github.com/NyaaPantsu/nyaa/utils/upload"
"github.com/NyaaPantsu/nyaa/utils/validator/torrent"
"github.com/gin-gonic/gin"
)
// UploadHandler : Main Controller for uploading a torrent
func UploadHandler(c *gin.Context) {
user := router.GetUser(c)
if !user.CanUpload() {
T := publicSettings.GetTfuncFromRequest(c)
c.AbortWithError(http.StatusBadRequest, errors.New(string(T("uploads_disabled"))))
return
}
if c.Request.Method == "POST" {
UploadPostHandler(c)
}
UploadGetHandler(c)
}
// UploadPostHandler : Controller for uploading a torrent, after POST request, redirect or makes error in messages
func UploadPostHandler(c *gin.Context) {
var uploadForm torrentValidator.TorrentRequest
user := router.GetUser(c)
messages := msg.GetMessages(c) // new utils for errors and infos
if user.NeedsCaptcha() {
userCaptcha := captcha.Extract(c)
if !captcha.Authenticate(userCaptcha) {
messages.AddError("errors", captcha.ErrInvalidCaptcha.Error())
}
}
// validation is done in ExtractInfo()
err := upload.ExtractInfo(c, &uploadForm)
if err != nil {
messages.AddError("errors", err.Error())
}
err = torrents.ExistOrDelete(uploadForm.Infohash, user)
if err != nil {
messages.AddError("errors", err.Error())
}
if !messages.HasErrors() {
// add to db and redirect
torrent, _ := torrents.Create(user, &uploadForm)
url := "/view/" + strconv.FormatUint(uint64(torrent.ID), 10)
c.Redirect(302, url+"?success")
}
}
// UploadGetHandler : Controller for uploading a torrent, after GET request or Failed Post request
func UploadGetHandler(c *gin.Context) {
var uploadForm torrentValidator.TorrentRequest
_ = upload.ExtractInfo(c, &uploadForm)
user := router.GetUser(c)
if user.NeedsCaptcha() {
uploadForm.CaptchaID = captcha.GetID()
} else {
uploadForm.CaptchaID = ""
}
templates.Form(c, "site/torrents/upload.jet.html", uploadForm)
}