2017-05-05 16:39:15 +02:00
package router
2017-05-06 10:36:37 +02:00
import (
"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-08 05:34:12 +02:00
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/captcha"
2017-05-06 19:01:15 +02:00
"github.com/ewhal/nyaa/service/torrent"
2017-05-09 19:23:21 +02:00
"github.com/ewhal/nyaa/util"
2017-05-08 22:43:33 +02:00
"github.com/ewhal/nyaa/util/languages"
2017-05-07 13:51:33 +02:00
"github.com/ewhal/nyaa/util/log"
2017-05-06 19:01:15 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
func ViewHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
torrent , err := torrentService . GetTorrentById ( 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-08 05:34:12 +02:00
htv := ViewTemplateVariables { b , captcha . Captcha { CaptchaID : captcha . GetID ( ) } , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-05 16:39:15 +02:00
2017-05-08 22:43:33 +02:00
languages . SetTranslationFromRequest ( viewTemplate , r , "en-us" )
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
func PostCommentHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-10 22:09:17 +02:00
if strings . TrimSpace ( r . FormValue ( "comment" ) ) == "" {
http . Error ( w , "comment empty" , 406 )
}
2017-05-08 05:34:12 +02:00
userCaptcha := captcha . Extract ( r )
if ! captcha . Authenticate ( userCaptcha ) {
http . Error ( w , "bad captcha" , 403 )
}
2017-05-08 13:18:52 +02:00
currentUser := GetUser ( r )
2017-05-08 06:58:21 +02:00
content := p . Sanitize ( r . FormValue ( "comment" ) )
2017-05-08 05:34:12 +02:00
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
idNum , err := strconv . Atoi ( id )
2017-05-10 06:02:03 +02:00
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
userID := currentUser . ID
comment := model . Comment { TorrentID : uint ( idNum ) , UserID : userID , Content : content , CreatedAt : time . Now ( ) }
2017-05-10 20:42:20 +02:00
err = db . ORM . Create ( & comment ) . Error
2017-05-09 19:23:21 +02:00
if err != nil {
util . SendError ( w , err , 500 )
return
}
2017-05-08 05:34:12 +02:00
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , id )
if err == nil {
http . Redirect ( w , r , url . String ( ) , 302 )
} else {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
}
2017-05-10 10:17:21 +02:00
func ReportTorrentHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
userCaptcha := captcha . Extract ( r )
if ! captcha . Authenticate ( userCaptcha ) {
http . Error ( w , "bad captcha" , 403 )
}
currentUser := GetUser ( r )
idNum , err := strconv . Atoi ( id )
userID := currentUser . ID
2017-05-10 20:42:20 +02:00
report := model . TorrentReport {
Description : r . FormValue ( "report_type" ) ,
TorrentID : uint ( idNum ) ,
UserID : userID ,
2017-05-11 00:10:42 +02:00
CreatedAt : time . Now ( ) ,
2017-05-10 20:42:20 +02:00
}
2017-05-10 10:17:21 +02:00
err = db . ORM . Create ( & report ) . Error
if err != nil {
util . SendError ( w , err , 500 )
return
}
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , id )
if err == nil {
http . Redirect ( w , r , url . String ( ) , 302 )
} else {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
}