diff --git a/.gitignore b/.gitignore index 8248a38a..804bde0c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ nyaa nyaa.exe nyaa-master.exe *.zip +*.swp diff --git a/css/style.css b/css/style.css index ecf5dd83..9772088c 100644 --- a/css/style.css +++ b/css/style.css @@ -21,7 +21,7 @@ nav#mainmenu a { color: white; } div#container { - padding-top: 7rem; + padding-top: 10rem; } body { diff --git a/main.go b/main.go index 2de73a28..46c62ccb 100644 --- a/main.go +++ b/main.go @@ -40,13 +40,17 @@ func checkErr(err error) { } func unZlib(description []byte) string { - b := bytes.NewReader(description) - z, err := zlib.NewReader(b) - checkErr(err) - defer z.Close() - p, err := ioutil.ReadAll(z) - checkErr(err) - return string(p) + if len(description) > 0 { + b := bytes.NewReader(description) + log.Println(b) + z, err := zlib.NewReader(b) + checkErr(err) + defer z.Close() + p, err := ioutil.ReadAll(z) + checkErr(err) + return string(p) + } + return "" } func apiHandler(w http.ResponseWriter, r *http.Request) { @@ -137,8 +141,9 @@ func searchHandler(w http.ResponseWriter, r *http.Request) { b := []TorrentsJson{} - torrents, nbTorrents := getTorrents(createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?", - "%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%"), maxPerPage, maxPerPage*(pagenum-1)) + parameters := createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?", + "%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%") + torrents, nbTorrents := getTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1)) for i, _ := range torrents { res := torrents[i].toJson() @@ -146,7 +151,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) { } navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} - htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, 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) if err != nil { @@ -159,7 +164,7 @@ func safe(s string) template.URL { func faqHandler(w http.ResponseWriter, r *http.Request) { var templates = template.Must(template.New("FAQ").Funcs(funcMap).ParseFiles("templates/index.html", "templates/FAQ.html")) - err := templates.ExecuteTemplate(w, "index.html", FaqTemplateVariables{r.URL, mux.CurrentRoute(r), "", "", "", Navigation{}}) + err := templates.ExecuteTemplate(w, "index.html", FaqTemplateVariables{r.URL, mux.CurrentRoute(r), "", "", "_", "torrent_id", "desc", Navigation{}}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -212,7 +217,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) { } } func viewHandler(w http.ResponseWriter, r *http.Request) { - + var templates = template.Must(template.ParseFiles("templates/index.html", "templates/view.html")) vars := mux.Vars(r) id := vars["id"] b := []TorrentsJson{} @@ -221,9 +226,9 @@ func viewHandler(w http.ResponseWriter, r *http.Request) { res := torrent.toJson() b = append(b, res) - htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", "", "", 1, 1} + htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", "", "", Navigation{}, r.URL, mux.CurrentRoute(r)} - err = templates.ExecuteTemplate(w, "view.html", htv) + err = templates.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -255,7 +260,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { } navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} - htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", navigationTorrents, r.URL, mux.CurrentRoute(r)} + htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", "torrent_id", "desc", navigationTorrents, r.URL, mux.CurrentRoute(r)} err := templates.ExecuteTemplate(w, "index.html", htv) if err != nil { @@ -281,9 +286,11 @@ func main() { router.HandleFunc("/page/{page:[0-9]+}", rootHandler).Name("home_page") router.HandleFunc("/search", searchHandler).Name("search") router.HandleFunc("/search/{page}", searchHandler).Name("search_page") - router.HandleFunc("/api/{page:[0-9]+}", apiHandler).Methods("GET") - router.HandleFunc("/api/torrent/{id:[0-9]+}", singleapiHandler).Methods("GET") + router.HandleFunc("/api/{page}", apiHandler).Methods("GET") + router.HandleFunc("/api/view/{id}", apiViewHandler).Methods("GET") router.HandleFunc("/faq", faqHandler).Name("faq") + router.HandleFunc("/feed.xml", rssHandler) + router.HandleFunc("/view/{id}", viewHandler).Name("view_torrent") http.Handle("/", router) diff --git a/models.go b/models.go index 5ee94005..1a0381bb 100644 --- a/models.go +++ b/models.go @@ -42,7 +42,7 @@ type Torrents struct { Name string `gorm:"column:torrent_name"` Category_id int `gorm:"column:category_id"` Sub_category_id int `gorm:"column:sub_category_id"` - Status_id int `gorm:"column:status_id"` + Status int `gorm:"column:status_id"` Hash string `gorm:"column:torrent_hash"` Date int `gorm:"column:date"` Downloads int `gorm:"column:downloads"` @@ -131,13 +131,14 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs var torrents []Torrents var dbQuery *gorm.DB var count int + conditions := "torrent_hash is not null" //filter out broken entries + var params []interface{} if parameters != nil { // if there is where parameters - db.Model(&torrents).Where(parameters.conditions, parameters.params...).Count(&count) - dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...) - } else { - db.Model(&torrents).Count(&count) - dbQuery = db.Model(&torrents) + conditions += " AND " + parameters.conditions + params = parameters.params } + db.Model(&torrents).Where(conditions, params...).Count(&count) + dbQuery = db.Model(&torrents).Where(conditions, params...) if orderBy == "" { orderBy = "torrent_id DESC" @@ -147,6 +148,7 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs } dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents) return torrents, count + } /* Functions to simplify the get parameters of the main function @@ -167,6 +169,7 @@ func getTorrentsDB(parameters WhereParams) ([]Torrents, int) { */ func getAllTorrentsOrderBy(orderBy string, limit int, offset int) ([]Torrents, int) { + return getTorrentsOrderBy(nil, orderBy, limit, offset) } @@ -215,6 +218,7 @@ func (t *Torrents) toJson() TorrentsJson { Sub_Category: t.Sub_Categories.toJson(), Category: t.Categories.toJson(), Magnet: safe(magnet)} + return res } diff --git a/templateFunctions.go b/templateFunctions.go index 284de4d7..b3d2a563 100644 --- a/templateFunctions.go +++ b/templateFunctions.go @@ -1,50 +1,54 @@ package main -import( - "math" - "html/template" - "net/url" - "strconv" - "log" +import ( + "html/template" + "log" + "math" + "net/url" + "strconv" ) var funcMap = template.FuncMap{ - "min": math.Min, - "genRoute": func (name string, params ...string) string { - url, err := router.Get(name).URL(params...) - if (err == nil) { - return url.String() - } - return "error" - }, - "genNav" : func (nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML { - maxPages := math.Ceil(float64(nav.TotalItem)/float64(nav.MaxItemPerPage)) - route := router.Get(nav.Route) + "min": math.Min, + "genRoute": func(name string, params ...string) string { + url, err := router.Get(name).URL(params...) + if err == nil { + return url.String() + } + return "error" + }, + "genNav": func(nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML { + maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage)) + route := router.Get(nav.Route) - var ret = "" - if nav.CurrentPage-1 > 0 { - url, _ := route.URL("page", "1") - ret = ret + "
  • «
  • " - } - startValue := 1 - if (nav.CurrentPage > pagesSelectable) { - startValue = (int(math.Min((float64(nav.CurrentPage) + math.Floor(float64(pagesSelectable)/2)), maxPages))-pagesSelectable+1) - } - endValue := (startValue+pagesSelectable-1) - if (endValue > int(maxPages)) { endValue = int(maxPages) } - log.Println(nav.TotalItem) - for i := startValue; i <= endValue; i++ { - pageNum := strconv.Itoa(i) - url,_ := route.URL("page", pageNum) - ret = ret + " 0 { + url, _ := route.URL("page", "1") + ret = ret + "
  • «
  • " + } + startValue := 1 + if nav.CurrentPage > pagesSelectable { + startValue = (int(math.Min((float64(nav.CurrentPage)+math.Floor(float64(pagesSelectable)/2)), maxPages)) - pagesSelectable + 1) + } + endValue := (startValue + pagesSelectable - 1) + if endValue > int(maxPages) { + endValue = int(maxPages) + } + log.Println(nav.TotalItem) + for i := startValue; i <= endValue; i++ { + pageNum := strconv.Itoa(i) + url, _ := route.URL("page", pageNum) + ret = ret + ""+strconv.Itoa(i)+"" - } - if nav.CurrentPage < int(maxPages) { - url, _ := route.URL("page", strconv.Itoa(nav.CurrentPage+1)) - ret = ret + "
  • »
  • " - } - return template.HTML(ret) - }, - } \ No newline at end of file + ret = ret + ">" + strconv.Itoa(i) + "" + } + if nav.CurrentPage < int(maxPages) { + url, _ := route.URL("page", strconv.Itoa(nav.CurrentPage+1)) + ret = ret + "
  • »
  • " + } + return template.HTML(ret) + }, +} diff --git a/templateVariables.go b/templateVariables.go index b7e6951a..6ace36bd 100644 --- a/templateVariables.go +++ b/templateVariables.go @@ -1,37 +1,41 @@ package main import ( - "net/url" "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 -*/ + */ type FaqTemplateVariables struct { - URL *url.URL // For parsing Url in templates - Route *mux.Route // For getting current route in templates - Query string - Status string - Category string - Navigation Navigation + URL *url.URL // For parsing Url in templates + Route *mux.Route // For getting current route in templates + Query string + Status string + Category string + Sort string + Order string + Navigation Navigation } type HomeTemplateVariables struct { - ListTorrents []TorrentsJson - ListCategories []Categories - Query string - Status string - Category string - Navigation Navigation - URL *url.URL // For parsing Url in templates - Route *mux.Route // For getting current route in templates + ListTorrents []TorrentsJson + ListCategories []Categories + Query string + Status string + Category string + Sort string + Order string + Navigation Navigation + URL *url.URL // For parsing Url in templates + Route *mux.Route // For getting current route in templates } type Navigation struct { - TotalItem int - MaxItemPerPage int - CurrentPage int - Route string + TotalItem int + MaxItemPerPage int + CurrentPage int + Route string } diff --git a/templates/home.html b/templates/home.html index 7783ebe9..bab2bf8d 100644 --- a/templates/home.html +++ b/templates/home.html @@ -6,6 +6,8 @@ Id Name Hash + Date + Size Links {{ range .ListTorrents}} @@ -13,10 +15,13 @@ {{if eq .Status 3}}class="trusted"{{end}} {{if eq .Status 4}}class="aplus"{{end}}> - {{.Name}} + {{.Name}} {{.Hash}} + {{.Date}} + {{.Filesize}} + diff --git a/templates/index.html b/templates/index.html index bcf86f85..4fa631ba 100644 --- a/templates/index.html +++ b/templates/index.html @@ -35,10 +35,9 @@ + + + +
    +
    +

    Advanced Search

    + -
    -
    - - -
    - - {{ range .ListTorrents}} - - - - - - - - - - - - - - - - - - - - - tr - - - - - - - - - {{end}} - -
    Id{{.Id}}
    Name{{.Name}}
    Hash{{.Hash}}
    Date{{.Date}}
    FileSize{{.Filesize}}
    Links - -
    Description{{.Description}}
    -
    - - - - - - - - -