summaryrefslogtreecommitdiffstats
path: root/router/database_dump_handler.go
blob: 5d41bccf184376ae9340ebe16de7d58f239172ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package router

import (
	"os"
	"path/filepath"
	"net/http"
	"fmt"
	"time"

	"github.com/NyaaPantsu/nyaa/model"
	"github.com/NyaaPantsu/nyaa/util/log"
	"github.com/NyaaPantsu/nyaa/util/metainfo"
)

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"}
	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)
	}
}