2017-05-11 05:06:12 +02:00
package router
import (
"os"
2017-05-11 18:30:38 +02:00
"path/filepath"
2017-05-11 05:06:12 +02:00
"net/http"
"fmt"
"time"
2017-05-18 03:04:18 +02:00
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/util/languages"
"github.com/NyaaPantsu/nyaa/util/log"
"github.com/NyaaPantsu/nyaa/util/metainfo"
2017-05-11 05:06:12 +02:00
"github.com/gorilla/mux"
)
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
for _ , f := range files {
// 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 ( ) )
}
// TODO Remove ?
navigationTorrents := Navigation { 0 , 0 , 0 , "search_page" }
2017-05-18 02:55:23 +02:00
languages . SetTranslationFromRequest ( databaseDumpTemplate , r )
2017-05-11 05:06:12 +02:00
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 )
}
}