diff --git a/main.go b/main.go index 2de73a28..2743602c 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) { @@ -108,9 +112,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) { maxPerPage = 50 // default Value maxPerPage } pagenum, _ := strconv.Atoi(html.EscapeString(page)) - if pagenum == 0 { - pagenum = 1 - } + if (pagenum == 0) { pagenum = 1 } searchQuery := r.URL.Query().Get("q") cat := r.URL.Query().Get("c") stat := r.URL.Query().Get("s") @@ -137,8 +139,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 +149,8 @@ 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 +163,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 +216,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 +225,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) } @@ -242,9 +246,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { nbTorrents := 0 pagenum, _ := strconv.Atoi(html.EscapeString(page)) - if pagenum == 0 { - pagenum = 1 - } + if (pagenum == 0) { pagenum = 1 } b := []TorrentsJson{} torrents, nbTorrents := getAllTorrents(maxPerPage, maxPerPage*(pagenum-1)) @@ -255,7 +257,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 +283,12 @@ 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..57acdd79 100644 --- a/models.go +++ b/models.go @@ -18,17 +18,17 @@ type Feed struct { } type Categories struct { - Id int `gorm:"column:category_id"` - Name string `gorm:"column:category_name"` - Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"` - Sub_Categories []Sub_Categories `gorm:"ForeignKey:parent_id;AssociationForeignKey:category_id"` + Id int `gorm:"column:category_id"` + Name string `gorm:"column:category_name"` + Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"` + Sub_Categories []Sub_Categories `gorm:"ForeignKey:parent_id;AssociationForeignKey:category_id"` } type Sub_Categories struct { - Id int `gorm:"column:sub_category_id"` - Name string `gorm:"column:Sub_category_name"` - Parent_id int `gorm:"column:parent_id"` - Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"` + Id int `gorm:"column:sub_category_id"` + Name string `gorm:"column:Sub_category_name"` + Parent_id int `gorm:"column:parent_id"` + Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"` } type Statuses struct { @@ -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"` @@ -60,29 +60,29 @@ JSON Models Oject */ type CategoryJson struct { - Id string `json: "id"` - Name string `json: "category"` + Id string `json: "id"` + Name string `json: "category"` Torrents []TorrentsJson `json: "torrents"` QueryRecordCount int `json: "queryRecordCount"` TotalRecordCount int `json: "totalRecordCount"` } type SubCategoryJson struct { - Id string `json: "id"` - Name string `json: "category"` + Id string `json: "id"` + Name string `json: "category"` } type TorrentsJson struct { - Id string `json: "id"` // Is there a need to put the ID? - Name string `json: "name"` - Status int `json: "status"` - Hash string `json: "hash"` - Date int `json: "date"` - Filesize string `json: "filesize"` - Description string `json: "description"` + Id string `json: "id"` // Is there a need to put the ID? + Name string `json: "name"` + Status int `json: "status"` + Hash string `json: "hash"` + Date int `json: "date"` + Filesize string `json: "filesize"` + Description string `json: "description"` Sub_Category SubCategoryJson `json: "sub_category"` - Category CategoryJson `json: "category"` - Magnet template.URL `json: "magnet"` + Category CategoryJson `json: "category"` + Magnet template.URL `json: "magnet"` } type WhereParams struct { @@ -90,6 +90,7 @@ type WhereParams struct { params []interface{} } + /* Function to interact with Models * * Get the torrents with where clause @@ -131,7 +132,7 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs var torrents []Torrents var dbQuery *gorm.DB 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) dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...) } else { @@ -145,8 +146,9 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs if limit != 0 || offset != 0 { // if limits provided 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 + } /* 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) } @@ -205,29 +208,30 @@ func createWhereParams(conditions string, params ...string) WhereParams { func (t *Torrents) toJson() TorrentsJson { magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(t.Hash) + "&dn=" + t.Name + trackers res := TorrentsJson{ - Id: strconv.Itoa(t.Id), - Name: html.UnescapeString(t.Name), - Status: t.Status, - Hash: t.Hash, - Date: t.Date, - Filesize: t.Filesize, - Description: unZlib(t.Description), + Id: strconv.Itoa(t.Id), + Name: html.UnescapeString(t.Name), + Status: t.Status, + Hash: t.Hash, + Date: t.Date, + Filesize: t.Filesize, + Description: unZlib(t.Description), Sub_Category: t.Sub_Categories.toJson(), - Category: t.Categories.toJson(), - Magnet: safe(magnet)} + Category: t.Categories.toJson(), + Magnet: safe(magnet)} + return res } func (c *Sub_Categories) toJson() SubCategoryJson { return SubCategoryJson{ - Id: strconv.Itoa(c.Id), - Name: html.UnescapeString(c.Name)} + Id: strconv.Itoa(c.Id), + Name: html.UnescapeString(c.Name)} } func (c *Categories) toJson() CategoryJson { return CategoryJson{ - Id: strconv.Itoa(c.Id), - Name: html.UnescapeString(c.Name)} + Id: strconv.Itoa(c.Id), + Name: html.UnescapeString(c.Name)} } /* Complete the functions when necessary... */ diff --git a/templateVariables.go b/templateVariables.go index b7e6951a..1951f9b1 100644 --- a/templateVariables.go +++ b/templateVariables.go @@ -15,6 +15,8 @@ type FaqTemplateVariables struct { Query string Status string Category string + Sort string + Order string Navigation Navigation } @@ -24,6 +26,8 @@ type HomeTemplateVariables struct { 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 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..51a2a8c3 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}}
-
- - - - - - - - -