Run go fmt and add swap files to gitignore
Cette révision appartient à :
Parent
480064f3a2
révision
6adce8eda9
5 fichiers modifiés avec 118 ajouts et 112 suppressions
1
.gitignore
externe
1
.gitignore
externe
|
@ -5,3 +5,4 @@ nyaa
|
||||||
nyaa.exe
|
nyaa.exe
|
||||||
nyaa-master.exe
|
nyaa-master.exe
|
||||||
*.zip
|
*.zip
|
||||||
|
*.swp
|
||||||
|
|
14
main.go
14
main.go
|
@ -40,7 +40,7 @@ func checkErr(err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func unZlib(description []byte) string {
|
func unZlib(description []byte) string {
|
||||||
if (len(description) > 0) {
|
if len(description) > 0 {
|
||||||
b := bytes.NewReader(description)
|
b := bytes.NewReader(description)
|
||||||
log.Println(b)
|
log.Println(b)
|
||||||
z, err := zlib.NewReader(b)
|
z, err := zlib.NewReader(b)
|
||||||
|
@ -49,7 +49,7 @@ func unZlib(description []byte) string {
|
||||||
p, err := ioutil.ReadAll(z)
|
p, err := ioutil.ReadAll(z)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
return string(p)
|
return string(p)
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,9 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
maxPerPage = 50 // default Value maxPerPage
|
maxPerPage = 50 // default Value maxPerPage
|
||||||
}
|
}
|
||||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||||
if (pagenum == 0) { pagenum = 1 }
|
if pagenum == 0 {
|
||||||
|
pagenum = 1
|
||||||
|
}
|
||||||
searchQuery := r.URL.Query().Get("q")
|
searchQuery := r.URL.Query().Get("q")
|
||||||
cat := r.URL.Query().Get("c")
|
cat := r.URL.Query().Get("c")
|
||||||
stat := r.URL.Query().Get("s")
|
stat := r.URL.Query().Get("s")
|
||||||
|
@ -151,7 +153,6 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
|
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
|
||||||
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, sort, order, navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, sort, order, navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
||||||
|
|
||||||
|
|
||||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -246,7 +247,9 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
nbTorrents := 0
|
nbTorrents := 0
|
||||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||||
if (pagenum == 0) { pagenum = 1 }
|
if pagenum == 0 {
|
||||||
|
pagenum = 1
|
||||||
|
}
|
||||||
|
|
||||||
b := []TorrentsJson{}
|
b := []TorrentsJson{}
|
||||||
torrents, nbTorrents := getAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
|
torrents, nbTorrents := getAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
|
||||||
|
@ -289,7 +292,6 @@ func main() {
|
||||||
router.HandleFunc("/feed.xml", rssHandler)
|
router.HandleFunc("/feed.xml", rssHandler)
|
||||||
router.HandleFunc("/view/{id}", viewHandler).Name("view_torrent")
|
router.HandleFunc("/view/{id}", viewHandler).Name("view_torrent")
|
||||||
|
|
||||||
|
|
||||||
http.Handle("/", router)
|
http.Handle("/", router)
|
||||||
|
|
||||||
// Set up server,
|
// Set up server,
|
||||||
|
|
75
models.go
75
models.go
|
@ -18,17 +18,17 @@ type Feed struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Categories struct {
|
type Categories struct {
|
||||||
Id int `gorm:"column:category_id"`
|
Id int `gorm:"column:category_id"`
|
||||||
Name string `gorm:"column:category_name"`
|
Name string `gorm:"column:category_name"`
|
||||||
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
|
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
|
||||||
Sub_Categories []Sub_Categories `gorm:"ForeignKey:parent_id;AssociationForeignKey:category_id"`
|
Sub_Categories []Sub_Categories `gorm:"ForeignKey:parent_id;AssociationForeignKey:category_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Sub_Categories struct {
|
type Sub_Categories struct {
|
||||||
Id int `gorm:"column:sub_category_id"`
|
Id int `gorm:"column:sub_category_id"`
|
||||||
Name string `gorm:"column:Sub_category_name"`
|
Name string `gorm:"column:Sub_category_name"`
|
||||||
Parent_id int `gorm:"column:parent_id"`
|
Parent_id int `gorm:"column:parent_id"`
|
||||||
Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
|
Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Statuses struct {
|
type Statuses struct {
|
||||||
|
@ -42,7 +42,7 @@ type Torrents struct {
|
||||||
Name string `gorm:"column:torrent_name"`
|
Name string `gorm:"column:torrent_name"`
|
||||||
Category_id int `gorm:"column:category_id"`
|
Category_id int `gorm:"column:category_id"`
|
||||||
Sub_category_id int `gorm:"column:sub_category_id"`
|
Sub_category_id int `gorm:"column:sub_category_id"`
|
||||||
Status int `gorm:"column:status_id"`
|
Status int `gorm:"column:status_id"`
|
||||||
Hash string `gorm:"column:torrent_hash"`
|
Hash string `gorm:"column:torrent_hash"`
|
||||||
Date int `gorm:"column:date"`
|
Date int `gorm:"column:date"`
|
||||||
Downloads int `gorm:"column:downloads"`
|
Downloads int `gorm:"column:downloads"`
|
||||||
|
@ -60,29 +60,29 @@ JSON Models Oject
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type CategoryJson struct {
|
type CategoryJson struct {
|
||||||
Id string `json: "id"`
|
Id string `json: "id"`
|
||||||
Name string `json: "category"`
|
Name string `json: "category"`
|
||||||
Torrents []TorrentsJson `json: "torrents"`
|
Torrents []TorrentsJson `json: "torrents"`
|
||||||
QueryRecordCount int `json: "queryRecordCount"`
|
QueryRecordCount int `json: "queryRecordCount"`
|
||||||
TotalRecordCount int `json: "totalRecordCount"`
|
TotalRecordCount int `json: "totalRecordCount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SubCategoryJson struct {
|
type SubCategoryJson struct {
|
||||||
Id string `json: "id"`
|
Id string `json: "id"`
|
||||||
Name string `json: "category"`
|
Name string `json: "category"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TorrentsJson struct {
|
type TorrentsJson struct {
|
||||||
Id string `json: "id"` // Is there a need to put the ID?
|
Id string `json: "id"` // Is there a need to put the ID?
|
||||||
Name string `json: "name"`
|
Name string `json: "name"`
|
||||||
Status int `json: "status"`
|
Status int `json: "status"`
|
||||||
Hash string `json: "hash"`
|
Hash string `json: "hash"`
|
||||||
Date int `json: "date"`
|
Date int `json: "date"`
|
||||||
Filesize string `json: "filesize"`
|
Filesize string `json: "filesize"`
|
||||||
Description string `json: "description"`
|
Description string `json: "description"`
|
||||||
Sub_Category SubCategoryJson `json: "sub_category"`
|
Sub_Category SubCategoryJson `json: "sub_category"`
|
||||||
Category CategoryJson `json: "category"`
|
Category CategoryJson `json: "category"`
|
||||||
Magnet template.URL `json: "magnet"`
|
Magnet template.URL `json: "magnet"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WhereParams struct {
|
type WhereParams struct {
|
||||||
|
@ -90,7 +90,6 @@ type WhereParams struct {
|
||||||
params []interface{}
|
params []interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Function to interact with Models
|
/* Function to interact with Models
|
||||||
*
|
*
|
||||||
* Get the torrents with where clause
|
* Get the torrents with where clause
|
||||||
|
@ -132,7 +131,7 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
|
||||||
var torrents []Torrents
|
var torrents []Torrents
|
||||||
var dbQuery *gorm.DB
|
var dbQuery *gorm.DB
|
||||||
var count int
|
var count int
|
||||||
if (parameters != nil) { // if there is where parameters
|
if parameters != nil { // if there is where parameters
|
||||||
db.Model(&torrents).Where(parameters.conditions, parameters.params...).Count(&count)
|
db.Model(&torrents).Where(parameters.conditions, parameters.params...).Count(&count)
|
||||||
dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...)
|
dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...)
|
||||||
} else {
|
} else {
|
||||||
|
@ -146,7 +145,7 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
|
||||||
if limit != 0 || offset != 0 { // if limits provided
|
if limit != 0 || offset != 0 { // if limits provided
|
||||||
dbQuery = dbQuery.Limit(limit).Offset(offset)
|
dbQuery = dbQuery.Limit(limit).Offset(offset)
|
||||||
}
|
}
|
||||||
dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
|
dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
|
||||||
return torrents, count
|
return torrents, count
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -208,30 +207,30 @@ func createWhereParams(conditions string, params ...string) WhereParams {
|
||||||
func (t *Torrents) toJson() TorrentsJson {
|
func (t *Torrents) toJson() TorrentsJson {
|
||||||
magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(t.Hash) + "&dn=" + t.Name + trackers
|
magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(t.Hash) + "&dn=" + t.Name + trackers
|
||||||
res := TorrentsJson{
|
res := TorrentsJson{
|
||||||
Id: strconv.Itoa(t.Id),
|
Id: strconv.Itoa(t.Id),
|
||||||
Name: html.UnescapeString(t.Name),
|
Name: html.UnescapeString(t.Name),
|
||||||
Status: t.Status,
|
Status: t.Status,
|
||||||
Hash: t.Hash,
|
Hash: t.Hash,
|
||||||
Date: t.Date,
|
Date: t.Date,
|
||||||
Filesize: t.Filesize,
|
Filesize: t.Filesize,
|
||||||
Description: unZlib(t.Description),
|
Description: unZlib(t.Description),
|
||||||
Sub_Category: t.Sub_Categories.toJson(),
|
Sub_Category: t.Sub_Categories.toJson(),
|
||||||
Category: t.Categories.toJson(),
|
Category: t.Categories.toJson(),
|
||||||
Magnet: safe(magnet)}
|
Magnet: safe(magnet)}
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Sub_Categories) toJson() SubCategoryJson {
|
func (c *Sub_Categories) toJson() SubCategoryJson {
|
||||||
return SubCategoryJson{
|
return SubCategoryJson{
|
||||||
Id: strconv.Itoa(c.Id),
|
Id: strconv.Itoa(c.Id),
|
||||||
Name: html.UnescapeString(c.Name)}
|
Name: html.UnescapeString(c.Name)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Categories) toJson() CategoryJson {
|
func (c *Categories) toJson() CategoryJson {
|
||||||
return CategoryJson{
|
return CategoryJson{
|
||||||
Id: strconv.Itoa(c.Id),
|
Id: strconv.Itoa(c.Id),
|
||||||
Name: html.UnescapeString(c.Name)}
|
Name: html.UnescapeString(c.Name)}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Complete the functions when necessary... */
|
/* Complete the functions when necessary... */
|
||||||
|
|
|
@ -1,50 +1,54 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import(
|
import (
|
||||||
"math"
|
"html/template"
|
||||||
"html/template"
|
"log"
|
||||||
"net/url"
|
"math"
|
||||||
"strconv"
|
"net/url"
|
||||||
"log"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var funcMap = template.FuncMap{
|
var funcMap = template.FuncMap{
|
||||||
"min": math.Min,
|
"min": math.Min,
|
||||||
"genRoute": func (name string, params ...string) string {
|
"genRoute": func(name string, params ...string) string {
|
||||||
url, err := router.Get(name).URL(params...)
|
url, err := router.Get(name).URL(params...)
|
||||||
if (err == nil) {
|
if err == nil {
|
||||||
return url.String()
|
return url.String()
|
||||||
}
|
}
|
||||||
return "error"
|
return "error"
|
||||||
},
|
},
|
||||||
"genNav" : func (nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML {
|
"genNav": func(nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML {
|
||||||
maxPages := math.Ceil(float64(nav.TotalItem)/float64(nav.MaxItemPerPage))
|
maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage))
|
||||||
route := router.Get(nav.Route)
|
route := router.Get(nav.Route)
|
||||||
|
|
||||||
var ret = ""
|
var ret = ""
|
||||||
if nav.CurrentPage-1 > 0 {
|
if nav.CurrentPage-1 > 0 {
|
||||||
url, _ := route.URL("page", "1")
|
url, _ := route.URL("page", "1")
|
||||||
ret = ret + "<li><a id=\"page-prev\" href=\""+ url.String() + "?" + currentUrl.RawQuery +"\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"
|
ret = ret + "<li><a id=\"page-prev\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"
|
||||||
}
|
}
|
||||||
startValue := 1
|
startValue := 1
|
||||||
if (nav.CurrentPage > pagesSelectable) {
|
if nav.CurrentPage > pagesSelectable {
|
||||||
startValue = (int(math.Min((float64(nav.CurrentPage) + math.Floor(float64(pagesSelectable)/2)), maxPages))-pagesSelectable+1)
|
startValue = (int(math.Min((float64(nav.CurrentPage)+math.Floor(float64(pagesSelectable)/2)), maxPages)) - pagesSelectable + 1)
|
||||||
}
|
}
|
||||||
endValue := (startValue+pagesSelectable-1)
|
endValue := (startValue + pagesSelectable - 1)
|
||||||
if (endValue > int(maxPages)) { endValue = int(maxPages) }
|
if endValue > int(maxPages) {
|
||||||
log.Println(nav.TotalItem)
|
endValue = int(maxPages)
|
||||||
for i := startValue; i <= endValue; i++ {
|
}
|
||||||
pageNum := strconv.Itoa(i)
|
log.Println(nav.TotalItem)
|
||||||
url,_ := route.URL("page", pageNum)
|
for i := startValue; i <= endValue; i++ {
|
||||||
ret = ret + "<li"
|
pageNum := strconv.Itoa(i)
|
||||||
if (i == nav.CurrentPage) { ret = ret + " class=\"active\"" }
|
url, _ := route.URL("page", pageNum)
|
||||||
|
ret = ret + "<li"
|
||||||
|
if i == nav.CurrentPage {
|
||||||
|
ret = ret + " class=\"active\""
|
||||||
|
}
|
||||||
|
|
||||||
ret = ret + "><a href=\""+ url.String() + "?" + currentUrl.RawQuery +"\">"+strconv.Itoa(i)+"</a></li>"
|
ret = ret + "><a href=\"" + url.String() + "?" + currentUrl.RawQuery + "\">" + strconv.Itoa(i) + "</a></li>"
|
||||||
}
|
}
|
||||||
if nav.CurrentPage < int(maxPages) {
|
if nav.CurrentPage < int(maxPages) {
|
||||||
url, _ := route.URL("page", strconv.Itoa(nav.CurrentPage+1))
|
url, _ := route.URL("page", strconv.Itoa(nav.CurrentPage+1))
|
||||||
ret = ret + "<li><a id=\"page-next\" href=\""+ url.String() + "?" + currentUrl.RawQuery +"\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"
|
ret = ret + "<li><a id=\"page-next\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"
|
||||||
}
|
}
|
||||||
return template.HTML(ret)
|
return template.HTML(ret)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +1,41 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Each Page should have an object to pass to their own template
|
/* Each Page should have an object to pass to their own template
|
||||||
* Therefore, we put them in a separate file for better maintenance
|
* Therefore, we put them in a separate file for better maintenance
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type FaqTemplateVariables struct {
|
type FaqTemplateVariables struct {
|
||||||
URL *url.URL // For parsing Url in templates
|
URL *url.URL // For parsing Url in templates
|
||||||
Route *mux.Route // For getting current route in templates
|
Route *mux.Route // For getting current route in templates
|
||||||
Query string
|
Query string
|
||||||
Status string
|
Status string
|
||||||
Category string
|
Category string
|
||||||
Sort string
|
Sort string
|
||||||
Order string
|
Order string
|
||||||
Navigation Navigation
|
Navigation Navigation
|
||||||
}
|
}
|
||||||
|
|
||||||
type HomeTemplateVariables struct {
|
type HomeTemplateVariables struct {
|
||||||
ListTorrents []TorrentsJson
|
ListTorrents []TorrentsJson
|
||||||
ListCategories []Categories
|
ListCategories []Categories
|
||||||
Query string
|
Query string
|
||||||
Status string
|
Status string
|
||||||
Category string
|
Category string
|
||||||
Sort string
|
Sort string
|
||||||
Order string
|
Order string
|
||||||
Navigation Navigation
|
Navigation Navigation
|
||||||
URL *url.URL // For parsing Url in templates
|
URL *url.URL // For parsing Url in templates
|
||||||
Route *mux.Route // For getting current route in templates
|
Route *mux.Route // For getting current route in templates
|
||||||
}
|
}
|
||||||
|
|
||||||
type Navigation struct {
|
type Navigation struct {
|
||||||
TotalItem int
|
TotalItem int
|
||||||
MaxItemPerPage int
|
MaxItemPerPage int
|
||||||
CurrentPage int
|
CurrentPage int
|
||||||
Route string
|
Route string
|
||||||
}
|
}
|
||||||
|
|
Référencer dans un nouveau ticket