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-08 11:18:49 +02:00
"time"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db"
2017-05-05 16:39:15 +02:00
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
2017-05-08 11:18:49 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
func ApiHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
page := vars [ "page" ]
2017-05-08 11:18:49 +02:00
maxPerPage , errConv := strconv . Atoi ( r . URL . Query ( ) . Get ( "max" ) )
if errConv != nil {
maxPerPage = 50 // default Value maxPerPage
}
2017-05-05 16:39:15 +02:00
nbTorrents := 0
2017-05-08 11:18:49 +02:00
pagenum , _ := strconv . Atoi ( html . EscapeString ( page ) )
if pagenum == 0 {
pagenum = 1
}
2017-05-05 16:39:15 +02:00
2017-05-08 11:18:49 +02:00
b := model . ApiResultJson { Torrents : [ ] model . TorrentsJson { } }
2017-05-05 16:39:15 +02:00
torrents , nbTorrents := torrentService . GetAllTorrents ( maxPerPage , maxPerPage * ( pagenum - 1 ) )
2017-05-08 11:18:49 +02:00
2017-05-05 16:39:15 +02:00
for i , _ := range torrents {
res := torrents [ i ] . ToJson ( )
b . Torrents = append ( b . Torrents , res )
}
b . QueryRecordCount = maxPerPage
b . TotalRecordCount = nbTorrents
w . Header ( ) . Set ( "Content-Type" , "application/json" )
err := json . NewEncoder ( w ) . Encode ( b )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
}
func ApiViewHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-06 23:46:53 +02:00
b := model . ApiResultJson { Torrents : [ ] model . TorrentsJson { } }
2017-05-05 16:39:15 +02:00
torrent , err := torrentService . GetTorrentById ( id )
res := torrent . ToJson ( )
b . Torrents = append ( b . Torrents , res )
b . QueryRecordCount = 1
b . TotalRecordCount = 1
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
func ApiUploadHandler ( w http . ResponseWriter , r * http . Request ) {
if config . UploadsDisabled == 1 {
http . Error ( w , "Error uploads are disabled" , http . StatusInternalServerError )
return
}
defer r . Body . Close ( )
//verify token
//token := r.Header.Get("Authorization")
decoder := json . NewDecoder ( r . Body )
b := model . TorrentsJson { }
err := decoder . Decode ( & b )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
category , sub_category , err := ValidateJson ( & b )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError ) //406?
return
}
torrent := model . Torrents {
Name : b . Name ,
Category : category ,
Sub_Category : sub_category ,
Status : 1 ,
Hash : b . Hash ,
Date : time . Now ( ) . Unix ( ) ,
Filesize : 0 ,
Description : string ( b . Description ) }
db . ORM . Create ( & torrent )
fmt . Printf ( "%+v\n" , torrent )
}