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/databaseDumpHandler.go

60 lignes
1,5 Kio
Go
Brut Vue normale Historique

2017-05-11 05:06:12 +02:00
package router
import (
"os"
"path/filepath"
2017-05-11 05:06:12 +02:00
"net/http"
"fmt"
"time"
"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"
)
const (
DatabaseDumpPath = "./public/dumps"
GPGPublicKeyPath = "./public/gpg/gpg.key"
)
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, err := filepath.Glob(filepath.Join(DatabaseDumpPath, "*.torrent"))
2017-05-11 05:06:12 +02:00
var dumpsJson []model.DatabaseDumpJSON
// TODO Filter *.torrent files
for _, f := range files {
// 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()}
2017-05-11 05:06:12 +02:00
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)
}
}