2017-05-05 16:39:15 +02:00
package router
2017-05-06 10:36:37 +02:00
import (
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-08 19:26:29 +02:00
"time"
2017-05-06 19:01:15 +02:00
2017-05-28 01:29:46 +02:00
"os"
"github.com/NyaaPantsu/nyaa/config"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
2017-05-23 04:05:33 +02:00
"github.com/NyaaPantsu/nyaa/service"
2017-06-15 04:44:46 +02:00
"github.com/NyaaPantsu/nyaa/service/activity"
2017-06-05 15:19:25 +02:00
"github.com/NyaaPantsu/nyaa/service/api"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/captcha"
2017-05-21 01:06:40 +02:00
"github.com/NyaaPantsu/nyaa/service/notifier"
2017-05-23 04:05:33 +02:00
"github.com/NyaaPantsu/nyaa/service/report"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/torrent"
"github.com/NyaaPantsu/nyaa/service/user/permission"
2017-05-23 22:09:20 +02:00
"github.com/NyaaPantsu/nyaa/util"
2017-05-25 21:54:58 +02:00
"github.com/NyaaPantsu/nyaa/util/filelist"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util/log"
2017-05-20 17:01:13 +02:00
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-05-27 20:33:40 +02:00
"github.com/NyaaPantsu/nyaa/util/publicSettings"
2017-05-06 19:01:15 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
2017-05-25 21:54:58 +02:00
// ViewHandler : Controller for displaying a torrent
2017-05-05 16:39:15 +02:00
func ViewHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-05 16:39:15 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-20 17:01:13 +02:00
messages := msg . GetMessages ( r )
2017-05-25 21:54:58 +02:00
user := getUser ( r )
2017-05-20 17:01:13 +02:00
2017-05-23 05:18:19 +02:00
if r . URL . Query ( ) [ "success" ] != nil {
2017-05-20 17:01:13 +02:00
messages . AddInfo ( "infos" , "Torrent uploaded successfully!" )
}
2017-05-05 16:39:15 +02:00
2017-05-26 12:12:52 +02:00
torrent , err := torrentService . GetTorrentByID ( id )
2017-05-23 05:18:19 +02:00
if r . URL . Query ( ) [ "notif" ] != nil {
2017-05-21 01:06:40 +02:00
notifierService . ToggleReadNotification ( torrent . Identifier ( ) , user . ID )
}
2017-05-07 13:51:33 +02:00
if err != nil {
NotFoundHandler ( w , r )
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 := ""
if userPermission . NeedsCaptcha ( user ) {
captchaID = captcha . GetID ( )
}
2017-05-25 21:54:58 +02:00
htv := viewTemplateVariables { newCommonVariables ( r ) , b , folder , captchaID , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-05 16:39:15 +02:00
2017-05-06 19:01:15 +02:00
err = viewTemplate . ExecuteTemplate ( w , "index.html" , htv )
2017-05-05 16:39:15 +02:00
if err != nil {
2017-05-07 13:51:33 +02:00
log . Errorf ( "ViewHandler(): %s" , err )
2017-05-05 16:39:15 +02:00
}
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-05-23 05:18:19 +02:00
func ViewHeadHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-23 05:18:19 +02:00
vars := mux . Vars ( r )
id , err := strconv . ParseInt ( vars [ "id" ] , 10 , 32 )
if err != nil {
return
}
2017-05-26 12:12:52 +02:00
_ , err = torrentService . GetRawTorrentByID ( uint ( id ) )
2017-05-23 05:18:19 +02:00
if err != nil {
NotFoundHandler ( w , r )
return
}
w . Write ( nil )
}
2017-05-25 21:54:58 +02:00
// PostCommentHandler : Controller for posting a comment
2017-05-08 05:34:12 +02:00
func PostCommentHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-08 05:34:12 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-26 12:12:52 +02:00
torrent , err := torrentService . GetTorrentByID ( id )
2017-05-23 05:18:19 +02:00
if err != nil {
2017-05-22 00:22:42 +02:00
NotFoundHandler ( w , r )
return
}
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-12 11:58:22 +02:00
if userPermission . NeedsCaptcha ( currentUser ) {
userCaptcha := captcha . Extract ( r )
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-05-24 00:08:02 +02:00
content := util . Sanitize ( r . FormValue ( "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-06-22 02:21:51 +02:00
if len ( content ) > config . Conf . 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 ( ) {
userID := currentUser . ID
2017-05-29 13:25:36 +02:00
2017-05-22 00:22:42 +02:00
comment := model . Comment { TorrentID : torrent . ID , UserID : userID , Content : content , CreatedAt : time . Now ( ) }
err := db . ORM . Create ( & comment ) . Error
2017-06-14 12:10:03 +02:00
if err != nil {
messages . ImportFromError ( "errors" , err )
}
2017-05-22 00:22:42 +02:00
comment . Torrent = & torrent
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , strconv . FormatUint ( uint64 ( torrent . ID ) , 10 ) )
torrent . Uploader . ParseSettings ( )
if torrent . Uploader . Settings . Get ( "new_comment" ) {
2017-05-27 19:08:47 +02:00
T , _ , _ := publicSettings . TfuncAndLanguageWithFallback ( torrent . Uploader . Language , torrent . Uploader . Language ) // We need to send the notification to every user in their language
2017-05-22 00:22:42 +02:00
notifierService . NotifyUser ( torrent . Uploader , comment . Identifier ( ) , fmt . Sprintf ( T ( "new_comment_on_torrent" ) , torrent . Name ) , url . String ( ) , torrent . Uploader . Settings . Get ( "new_comment_email" ) )
}
2017-05-08 05:34:12 +02:00
2017-05-21 19:38:39 +02:00
if err != nil {
messages . ImportFromError ( "errors" , err )
}
2017-05-08 05:34:12 +02:00
}
2017-05-23 05:18:19 +02:00
ViewHandler ( w , r )
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-05-10 10:17:21 +02:00
func ReportTorrentHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-10 10:17:21 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-12 11:58:22 +02:00
if userPermission . NeedsCaptcha ( currentUser ) {
userCaptcha := captcha . Extract ( r )
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-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-06-14 12:10:03 +02:00
idNum , _ := strconv . Atoi ( id )
2017-05-21 19:38:39 +02:00
userID := currentUser . ID
report := model . TorrentReport {
Description : r . FormValue ( "report_type" ) ,
TorrentID : uint ( idNum ) ,
UserID : userID ,
CreatedAt : time . Now ( ) ,
}
2017-05-10 10:17:21 +02:00
2017-06-14 12:10:03 +02:00
err := db . ORM . Create ( & report ) . Error
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-05-10 10:17:21 +02:00
}
2017-05-23 05:18:19 +02:00
ViewHandler ( w , r )
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-05-23 04:05:33 +02:00
func TorrentEditUserPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-23 04:05:33 +02:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-26 12:12:52 +02:00
torrent , _ := torrentService . GetTorrentByID ( id )
2017-05-24 09:11:13 +02:00
messages := msg . GetMessages ( r )
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-23 04:05:33 +02:00
if userPermission . CurrentOrAdmin ( currentUser , torrent . UploaderID ) {
2017-06-05 15:19:25 +02:00
uploadForm := apiService . NewTorrentRequest ( )
2017-05-23 04:12:02 +02:00
uploadForm . Name = torrent . Name
uploadForm . Category = strconv . Itoa ( torrent . Category ) + "_" + strconv . Itoa ( torrent . SubCategory )
uploadForm . Remake = torrent . Status == model . TorrentStatusRemake
uploadForm . WebsiteLink = string ( torrent . WebsiteLink )
uploadForm . Description = string ( torrent . Description )
2017-05-27 20:33:40 +02:00
uploadForm . Hidden = torrent . Hidden
2017-06-12 01:14:26 +02:00
uploadForm . Language = torrent . Language
2017-05-25 21:54:58 +02:00
htv := formTemplateVariables { newCommonVariables ( r ) , uploadForm , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-23 04:05:33 +02:00
err := userTorrentEd . ExecuteTemplate ( w , "index.html" , htv )
log . CheckError ( err )
} else {
NotFoundHandler ( w , r )
}
}
2017-05-25 21:54:58 +02:00
// TorrentPostEditUserPanel : Controller for editing a user torrent by a user, after post request
2017-05-23 04:05:33 +02:00
func TorrentPostEditUserPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-06-05 15:19:25 +02:00
var uploadForm apiService . TorrentRequest
2017-05-23 04:05:33 +02:00
id := r . URL . Query ( ) . Get ( "id" )
messages := msg . GetMessages ( r )
2017-05-26 12:12:52 +02:00
torrent , _ := torrentService . GetTorrentByID ( id )
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-23 04:05:33 +02:00
if torrent . ID > 0 && userPermission . CurrentOrAdmin ( currentUser , torrent . UploaderID ) {
errUp := uploadForm . ExtractEditInfo ( r )
if errUp != nil {
messages . AddErrorT ( "errors" , "fail_torrent_update" )
}
if ! messages . HasErrors ( ) {
status := model . TorrentStatusNormal
if uploadForm . Remake { // overrides trusted
status = model . TorrentStatusRemake
} else if currentUser . IsTrusted ( ) {
status = model . TorrentStatusTrusted
}
// update some (but not all!) values
torrent . Name = uploadForm . Name
torrent . Category = uploadForm . CategoryID
torrent . SubCategory = uploadForm . SubCategoryID
torrent . Status = status
2017-05-27 20:33:40 +02:00
torrent . Hidden = uploadForm . Hidden
2017-05-23 04:05:33 +02:00
torrent . WebsiteLink = uploadForm . WebsiteLink
torrent . Description = uploadForm . Description
2017-06-12 01:14:26 +02:00
torrent . Language = uploadForm . Language
2017-05-25 02:19:05 +02:00
db . ORM . Model ( & torrent ) . UpdateColumn ( & torrent )
2017-05-23 04:05:33 +02:00
messages . AddInfoT ( "infos" , "torrent_updated" )
}
2017-05-25 21:54:58 +02:00
htv := formTemplateVariables { newCommonVariables ( r ) , uploadForm , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
err := userTorrentEd . ExecuteTemplate ( w , "index.html" , htv )
log . CheckError ( err )
2017-05-23 04:05:33 +02:00
} else {
NotFoundHandler ( w , r )
}
}
2017-05-25 21:54:58 +02:00
// TorrentDeleteUserPanel : Controller for deleting a user torrent by a user
2017-05-23 04:05:33 +02:00
func TorrentDeleteUserPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-23 04:05:33 +02:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-26 12:12:52 +02:00
torrent , _ := torrentService . GetTorrentByID ( id )
2017-05-23 04:05:33 +02:00
if userPermission . CurrentOrAdmin ( currentUser , torrent . UploaderID ) {
2017-06-15 04:44:46 +02:00
_ , _ , err := torrentService . DeleteTorrent ( id )
2017-05-24 09:11:13 +02:00
if err == nil {
2017-06-15 04:44:46 +02:00
_ , username := torrentService . HideTorrentUser ( torrent . UploaderID , torrent . Uploader . Username , torrent . Hidden )
if userPermission . HasAdmin ( currentUser ) { // We hide username on log activity if user is not admin and torrent is hidden
activity . Log ( & model . User { } , torrent . Identifier ( ) , "delete" , "torrent_deleted_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , currentUser . Username )
} else {
activity . Log ( & model . User { } , torrent . Identifier ( ) , "delete" , "torrent_deleted_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , username )
}
2017-05-23 04:05:33 +02:00
//delete reports of torrent
whereParams := serviceBase . CreateWhereParams ( "torrent_id = ?" , id )
reports , _ , _ := reportService . GetTorrentReportsOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range reports {
reportService . DeleteTorrentReport ( report . ID )
}
}
url , _ := Router . Get ( "home" ) . URL ( )
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
} else {
NotFoundHandler ( w , r )
}
2017-05-24 09:11:13 +02:00
}
2017-05-28 01:29:46 +02:00
// DownloadTorrent : Controller for downloading a torrent
func DownloadTorrent ( w http . ResponseWriter , r * http . Request ) {
defer r . Body . Close ( )
vars := mux . Vars ( r )
hash := vars [ "hash" ]
2017-05-31 04:21:57 +02:00
if hash == "" && len ( config . Conf . Torrents . FileStorage ) == 0 {
2017-05-28 01:29:46 +02:00
//File not found, send 404
http . Error ( w , "File not found." , 404 )
return
}
//Check if file exists and open
2017-05-31 04:21:57 +02:00
Openfile , err := os . Open ( fmt . Sprintf ( "%s%c%s.torrent" , config . Conf . Torrents . FileStorage , os . PathSeparator , hash ) )
2017-05-28 01:29:46 +02:00
if err != nil {
//File not found, send 404
http . Error ( w , "File not found." , 404 )
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
torrent , err := torrentService . GetRawTorrentByHash ( hash )
if err != nil {
//File not found, send 404
http . Error ( w , "File not found." , 404 )
return
}
w . Header ( ) . Set ( "Content-Disposition" , fmt . Sprintf ( "attachment; filename=\"%s.torrent\"" , torrent . Name ) )
w . Header ( ) . Set ( "Content-Type" , "application/x-bittorrent" )
w . Header ( ) . Set ( "Content-Length" , FileSize )
//Send the file
// We reset the offset to 0
Openfile . Seek ( 0 , 0 )
io . Copy ( w , Openfile ) //'Copy' the file to the client
}