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
akuma06 2773fe200d Golint friendly (#747)
* Making the code Golint friendly

* No exported variables when not needed
* Same for functions
* Simplifying Templates variables with a form basic template variable
and a modelList basic template variable

* Adapted templates to new template variables

* use of .Models instead of model list
* use of .Form instead of modelform

* Small fix

* Small fix 2

Forgot $.Form

* Reverting templateDir as a var
2017-05-25 21:54:58 +02:00

60 lignes
1,5 Kio
Go

package router
import (
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/util/log"
"github.com/NyaaPantsu/nyaa/util/metainfo"
)
const (
// DatabaseDumpPath : Location of database dumps
DatabaseDumpPath = "./public/dumps"
// GPGPublicKeyPath : Location of the GPG key
GPGPublicKeyPath = "./public/gpg/gpg.key"
)
// DatabaseDumpHandler : Controller for getting database dumps
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"}
common := newCommonVariables(r)
common.Navigation = navigationTorrents
dtv := databaseDumpTemplateVariables{common, dumpsJSON, "/gpg/gpg.pub"}
err = databaseDumpTemplate.ExecuteTemplate(w, "index.html", dtv)
if err != nil {
log.Errorf("DatabaseDump(): %s", err)
}
}