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

62 lignes
1,5 Kio
Go
Brut Vue normale Historique

2017-05-11 05:06:12 +02:00
package router
import (
2017-05-24 09:11:13 +02:00
"fmt"
"net/http"
2017-05-11 05:06:12 +02:00
"os"
"path/filepath"
2017-05-11 05:06:12 +02:00
"time"
2017-05-18 03:04:18 +02:00
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/util/log"
"github.com/NyaaPantsu/nyaa/util/metainfo"
2017-05-11 05:06:12 +02:00
)
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
2017-05-11 05:06:12 +02:00
func DatabaseDumpHandler(w http.ResponseWriter, r *http.Request) {
// db params url
var err error
// TODO Use config from cli
files, _ := filepath.Glob(filepath.Join(DatabaseDumpPath, "*.torrent"))
2017-05-27 03:50:31 +02:00
defer r.Body.Close()
var dumpsJSON []model.DatabaseDumpJSON
2017-05-11 05:06:12 +02:00
// TODO Filter *.torrent files
2017-05-24 09:11:13 +02:00
for _, f := range files {
2017-05-11 05:06:12 +02:00
// TODO Use config from cli
file, err := os.Open(f)
2017-05-11 05:06:12 +02:00
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())
2017-05-24 09:11:13 +02:00
}
2017-05-11 05:06:12 +02:00
// TODO Remove ?
navigationTorrents := navigation{0, 0, 0, "search_page"}
common := newCommonVariables(r)
common.Navigation = navigationTorrents
dtv := databaseDumpTemplateVariables{common, dumpsJSON, "/gpg/gpg.pub"}
2017-05-11 05:06:12 +02:00
err = databaseDumpTemplate.ExecuteTemplate(w, "index.html", dtv)
if err != nil {
log.Errorf("DatabaseDump(): %s", err)
}
}