2017-07-16 17:14:21 +02:00
package torrentController
import (
"net/http"
"strconv"
"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/models/notifications"
"github.com/NyaaPantsu/nyaa/models/torrents"
"github.com/NyaaPantsu/nyaa/templates"
"github.com/NyaaPantsu/nyaa/utils/captcha"
"github.com/NyaaPantsu/nyaa/utils/filelist"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/gin-gonic/gin"
)
// ViewHandler : Controller for displaying a torrent
func ViewHandler ( c * gin . Context ) {
id , _ := strconv . ParseInt ( c . Param ( "id" ) , 10 , 32 )
messages := msg . GetMessages ( c )
user := router . GetUser ( c )
2017-07-28 20:43:22 +02:00
// Display success message on upload
2017-07-16 17:14:21 +02:00
if c . Request . URL . Query ( ) [ "success" ] != nil {
messages . AddInfoT ( "infos" , "torrent_uploaded" )
}
2017-07-28 20:43:22 +02:00
// Display success message on edit
2017-07-26 03:59:46 +02:00
if c . Request . URL . Query ( ) [ "success_edit" ] != nil {
messages . AddInfoT ( "infos" , "torrent_updated" )
}
2017-07-28 20:43:22 +02:00
// Display wrong captcha error message
2017-07-16 17:14:21 +02:00
if c . Request . URL . Query ( ) [ "badcaptcha" ] != nil {
messages . AddErrorT ( "errors" , "bad_captcha" )
}
2017-07-28 20:43:22 +02:00
// Display reported successful message
2017-07-16 17:14:21 +02:00
if c . Request . URL . Query ( ) [ "reported" ] != nil {
messages . AddInfoTf ( "infos" , "report_msg" , id )
}
2017-07-28 20:43:22 +02:00
// Retrieve the torrent
2017-07-16 17:14:21 +02:00
torrent , err := torrents . FindByID ( uint ( id ) )
2017-07-28 20:43:22 +02:00
// If come from notification, toggle the notification as read
if c . Request . URL . Query ( ) [ "notif" ] != nil && user . ID > 0 {
2017-07-16 17:14:21 +02:00
notifications . ToggleReadNotification ( torrent . Identifier ( ) , user . ID )
}
2017-07-28 20:43:22 +02:00
// If torrent not found, display 404
2017-07-16 17:14:21 +02:00
if err != nil {
c . Status ( http . StatusNotFound )
return
}
2017-07-28 20:43:22 +02:00
// We load tags for user and torrents
user . LoadTags ( torrent )
torrent . LoadTags ( )
// Convert torrent to the JSON Model used to display a torrent
// Since many datas need to be parsed from a simple torrent model to the actual display
2017-07-16 17:14:21 +02:00
b := torrent . ToJSON ( )
2017-07-28 20:43:22 +02:00
// Get the folder root for the filelist view
2017-07-16 17:14:21 +02:00
folder := filelist . FileListToFolder ( torrent . FileList , "root" )
captchaID := ""
2017-07-28 20:43:22 +02:00
//Generate a captcha
2017-07-16 17:14:21 +02:00
if user . NeedsCaptcha ( ) {
captchaID = captcha . GetID ( )
}
2017-07-28 20:43:22 +02:00
// Display finally the view
2017-07-16 17:14:21 +02:00
templates . Torrent ( c , b , folder , captchaID )
}
// ViewHeadHandler : Controller for checking a torrent
func ViewHeadHandler ( c * gin . Context ) {
id , err := strconv . ParseInt ( c . Param ( "id" ) , 10 , 32 )
if err != nil {
return
}
_ , err = torrents . FindRawByID ( uint ( id ) )
if err != nil {
c . Status ( http . StatusNotFound )
return
}
c . AbortWithStatus ( http . StatusOK )
}