2017-05-05 16:39:15 +02:00
package router
2017-05-09 13:31:58 +02:00
import (
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-05-22 17:28:20 +02:00
"github.com/NyaaPantsu/nyaa/feeds"
2017-05-20 01:09:09 +02:00
userService "github.com/NyaaPantsu/nyaa/service/user"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util"
"github.com/NyaaPantsu/nyaa/util/search"
"github.com/gorilla/mux"
2017-05-16 23:32:41 +02:00
"html"
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
"net/http"
2017-05-16 23:32:41 +02:00
"strconv"
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
"time"
2017-05-05 16:39:15 +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
func RSSHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-16 23:32:41 +02:00
vars := mux . Vars ( r )
page := vars [ "page" ]
2017-05-20 01:09:09 +02:00
userID := vars [ "id" ]
2017-05-17 07:58:40 +02:00
2017-05-16 23:32:41 +02:00
var err error
pagenum := 1
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if err != nil {
util . SendError ( w , err , 400 )
return
}
2017-05-20 01:10:16 +02:00
if pagenum <= 0 {
NotFoundHandler ( w , r )
return
}
2017-05-16 23:32:41 +02:00
}
2017-05-20 01:09:09 +02:00
if userID != "" {
userIDnum , err := strconv . Atoi ( html . EscapeString ( userID ) )
// Should we have a feed for anonymous uploads?
if err != nil || userIDnum == 0 {
util . SendError ( w , err , 400 )
return
}
_ , _ , err = userService . RetrieveUserForAdmin ( userID )
if err != nil {
util . SendError ( w , err , 404 )
return
}
// Set the user ID on the request, so that SearchByQuery finds it.
query := r . URL . Query ( )
query . Set ( "userID" , userID )
r . URL . RawQuery = query . Encode ( )
}
2017-05-16 23:32:41 +02:00
_ , torrents , err := search . SearchByQueryNoCount ( r , pagenum )
2017-05-09 13:31:58 +02:00
if err != nil {
util . SendError ( w , err , 400 )
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
createdAsTime := time . Now ( )
2017-05-05 16:39:15 +02:00
if len ( torrents ) > 0 {
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
createdAsTime = torrents [ 0 ] . Date
2017-05-05 16:39:15 +02:00
}
feed := & feeds . Feed {
Title : "Nyaa Pantsu" ,
2017-05-07 15:19:36 +02:00
Link : & feeds . Link { Href : "https://" + config . WebAddress + "/" } ,
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
Created : createdAsTime ,
2017-05-05 16:39:15 +02:00
}
feed . Items = make ( [ ] * feeds . Item , len ( torrents ) )
2017-05-11 23:35:51 +02:00
for i , torrent := range torrents {
torrentJSON := torrent . ToJSON ( )
2017-05-05 16:39:15 +02:00
feed . Items [ i ] = & feeds . Item {
2017-05-12 17:54:08 +02:00
Id : "https://" + config . WebAddress + "/view/" + torrentJSON . ID ,
2017-05-11 23:35:51 +02:00
Title : torrent . Name ,
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
Link : & feeds . Link { Href : string ( torrentJSON . Magnet ) } ,
2017-05-11 15:50:26 +02:00
Description : string ( torrentJSON . Description ) ,
2017-05-11 23:35:51 +02:00
Created : torrent . Date ,
Updated : torrent . Date ,
2017-05-22 17:28:20 +02:00
Torrent : & feeds . Torrent {
FileName : torrent . Name ,
Seeds : torrent . Seeders ,
Peers : torrent . Leechers ,
InfoHash : torrent . Hash ,
ContentLength : torrent . Filesize ,
MagnetURI : string ( torrentJSON . Magnet ) ,
} ,
2017-05-05 16:39:15 +02:00
}
}
2017-05-11 23:35:51 +02:00
// allow cross domain AJAX requests
2017-05-11 15:50:26 +02:00
w . Header ( ) . Set ( "Access-Control-Allow-Origin" , "*" )
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
rss , rssErr := feed . ToRss ( )
if rssErr != nil {
2017-05-12 16:29:40 +02:00
http . Error ( w , rssErr . Error ( ) , http . StatusInternalServerError )
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
}
_ , writeErr := w . Write ( [ ] byte ( rss ) )
if writeErr != nil {
2017-05-12 16:29:40 +02:00
http . Error ( w , writeErr . Error ( ) , http . StatusInternalServerError )
2017-05-05 16:39:15 +02:00
}
2017-05-07 15:19:36 +02:00
}