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/database_dump_handler.go
Ramon Dantas 1968d2ae54 Move translation func to template variables (#652)
* Add T field to template variables

* Remove languages.SetTranslationFromRequest

* Add Tfunc on handlers

* Remove T and Ts from template_functions

* Update templates

Change the templates to use the local Tfunc, instead of the global one.
Also changed the signature of the fields on template_variables.go, so that
they return a template.HTML to avoid escaping problems.

* Remove unnecessary variable
2017-05-21 08:38:28 +10:00

60 lignes
1,5 Kio
Go

package router
import (
"os"
"path/filepath"
"net/http"
"fmt"
"time"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/util/languages"
"github.com/NyaaPantsu/nyaa/util/log"
"github.com/NyaaPantsu/nyaa/util/metainfo"
"github.com/gorilla/mux"
)
const (
DatabaseDumpPath = "./public/dumps"
GPGPublicKeyPath = "./public/gpg/gpg.key"
)
func DatabaseDumpHandler(w http.ResponseWriter, r *http.Request) {
// db params url
var err error
// TODO Use config from cli
files, err := filepath.Glob(filepath.Join(DatabaseDumpPath, "*.torrent"))
var dumpsJson []model.DatabaseDumpJSON
// TODO Filter *.torrent files
for _, f := range files {
// TODO Use config from cli
file, err := os.Open(f)
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/" + file.Name()}
dumpsJson = append(dumpsJson, dump.ToJSON())
}
// TODO Remove ?
navigationTorrents := Navigation{0, 0, 0, "search_page"}
T := languages.GetTfuncFromRequest(r)
dtv := DatabaseDumpTemplateVariables{dumpsJson, "/gpg/gpg.pub", NewSearchForm(), navigationTorrents, T, GetUser(r), r.URL, mux.CurrentRoute(r)}
err = databaseDumpTemplate.ExecuteTemplate(w, "index.html", dtv)
if err != nil {
log.Errorf("DatabaseDump(): %s", err)
}
}