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"
2017-05-11 18:30:38 +02:00
"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
)
2017-05-11 18:30:38 +02:00
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
2017-05-11 18:30:38 +02:00
files , err := filepath . Glob ( filepath . Join ( DatabaseDumpPath , "*.torrent" ) )
2017-05-11 05:06:12 +02:00
var dumpsJson [ ] model . DatabaseDumpJSON
// 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
2017-05-11 18:30:38 +02:00
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 ( ) ,
2017-05-11 18:30:38 +02:00
TorrentLink : "/dbdumps/" + file . Name ( ) }
2017-05-11 05:06:12 +02:00
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" }
2017-05-21 09:10:19 +02:00
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 )
}
}