2017-07-16 17:14:21 +02:00
package torrentController
import (
2017-08-01 23:38:13 +02:00
"fmt"
2017-07-16 17:14:21 +02:00
"net/http"
"strconv"
"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/torrents"
"github.com/NyaaPantsu/nyaa/templates"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/upload"
"github.com/NyaaPantsu/nyaa/utils/validator/torrent"
"github.com/gin-gonic/gin"
)
// TorrentEditUserPanel : Controller for editing a user torrent by a user, after GET request
func TorrentEditUserPanel ( c * gin . Context ) {
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
torrent , _ := torrents . FindByID ( uint ( id ) )
2017-08-01 23:38:13 +02:00
torrent . LoadTags ( )
2017-07-16 17:14:21 +02:00
currentUser := router . GetUser ( c )
2017-08-01 23:38:13 +02:00
if currentUser . CurrentOrAdmin ( torrent . UploaderID ) && torrent . ID > 0 {
2017-07-16 17:14:21 +02:00
uploadForm := torrentValidator . TorrentRequest { }
uploadForm . Name = torrent . Name
uploadForm . Category = strconv . Itoa ( torrent . Category ) + "_" + strconv . Itoa ( torrent . SubCategory )
uploadForm . Remake = torrent . Status == models . TorrentStatusRemake
uploadForm . WebsiteLink = string ( torrent . WebsiteLink )
uploadForm . Description = string ( torrent . Description )
uploadForm . Hidden = torrent . Hidden
uploadForm . Languages = torrent . Languages
2017-08-01 23:38:13 +02:00
uploadForm . Tags = torrent . Tags . ToJSON ( true )
2017-07-16 17:14:21 +02:00
templates . Form ( c , "site/torrents/edit.jet.html" , uploadForm )
} else {
c . AbortWithStatus ( http . StatusNotFound )
}
}
// TorrentPostEditUserPanel : Controller for editing a user torrent by a user, after post request
func TorrentPostEditUserPanel ( c * gin . Context ) {
var uploadForm torrentValidator . UpdateRequest
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
uploadForm . ID = uint ( id )
messages := msg . GetMessages ( c )
torrent , _ := torrents . FindByID ( uint ( id ) )
2017-08-01 23:38:13 +02:00
torrent . LoadTags ( )
2017-07-16 17:14:21 +02:00
currentUser := router . GetUser ( c )
if torrent . ID > 0 && currentUser . CurrentOrAdmin ( torrent . UploaderID ) {
errUp := upload . ExtractEditInfo ( c , & uploadForm . Update )
if errUp != nil {
messages . AddErrorT ( "errors" , "fail_torrent_update" )
}
if ! messages . HasErrors ( ) {
upload . UpdateTorrent ( & uploadForm , torrent , currentUser ) . Update ( currentUser . HasAdmin ( ) )
2017-07-26 03:59:46 +02:00
c . Redirect ( http . StatusSeeOther , fmt . Sprintf ( "/view/%d?success_edit" , id ) )
return
2017-07-16 17:14:21 +02:00
}
templates . Form ( c , "site/torrents/edit.jet.html" , uploadForm . Update )
} else {
c . AbortWithStatus ( http . StatusNotFound )
}
}