2017-06-28 13:42:38 +02:00
package controllers
2017-05-05 16:39:15 +02:00
2017-05-06 10:36:37 +02:00
import (
2017-06-28 13:42:38 +02:00
"errors"
2017-05-22 00:22:42 +02:00
"fmt"
2017-05-28 01:29:46 +02:00
"io"
2017-05-06 10:36:37 +02:00
"net/http"
2017-05-08 05:34:12 +02:00
"strconv"
2017-05-10 22:09:17 +02:00
"strings"
2017-05-06 19:01:15 +02:00
2017-05-28 01:29:46 +02:00
"os"
"github.com/NyaaPantsu/nyaa/config"
2017-06-29 13:15:23 +02:00
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/activities"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/models/comments"
2017-06-29 13:15:23 +02:00
"github.com/NyaaPantsu/nyaa/models/notifications"
"github.com/NyaaPantsu/nyaa/models/reports"
"github.com/NyaaPantsu/nyaa/models/torrents"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/captcha"
"github.com/NyaaPantsu/nyaa/utils/filelist"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/utils/sanitize"
"github.com/NyaaPantsu/nyaa/utils/search/structs"
"github.com/NyaaPantsu/nyaa/utils/upload"
"github.com/NyaaPantsu/nyaa/utils/validator/torrent"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
2017-05-05 16:39:15 +02:00
)
2017-05-25 21:54:58 +02:00
// ViewHandler : Controller for displaying a torrent
2017-06-28 13:42:38 +02:00
func ViewHandler ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseInt ( c . Param ( "id" ) , 10 , 32 )
2017-06-28 13:42:38 +02:00
messages := msg . GetMessages ( c )
user := getUser ( c )
if c . Request . URL . Query ( ) [ "success" ] != nil {
2017-07-04 01:15:43 +02:00
messages . AddInfoT ( "infos" , "torrent_uploaded" )
}
if c . Request . URL . Query ( ) [ "badcaptcha" ] != nil {
messages . AddErrorT ( "errors" , "bad_captcha" )
}
if c . Request . URL . Query ( ) [ "reported" ] != nil {
messages . AddInfoTf ( "infos" , "report_msg" , id )
2017-05-20 17:01:13 +02:00
}
2017-05-05 16:39:15 +02:00
2017-07-02 23:53:23 +02:00
torrent , err := torrents . FindByID ( uint ( id ) )
2017-05-23 05:18:19 +02:00
2017-06-28 13:42:38 +02:00
if c . Request . URL . Query ( ) [ "notif" ] != nil {
2017-07-02 23:53:23 +02:00
notifications . ToggleReadNotification ( torrent . Identifier ( ) , user . ID )
2017-05-21 01:06:40 +02:00
}
2017-05-07 13:51:33 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
NotFoundHandler ( c )
2017-05-07 13:51:33 +02:00
return
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
b := torrent . ToJSON ( )
2017-05-26 03:53:18 +02:00
folder := filelist . FileListToFolder ( torrent . FileList , "root" )
2017-05-12 11:58:22 +02:00
captchaID := ""
2017-07-02 23:53:23 +02:00
if user . NeedsCaptcha ( ) {
2017-05-12 11:58:22 +02:00
captchaID = captcha . GetID ( )
}
2017-06-28 13:42:38 +02:00
torrentTemplate ( c , b , folder , captchaID )
2017-05-06 10:36:37 +02:00
}
2017-05-08 05:34:12 +02:00
2017-05-25 21:54:58 +02:00
// ViewHeadHandler : Controller for checking a torrent
2017-06-28 13:42:38 +02:00
func ViewHeadHandler ( c * gin . Context ) {
id , err := strconv . ParseInt ( c . Param ( "id" ) , 10 , 32 )
2017-05-23 05:18:19 +02:00
if err != nil {
return
}
2017-07-02 23:53:23 +02:00
_ , err = torrents . FindRawByID ( uint ( id ) )
2017-05-23 05:18:19 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
NotFoundHandler ( c )
2017-05-23 05:18:19 +02:00
return
}
2017-06-28 13:42:38 +02:00
c . AbortWithStatus ( http . StatusOK )
2017-05-23 05:18:19 +02:00
}
2017-05-25 21:54:58 +02:00
// PostCommentHandler : Controller for posting a comment
2017-06-28 13:42:38 +02:00
func PostCommentHandler ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseInt ( c . Param ( "id" ) , 10 , 32 )
2017-05-08 05:34:12 +02:00
2017-07-02 23:53:23 +02:00
torrent , err := torrents . FindByID ( uint ( id ) )
2017-05-23 05:18:19 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
NotFoundHandler ( c )
2017-05-22 00:22:42 +02:00
return
}
2017-06-28 13:42:38 +02:00
currentUser := getUser ( c )
messages := msg . GetMessages ( c )
2017-05-21 19:38:39 +02:00
2017-07-02 23:53:23 +02:00
if currentUser . NeedsCaptcha ( ) {
2017-06-28 13:42:38 +02:00
userCaptcha := captcha . Extract ( c )
2017-05-12 11:58:22 +02:00
if ! captcha . Authenticate ( userCaptcha ) {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "bad_captcha" )
2017-05-12 11:58:22 +02:00
}
}
2017-07-02 23:53:23 +02:00
content := sanitize . Sanitize ( c . PostForm ( "comment" ) , "comment" )
2017-05-08 05:34:12 +02:00
2017-05-11 21:46:14 +02:00
if strings . TrimSpace ( content ) == "" {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "comment_empty" )
2017-05-11 21:46:14 +02:00
}
2017-07-10 14:11:05 +02:00
if len ( content ) > config . Get ( ) . CommentLength {
2017-05-29 13:25:36 +02:00
messages . AddErrorT ( "errors" , "comment_toolong" )
}
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-05-22 00:22:42 +02:00
2017-07-09 16:21:34 +02:00
_ , err := comments . Create ( content , torrent , currentUser )
2017-05-21 19:38:39 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
messages . Error ( err )
2017-05-21 19:38:39 +02:00
}
2017-05-08 05:34:12 +02:00
}
2017-06-28 13:42:38 +02:00
ViewHandler ( c )
2017-05-08 05:34:12 +02:00
}
2017-05-10 10:17:21 +02:00
2017-05-25 21:54:58 +02:00
// ReportTorrentHandler : Controller for sending a torrent report
2017-06-28 13:42:38 +02:00
func ReportTorrentHandler ( c * gin . Context ) {
2017-07-04 01:15:43 +02:00
fmt . Println ( "report" )
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseInt ( c . Param ( "id" ) , 10 , 32 )
2017-06-28 13:42:38 +02:00
messages := msg . GetMessages ( c )
2017-07-04 01:15:43 +02:00
captchaError := "?reported"
2017-06-28 13:42:38 +02:00
currentUser := getUser ( c )
2017-07-02 23:53:23 +02:00
if currentUser . NeedsCaptcha ( ) {
2017-06-28 13:42:38 +02:00
userCaptcha := captcha . Extract ( c )
2017-05-12 11:58:22 +02:00
if ! captcha . Authenticate ( userCaptcha ) {
2017-07-04 01:15:43 +02:00
captchaError = "?badcaptcha"
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "bad_captcha" )
2017-05-12 11:58:22 +02:00
}
}
2017-07-02 23:53:23 +02:00
torrent , err := torrents . FindByID ( uint ( id ) )
if err != nil {
messages . Error ( err )
}
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-07-05 17:06:24 +02:00
_ , err := reports . Create ( c . PostForm ( "report_type" ) , torrent , currentUser )
2017-06-06 00:06:52 +02:00
messages . AddInfoTf ( "infos" , "report_msg" , id )
2017-05-21 19:38:39 +02:00
if err != nil {
messages . ImportFromError ( "errors" , err )
}
2017-07-04 01:15:43 +02:00
c . Redirect ( http . StatusSeeOther , "/view/" + strconv . Itoa ( int ( torrent . ID ) ) + captchaError )
} else {
ReportViewTorrentHandler ( c )
}
}
// ReportTorrentHandler : Controller for sending a torrent report
func ReportViewTorrentHandler ( c * gin . Context ) {
type Report struct {
ID uint
CaptchaID string
}
id , _ := strconv . ParseInt ( c . Param ( "id" ) , 10 , 32 )
messages := msg . GetMessages ( c )
currentUser := getUser ( c )
if currentUser . ID > 0 {
torrent , err := torrents . FindByID ( uint ( id ) )
if err != nil {
messages . Error ( err )
}
captchaID := ""
if currentUser . NeedsCaptcha ( ) {
captchaID = captcha . GetID ( )
}
formTemplate ( c , "site/torrents/report.jet.html" , Report { torrent . ID , captchaID } )
} else {
c . Status ( 404 )
2017-05-10 10:17:21 +02:00
}
}
2017-05-23 04:05:33 +02:00
2017-05-25 21:54:58 +02:00
// TorrentEditUserPanel : Controller for editing a user torrent by a user, after GET request
2017-06-28 13:42:38 +02:00
func TorrentEditUserPanel ( c * gin . Context ) {
2017-07-04 02:40:54 +02:00
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
2017-07-02 23:53:23 +02:00
torrent , _ := torrents . FindByID ( uint ( id ) )
2017-06-28 13:42:38 +02:00
currentUser := getUser ( c )
2017-07-02 23:53:23 +02:00
if currentUser . CurrentOrAdmin ( torrent . UploaderID ) {
uploadForm := torrentValidator . TorrentRequest { }
2017-05-23 04:12:02 +02:00
uploadForm . Name = torrent . Name
uploadForm . Category = strconv . Itoa ( torrent . Category ) + "_" + strconv . Itoa ( torrent . SubCategory )
2017-07-01 23:09:35 +02:00
uploadForm . Remake = torrent . Status == models . TorrentStatusRemake
2017-05-23 04:12:02 +02:00
uploadForm . WebsiteLink = string ( torrent . WebsiteLink )
uploadForm . Description = string ( torrent . Description )
2017-05-27 20:33:40 +02:00
uploadForm . Hidden = torrent . Hidden
2017-07-02 23:53:23 +02:00
uploadForm . Languages = torrent . Languages
2017-06-28 13:42:38 +02:00
formTemplate ( c , "site/torrents/edit.jet.html" , uploadForm )
2017-05-23 04:05:33 +02:00
} else {
2017-06-28 13:42:38 +02:00
NotFoundHandler ( c )
2017-05-23 04:05:33 +02:00
}
}
2017-05-25 21:54:58 +02:00
// TorrentPostEditUserPanel : Controller for editing a user torrent by a user, after post request
2017-06-28 13:42:38 +02:00
func TorrentPostEditUserPanel ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
var uploadForm torrentValidator . UpdateRequest
2017-07-04 02:40:54 +02:00
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
2017-07-02 23:53:23 +02:00
uploadForm . ID = uint ( id )
2017-06-28 13:42:38 +02:00
messages := msg . GetMessages ( c )
2017-07-02 23:53:23 +02:00
torrent , _ := torrents . FindByID ( uint ( id ) )
2017-06-28 13:42:38 +02:00
currentUser := getUser ( c )
2017-07-02 23:53:23 +02:00
if torrent . ID > 0 && currentUser . CurrentOrAdmin ( torrent . UploaderID ) {
errUp := upload . ExtractEditInfo ( c , & uploadForm . Update )
2017-05-23 04:05:33 +02:00
if errUp != nil {
messages . AddErrorT ( "errors" , "fail_torrent_update" )
}
if ! messages . HasErrors ( ) {
2017-07-05 17:06:24 +02:00
upload . UpdateTorrent ( & uploadForm , torrent , currentUser ) . Update ( currentUser . HasAdmin ( ) )
2017-05-23 04:05:33 +02:00
messages . AddInfoT ( "infos" , "torrent_updated" )
}
2017-07-04 02:40:54 +02:00
formTemplate ( c , "site/torrents/edit.jet.html" , uploadForm . Update )
2017-05-23 04:05:33 +02:00
} else {
2017-06-28 13:42:38 +02:00
NotFoundHandler ( c )
2017-05-23 04:05:33 +02:00
}
}
2017-05-25 21:54:58 +02:00
// TorrentDeleteUserPanel : Controller for deleting a user torrent by a user
2017-06-28 13:42:38 +02:00
func TorrentDeleteUserPanel ( c * gin . Context ) {
2017-07-04 02:40:54 +02:00
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
2017-06-28 13:42:38 +02:00
currentUser := getUser ( c )
2017-07-02 23:53:23 +02:00
torrent , _ := torrents . FindByID ( uint ( id ) )
if currentUser . CurrentOrAdmin ( torrent . UploaderID ) {
_ , _ , err := torrent . Delete ( false )
2017-05-24 09:11:13 +02:00
if err == nil {
2017-07-02 23:53:23 +02:00
_ , username := torrents . HideUser ( torrent . UploaderID , torrent . Uploader . Username , torrent . Hidden )
if currentUser . HasAdmin ( ) { // We hide username on log activity if user is not admin and torrent is hidden
activities . Log ( & models . User { } , torrent . Identifier ( ) , "delete" , "torrent_deleted_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , currentUser . Username )
2017-06-15 04:44:46 +02:00
} else {
2017-07-02 23:53:23 +02:00
activities . Log ( & models . User { } , torrent . Identifier ( ) , "delete" , "torrent_deleted_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , username )
2017-06-15 04:44:46 +02:00
}
2017-05-23 04:05:33 +02:00
//delete reports of torrent
2017-07-02 23:53:23 +02:00
whereParams := structs . CreateWhereParams ( "torrent_id = ?" , id )
torrentReports , _ , _ := reports . FindOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range torrentReports {
report . Delete ( false )
2017-05-23 04:05:33 +02:00
}
}
2017-06-28 13:42:38 +02:00
c . Redirect ( http . StatusSeeOther , "/?deleted" )
2017-05-23 04:05:33 +02:00
} else {
2017-06-28 13:42:38 +02:00
NotFoundHandler ( c )
2017-05-23 04:05:33 +02:00
}
2017-05-24 09:11:13 +02:00
}
2017-05-28 01:29:46 +02:00
// DownloadTorrent : Controller for downloading a torrent
2017-06-28 13:42:38 +02:00
func DownloadTorrent ( c * gin . Context ) {
hash := c . Param ( "hash" )
2017-05-28 01:29:46 +02:00
2017-07-10 14:11:05 +02:00
if hash == "" && len ( config . Get ( ) . Torrents . FileStorage ) == 0 {
2017-05-28 01:29:46 +02:00
//File not found, send 404
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusNotFound , errors . New ( "File not found" ) )
2017-05-28 01:29:46 +02:00
return
}
//Check if file exists and open
2017-07-10 14:11:05 +02:00
Openfile , err := os . Open ( fmt . Sprintf ( "%s%c%s.torrent" , config . Get ( ) . Torrents . FileStorage , os . PathSeparator , hash ) )
2017-05-28 01:29:46 +02:00
if err != nil {
//File not found, send 404
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusNotFound , errors . New ( "File not found" ) )
2017-05-28 01:29:46 +02:00
return
}
2017-06-14 12:10:03 +02:00
defer Openfile . Close ( ) //Close after function return
2017-05-28 01:29:46 +02:00
//Get the file size
FileStat , _ := Openfile . Stat ( ) //Get info from file
FileSize := strconv . FormatInt ( FileStat . Size ( ) , 10 ) //Get file size as a string
2017-07-02 23:53:23 +02:00
torrent , err := torrents . FindRawByHash ( hash )
2017-05-28 01:29:46 +02:00
if err != nil {
//File not found, send 404
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusNotFound , errors . New ( "File not found" ) )
2017-05-28 01:29:46 +02:00
return
}
2017-06-28 13:42:38 +02:00
c . Header ( "Content-Disposition" , fmt . Sprintf ( "attachment; filename=\"%s.torrent\"" , torrent . Name ) )
c . Header ( "Content-Type" , "application/x-bittorrent" )
c . Header ( "Content-Length" , FileSize )
2017-05-28 01:29:46 +02:00
//Send the file
// We reset the offset to 0
Openfile . Seek ( 0 , 0 )
2017-06-28 13:42:38 +02:00
io . Copy ( c . Writer , Openfile ) //'Copy' the file to the client
2017-05-28 01:29:46 +02:00
}