Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/rss_handler.go
akuma06 5376b9e271 New config files (#854)
* New config files

As decided, config files are parsed at runtime.
I decided to go for YAML config files because there can be comments in
it.
There are 2 files:
* config/default_config.yml <= which shouldn't be edited unless we add a
config parameter
* config/config.yml <= which is the user-defined config. This file
shouldn't be commited

Changed every call to config.XXX to config.Conf.XXX (look to the new
stucture of config in config/types.go)

Of course, putting config parameters in config.yml overrides config in
config_default.yml. You don't have to put everything in it, just add
what you want to override.

* Fixing test

Replacing conf.New by config.Conf

* Fixing call to config.Conf to config.Config{} in test files

* Might have fixed testing with this

Printf instead of Fatalf

* Renaming config.yml in example file

* Forbid commiting config.yml

* Should be now fixed

* Do not need this file anymore
2017-05-30 21:21:57 -05:00

105 lignes
2,6 Kio
Go

package router
import (
"html"
"net/http"
"strconv"
"time"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/feeds"
userService "github.com/NyaaPantsu/nyaa/service/user"
"github.com/NyaaPantsu/nyaa/util"
"github.com/NyaaPantsu/nyaa/util/search"
"github.com/gorilla/mux"
)
// RSSHandler : Controller for displaying rss feed, accepting common search arguments
func RSSHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
vars := mux.Vars(r)
page := vars["page"]
userID := vars["id"]
var err error
pagenum := 1
if page != "" {
pagenum, err = strconv.Atoi(html.EscapeString(page))
if err != nil {
util.SendError(w, err, 400)
return
}
if pagenum <= 0 {
NotFoundHandler(w, r)
return
}
}
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()
}
_, torrents, err := search.SearchByQueryNoCount(r, pagenum)
if err != nil {
util.SendError(w, err, 400)
return
}
createdAsTime := time.Now()
if len(torrents) > 0 {
createdAsTime = torrents[0].Date
}
feed := &feeds.Feed{
Title: "Nyaa Pantsu",
Link: &feeds.Link{Href: "https://" + config.Conf.WebAddress + "/"},
Created: createdAsTime,
}
feed.Items = make([]*feeds.Item, len(torrents))
for i, torrent := range torrents {
torrentJSON := torrent.ToJSON()
feed.Items[i] = &feeds.Item{
ID: "https://" + config.Conf.WebAddress + "/view/" + strconv.FormatUint(uint64(torrentJSON.ID), 10),
Title: torrent.Name,
Link: &feeds.Link{Href: string("<![CDATA[" + torrentJSON.Magnet + "]]>")},
Description: string(torrentJSON.Description),
Created: torrent.Date,
Updated: torrent.Date,
Torrent: &feeds.Torrent{
FileName: torrent.Name,
Seeds: torrent.Seeders,
Peers: torrent.Leechers,
InfoHash: torrent.Hash,
ContentLength: torrent.Filesize,
MagnetURI: string(torrentJSON.Magnet),
},
}
}
// allow cross domain AJAX requests
w.Header().Set("Access-Control-Allow-Origin", "*")
rss, rssErr := feed.ToRss()
if rssErr != nil {
http.Error(w, rssErr.Error(), http.StatusInternalServerError)
}
_, writeErr := w.Write([]byte(rss))
if writeErr != nil {
http.Error(w, writeErr.Error(), http.StatusInternalServerError)
}
}