2017-05-05 16:39:15 +02:00
package router
2017-05-08 11:18:49 +02:00
import (
"encoding/json"
"fmt"
2017-05-05 16:39:15 +02:00
"html"
2017-05-08 11:18:49 +02:00
"net/http"
2017-05-05 16:39:15 +02:00
"strconv"
2017-05-11 05:04:11 +02:00
"strings"
2017-05-08 11:18:49 +02:00
"time"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service"
"github.com/NyaaPantsu/nyaa/service/api"
"github.com/NyaaPantsu/nyaa/service/torrent"
2017-05-23 05:18:19 +02:00
"github.com/NyaaPantsu/nyaa/service/upload"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util"
"github.com/NyaaPantsu/nyaa/util/log"
2017-05-25 02:42:35 +02:00
"github.com/NyaaPantsu/nyaa/util/search"
2017-05-08 11:18:49 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
2017-05-25 21:54:58 +02:00
// APIHandler : Controller for api request on torrent list
func APIHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-05 16:39:15 +02:00
vars := mux . Vars ( r )
page := vars [ "page" ]
2017-05-10 20:42:20 +02:00
whereParams := serviceBase . WhereParams { }
2017-05-09 18:54:12 +02:00
req := apiService . TorrentsRequest { }
2017-05-05 16:39:15 +02:00
2017-05-09 17:09:45 +02:00
contentType := r . Header . Get ( "Content-Type" )
if contentType == "application/json" {
d := json . NewDecoder ( r . Body )
2017-05-09 18:37:40 +02:00
if err := d . Decode ( & req ) ; err != nil {
2017-05-09 17:09:45 +02:00
util . SendError ( w , err , 502 )
}
2017-05-09 18:54:12 +02:00
if req . MaxPerPage == 0 {
2017-05-19 04:55:59 +02:00
req . MaxPerPage = config . TorrentsPerPage
2017-05-09 18:54:12 +02:00
}
2017-05-20 01:10:16 +02:00
if req . Page <= 0 {
2017-05-09 18:54:12 +02:00
req . Page = 1
}
2017-05-09 18:37:40 +02:00
whereParams = req . ToParams ( )
2017-05-09 18:54:12 +02:00
} else {
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
var err error
maxString := r . URL . Query ( ) . Get ( "max" )
if maxString != "" {
req . MaxPerPage , err = strconv . Atoi ( maxString )
if ! log . CheckError ( err ) {
2017-05-19 04:55:59 +02:00
req . MaxPerPage = config . TorrentsPerPage
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
}
2017-05-19 04:55:59 +02:00
} else {
req . MaxPerPage = config . TorrentsPerPage
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
}
req . Page = 1
if page != "" {
req . Page , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-20 01:10:16 +02:00
if req . Page <= 0 {
NotFoundHandler ( w , r )
return
}
2017-05-09 18:54:12 +02:00
}
2017-05-09 17:09:45 +02:00
}
2017-05-09 18:54:12 +02:00
torrents , nbTorrents , err := torrentService . GetTorrents ( whereParams , req . MaxPerPage , req . MaxPerPage * ( req . Page - 1 ) )
2017-05-09 13:31:58 +02:00
if err != nil {
util . SendError ( w , err , 400 )
return
}
2017-05-08 11:18:49 +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
b := model . ApiResultJSON {
2017-05-09 01:56:57 +02:00
Torrents : model . TorrentsToJSON ( torrents ) ,
2017-05-05 16:39:15 +02:00
}
2017-05-09 18:54:12 +02:00
b . QueryRecordCount = req . MaxPerPage
2017-05-05 16:39:15 +02:00
b . TotalRecordCount = nbTorrents
w . Header ( ) . Set ( "Content-Type" , "application/json" )
2017-05-09 13:31:58 +02:00
err = json . NewEncoder ( w ) . Encode ( b )
2017-05-05 16:39:15 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
}
2017-05-25 21:54:58 +02:00
// APIViewHandler : Controller for viewing a torrent by its ID
func APIViewHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-05 16:39:15 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
torrent , err := torrentService . GetTorrentById ( id )
2017-05-23 05:18:25 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusNotFound )
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-05 16:39:15 +02:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
err = json . NewEncoder ( w ) . Encode ( b )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-06 23:46:53 +02:00
}
2017-05-08 11:18:49 +02:00
2017-05-25 21:54:58 +02:00
// APIViewHeadHandler : Controller for checking a torrent by its ID
func APIViewHeadHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-23 05:18:19 +02:00
vars := mux . Vars ( r )
id , err := strconv . ParseInt ( vars [ "id" ] , 10 , 32 )
if err != nil {
return
}
_ , err = torrentService . GetRawTorrentById ( uint ( id ) )
if err != nil {
NotFoundHandler ( w , r )
return
}
w . Write ( nil )
}
2017-05-25 21:54:58 +02:00
// APIUploadHandler : Controller for uploading a torrent with api
func APIUploadHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-18 15:37:44 +02:00
token := r . Header . Get ( "Authorization" )
2017-05-18 14:27:13 +02:00
user := model . User { }
2017-05-18 15:37:44 +02:00
db . ORM . Where ( "api_token = ?" , token ) . First ( & user ) //i don't like this
2017-05-20 01:10:16 +02:00
if ! uploadService . IsUploadEnabled ( user ) {
http . Error ( w , "Error uploads are disabled" , http . StatusBadRequest )
return
2017-05-08 11:18:49 +02:00
}
2017-05-20 01:10:16 +02:00
2017-05-11 05:04:11 +02:00
if user . ID == 0 {
2017-05-11 06:12:54 +02:00
http . Error ( w , apiService . ErrApiKey . Error ( ) , http . StatusUnauthorized )
2017-05-11 05:04:11 +02:00
return
}
upload := apiService . TorrentRequest { }
var filesize int64
2017-05-09 20:54:50 +02:00
contentType := r . Header . Get ( "Content-Type" )
2017-05-09 19:37:39 +02:00
if contentType == "application/json" {
defer r . Body . Close ( )
2017-05-08 11:18:49 +02:00
2017-05-09 19:37:39 +02:00
d := json . NewDecoder ( r . Body )
if err := d . Decode ( & upload ) ; err != nil {
2017-05-19 04:55:59 +02:00
decodeError := fmt . Errorf ( "Unable to decode upload data: %s" , err ) . Error ( )
http . Error ( w , decodeError , http . StatusInternalServerError )
2017-05-09 19:37:39 +02:00
return
}
2017-05-09 20:54:50 +02:00
err , code := upload . ValidateUpload ( )
2017-05-09 19:37:39 +02:00
if err != nil {
2017-05-09 19:58:35 +02:00
http . Error ( w , err . Error ( ) , code )
2017-05-09 19:37:39 +02:00
return
}
2017-05-11 05:04:11 +02:00
} else if strings . HasPrefix ( contentType , "multipart/form-data" ) {
upload . Name = r . FormValue ( "name" )
upload . Category , _ = strconv . Atoi ( r . FormValue ( "category" ) )
upload . SubCategory , _ = strconv . Atoi ( r . FormValue ( "sub_category" ) )
upload . Description = r . FormValue ( "description" )
var err error
var code int
2017-05-09 19:37:39 +02:00
2017-05-11 05:04:11 +02:00
filesize , err , code = upload . ValidateMultipartUpload ( r )
2017-05-09 23:21:15 +02:00
if err != nil {
2017-05-11 05:04:11 +02:00
http . Error ( w , err . Error ( ) , code )
2017-05-09 23:21:15 +02:00
return
}
2017-05-19 04:55:59 +02:00
} else {
// TODO What should we do here ? upload is empty so we shouldn't
// create a torrent from it
err := fmt . Errorf ( "Please provide either of Content-Type: application/json header or multipart/form-data" ) . Error ( )
http . Error ( w , err , http . StatusInternalServerError )
2017-05-25 21:54:58 +02:00
return
2017-05-09 19:37:39 +02:00
}
2017-05-11 05:25:41 +02:00
var sameTorrents int
2017-05-19 04:55:59 +02:00
2017-05-11 05:25:41 +02:00
db . ORM . Model ( & model . Torrent { } ) . Where ( "torrent_hash = ?" , upload . Hash ) . Count ( & sameTorrents )
if sameTorrents == 0 {
torrent := model . Torrent {
Name : upload . Name ,
Category : upload . Category ,
SubCategory : upload . SubCategory ,
Status : 1 ,
Hash : upload . Hash ,
Date : time . Now ( ) ,
Filesize : filesize ,
Description : upload . Description ,
UploaderID : user . ID ,
Uploader : & user ,
}
db . ORM . Create ( & torrent )
/ * if err != nil {
util . SendError ( w , err , 500 )
return
} * /
fmt . Printf ( "%+v\n" , torrent )
} else {
http . Error ( w , "torrent already exists" , http . StatusBadRequest )
2017-05-11 05:04:11 +02:00
return
2017-05-11 05:25:41 +02:00
}
2017-05-08 11:18:49 +02:00
}
2017-05-09 19:37:39 +02:00
2017-05-25 21:54:58 +02:00
// APIUpdateHandler : Controller for updating a torrent with api
2017-05-19 04:55:59 +02:00
// FIXME Impossible to update a torrent uploaded by user 0
2017-05-25 21:54:58 +02:00
func APIUpdateHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-18 15:37:44 +02:00
token := r . Header . Get ( "Authorization" )
2017-05-18 14:27:13 +02:00
user := model . User { }
2017-05-18 15:37:44 +02:00
db . ORM . Where ( "api_token = ?" , token ) . First ( & user ) //i don't like this
2017-05-20 01:10:16 +02:00
if ! uploadService . IsUploadEnabled ( user ) {
http . Error ( w , "Error uploads are disabled" , http . StatusBadRequest )
return
2017-05-09 20:54:50 +02:00
}
contentType := r . Header . Get ( "Content-Type" )
if contentType == "application/json" {
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
if user . ID == 0 {
2017-05-09 22:24:32 +02:00
http . Error ( w , apiService . ErrApiKey . Error ( ) , http . StatusForbidden )
2017-05-09 20:54:50 +02:00
return
}
defer r . Body . Close ( )
update := apiService . UpdateRequest { }
d := json . NewDecoder ( r . Body )
if err := d . Decode ( & update ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
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
id := update . ID
torrent := model . Torrent { }
2017-05-09 20:54:50 +02:00
db . ORM . Where ( "torrent_id = ?" , id ) . First ( & torrent )
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
if torrent . ID == 0 {
2017-05-09 22:24:32 +02:00
http . Error ( w , apiService . ErrTorrentId . Error ( ) , http . StatusBadRequest )
2017-05-09 20:54:50 +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
if torrent . UploaderID != 0 && torrent . UploaderID != user . ID { //&& user.Status != mod
2017-05-09 22:24:32 +02:00
http . Error ( w , apiService . ErrRights . Error ( ) , http . StatusForbidden )
2017-05-09 20:54:50 +02:00
return
}
2017-05-09 22:24:32 +02:00
2017-05-09 20:54:50 +02:00
err , code := update . Update . ValidateUpdate ( )
if err != nil {
http . Error ( w , err . Error ( ) , code )
return
}
update . UpdateTorrent ( & torrent )
2017-05-09 19:58:35 +02:00
2017-05-25 02:19:05 +02:00
db . ORM . Model ( & torrent ) . UpdateColumn ( & torrent )
2017-05-09 23:21:15 +02:00
if err != nil {
util . SendError ( w , err , 500 )
return
}
2017-05-09 20:54:50 +02:00
fmt . Printf ( "%+v\n" , torrent )
}
2017-05-09 19:58:35 +02:00
}
2017-05-25 21:54:58 +02:00
// APISearchHandler : Controller for searching with api
func APISearchHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-25 02:42:35 +02:00
vars := mux . Vars ( r )
page := vars [ "page" ]
// db params url
var err error
pagenum := 1
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
if pagenum <= 0 {
NotFoundHandler ( w , r )
return
}
}
_ , torrents , _ , err := search . SearchByQuery ( r , pagenum )
if err != nil {
util . SendError ( w , err , 400 )
return
}
b := model . TorrentsToJSON ( torrents )
w . Header ( ) . Set ( "Content-Type" , "application/json" )
err = json . NewEncoder ( w ) . Encode ( b )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
}