Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Add dumps view

Cette révision appartient à :
tomleb 2017-05-10 23:06:12 -04:00
Parent e4a244adf7
révision 44d75f506d
7 fichiers modifiés avec 173 ajouts et 3 suppressions

6
config/dumps.go Fichier normal
Voir le fichier

@ -0,0 +1,6 @@
package config
const (
DefaultDatabaseDumpPath = "./public/dumps/"
DefaultGPGPublicKeyPath = "./public/gpg/"
)

33
model/dumps.go Fichier normal
Voir le fichier

@ -0,0 +1,33 @@
package model
import (
"time"
"html/template"
"github.com/ewhal/nyaa/util"
)
type DatabaseDump struct {
Date time.Time
Filesize int64
Name string
TorrentLink string
}
type DatabaseDumpJSON struct {
Date string `json:"date"`
Filesize string `json:"filesize"`
Name string `json:"name"`
//Magnet template.URL `json:"magnet"`
TorrentLink template.URL `json:"torrent"`
}
func (dump *DatabaseDump) ToJSON() DatabaseDumpJSON {
json := DatabaseDumpJSON{
Date: dump.Date.Format(time.RFC3339),
Filesize: util.FormatFilesize2(dump.Filesize),
Name: dump.Name,
TorrentLink: template.URL(dump.TorrentLink),
}
return json
}

58
router/databaseDumpHandler.go Fichier normal
Voir le fichier

@ -0,0 +1,58 @@
package router
import (
"io/ioutil"
"os"
"net/http"
"fmt"
"time"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/log"
"github.com/ewhal/nyaa/util/metainfo"
"github.com/gorilla/mux"
)
func DatabaseDumpHandler(w http.ResponseWriter, r *http.Request) {
// db params url
var err error
// TODO Use config from cli
files, _ := ioutil.ReadDir(config.DefaultDatabaseDumpPath)
if len(files) <= 0 {
return
}
var dumpsJson []model.DatabaseDumpJSON
// TODO Filter *.torrent files
for _, f := range files {
// TODO Use config from cli
file, err := os.Open(config.DefaultDatabaseDumpPath + f.Name())
if err != nil {
continue
}
var tf metainfo.TorrentFile
err = tf.Decode(file)
if err != nil {
log.CheckError(err)
fmt.Println(err)
continue
}
dump := model.DatabaseDump{
Date: time.Now(),
Filesize: int64(tf.TotalSize()),
Name: tf.TorrentName(),
TorrentLink: "/dbdumps/" + f.Name()}
dumpsJson = append(dumpsJson, dump.ToJSON())
}
// TODO Remove ?
navigationTorrents := Navigation{0, 0, 0, "search_page"}
languages.SetTranslationFromRequest(databaseDumpTemplate, r, "en-us")
dtv := DatabaseDumpTemplateVariables{dumpsJson, "/gpg/gpg.pub", NewSearchForm(), navigationTorrents, GetUser(r), r.URL, mux.CurrentRoute(r)}
err = databaseDumpTemplate.ExecuteTemplate(w, "index.html", dtv)
if err != nil {
log.Errorf("DatabaseDump(): %s", err)
}
}

Voir le fichier

@ -15,6 +15,12 @@ func init() {
cssHandler := http.FileServer(http.Dir("./public/css/"))
jsHandler := http.FileServer(http.Dir("./public/js/"))
imgHandler := http.FileServer(http.Dir("./public/img/"))
// TODO Use config from cli
// TODO Make sure the directory exists
dumpsHandler := http.FileServer(http.Dir(config.DefaultDatabaseDumpPath))
// TODO Use config from cli
// TODO Make sure the directory exists
gpgKeyHandler := http.FileServer(http.Dir(config.DefaultGPGPublicKeyPath))
gzipHomeHandler := http.HandlerFunc(HomeHandler)
gzipAPIHandler := http.HandlerFunc(ApiHandler)
gzipAPIViewHandler := http.HandlerFunc(ApiViewHandler)
@ -22,6 +28,9 @@ func init() {
gzipUserProfileHandler := http.HandlerFunc(UserProfileHandler)
gzipUserDetailsHandler := http.HandlerFunc(UserDetailsHandler)
gzipUserProfileFormHandler := http.HandlerFunc(UserProfileFormHandler)
gzipDumpsHandler := handlers.CompressHandler(dumpsHandler)
gzipGpgKeyHandler := handlers.CompressHandler(gpgKeyHandler)
/*
// Enable GZIP compression for all handlers except imgHandler and captcha
gzipCSSHandler := cssHandler)
@ -50,10 +59,10 @@ func init() {
gzipCommentDeleteModPanel := http.HandlerFunc(CommentDeleteModPanel)
gzipTorrentDeleteModPanel := http.HandlerFunc(TorrentDeleteModPanel)
gzipTorrentReportDeleteModPanel := http.HandlerFunc(TorrentReportDeleteModPanel)*/
//gzipTorrentReportCreateHandler := http.HandlerFunc(CreateTorrentReportHandler)
//gzipTorrentReportDeleteHandler := http.HandlerFunc(DeleteTorrentReportHandler)
//gzipTorrentDeleteHandler := http.HandlerFunc(DeleteTorrentHandler)
gzipDatabaseDumpHandler := handlers.CompressHandler(http.HandlerFunc(DatabaseDumpHandler))
Router = mux.NewRouter()
@ -61,7 +70,9 @@ func init() {
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
http.Handle("/js/", http.StripPrefix("/js/", jsHandler))
http.Handle("/img/", http.StripPrefix("/img/", imgHandler))
Router.Handle("/", wrapHandler(gzipHomeHandler)).Name("home")
http.Handle("/dbdumps/", http.StripPrefix("/dbdumps/", wrapHandler(gzipDumpsHandler)))
http.Handle("/gpg/", http.StripPrefix("/gpg/", wrapHandler(gzipGpgKeyHandler)))
Router.Handle("/", gzipHomeHandler).Name("home")
Router.Handle("/page/{page:[0-9]+}", wrapHandler(gzipHomeHandler)).Name("home_page")
Router.HandleFunc("/search", SearchHandler).Name("search")
Router.HandleFunc("/search/{page}", SearchHandler).Name("search_page")
@ -110,6 +121,8 @@ func init() {
Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles)
Router.Handle("/dumps", gzipDatabaseDumpHandler).Name("dump").Methods("GET")
Router.HandleFunc("/language", SeeLanguagesHandler).Methods("GET").Name("see_languages")
Router.HandleFunc("/language", ChangeLanguageHandler).Methods("POST").Name("change_language")

Voir le fichier

@ -7,7 +7,7 @@ import (
var TemplateDir = "templates"
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate, changeLanguageTemplate *template.Template
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate, changeLanguageTemplate, databaseDumpTemplate *template.Template
var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd, panelTorrentReportList, panelTorrentReassign *template.Template
@ -21,6 +21,11 @@ type templateLoader struct {
// ReloadTemplates reloads templates on runtime
func ReloadTemplates() {
pubTempls := []templateLoader{
templateLoader{
templ: &databaseDumpTemplate,
name: "dump",
file: "dumps.html",
},
templateLoader{
templ: &homeTemplate,
name: "home",

Voir le fichier

@ -104,6 +104,16 @@ type HomeTemplateVariables struct {
Route *mux.Route // For getting current route in templates
}
type DatabaseDumpTemplateVariables struct {
ListDumps []model.DatabaseDumpJSON
GPGLink string
Search SearchForm
Navigation Navigation
User *model.User
URL *url.URL // For parsing Url in templates
Route *mux.Route // For getting current route in templates
}
type UploadTemplateVariables struct {
Upload UploadForm
Search SearchForm

45
templates/dumps.html Fichier normal
Voir le fichier

@ -0,0 +1,45 @@
{{define "title"}}{{T "home"}}{{end}}
{{define "contclass"}}cont-home{{end}}
{{define "content"}}
<div class="blockBody">
<ul class="list-inline" aria-label="Page navigation">
<li><img id="mascot" src="/img/renchon.png" /></li>
<li style="padding-top: 7%;" class="pull-right"><ul class="pagination">
<a href="{{ .GPGLink }}">Gpg key</a>
</ul></li>
</nav>
<div class="table-responsive">
<table class="table custom-table-hover">
<tr>
<th class="col-xs-8">{{T "name"}}</th>
<th class="col-xs-1 hidden-xs">{{T "date"}}</th>
<th class="col-xs-1 hidden-xs">{{T "size"}}</th>
<th class="col-xs-1 hidden-xs">{{T "links"}}</th>
</tr>
{{ range .ListDumps}}
<tr class="torrent-info">
<!-- forced width because the <td> gets bigger randomly otherwise -->
<td class="name">
{{.Name}}
</td>
<td class="hidden-xs date date-short">{{.Date}}</td>
<td class="hidden-xs filesize">{{.Filesize}}</td>
<td class="hidden-xs">
{{if ne .TorrentLink ""}}
<a href="{{.TorrentLink}}" title="Torrent file">
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span>
</a>
{{end}}
</td>
</tr>
{{end}}
</table>
</div>
<nav class="torrentNav" aria-label="Page navigation">
<ul class="pagination">
{{ genNav .Navigation .URL 5 }}
</ul>
</nav>
</div>
{{end}}