From 22e1c7e9a72e5c3796089745708624503c027f54 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 16:45:23 +0100 Subject: [PATCH 01/45] Update stats.go --- controllers/torrent/stats.go | 85 ++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index e0b036f9c..ee787d435 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -1,6 +1,7 @@ package torrentController import ( + "encoding/hex" "strconv" "strings" "net/url" @@ -9,10 +10,33 @@ import ( "github.com/NyaaPantsu/nyaa/models/torrents" "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/config" + "github.com/NyaaPantsu/nyaa/utils/log" + "github.com/NyaaPantsu/nyaa/utils/format" "github.com/Stephen304/goscrape" "github.com/gin-gonic/gin" + + "github.com/anacrolix/dht" + "github.com/anacrolix/torrent" ) +var client *torrent.Client + +func initClient() error { + clientConfig := torrent.Config{ + DHTConfig: dht.ServerConfig{ + StartingNodes: dht.GlobalBootstrapAddrs, + }, + ListenAddr: ":5977", + } + cl, err := torrent.NewClient(&clientConfig) + if err != nil { + log.Errorf("error creating client: %s", err) + return err + } + client = cl + return nil +} + // ViewHeadHandler : Controller for getting torrent stats func GetStatsHandler(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 32) @@ -42,13 +66,13 @@ func GetStatsHandler(c *gin.Context) { } var Trackers []string - if len(Trackers) > 3 { + if len(torrent.Trackers) > 3 { for _, line := range strings.Split(torrent.Trackers[3:], "&tr=") { tracker, error := url.QueryUnescape(line) - if error == nil && strings.Contains(tracker, "udp://") { + if error == nil && strings.HasPrefix(tracker, "udp") { Trackers = append(Trackers, tracker) } - //Cannot scrape from http trackers so don't put them in the array + //Cannot scrape from http trackers only keep UDP ones } } @@ -57,7 +81,15 @@ func GetStatsHandler(c *gin.Context) { Trackers = append(Trackers, line) } } + + err = ScrapeFiles(format.InfoHashToMagnet(strings.TrimSpace(torrent.Hash), torrent.Name, Trackers...), torrent) + + if err != nil { + return + } + return + stats := goscrape.Single(Trackers, []string{ torrent.Hash, })[0] @@ -96,6 +128,53 @@ func GetStatsHandler(c *gin.Context) { return } +type TStruct struct { + Peers goscrape.Result + Trackers []string + //Files []metainfo.FileInfo + Files []torrent.File + Magnet string +} + +func ScrapeFiles(magnet string, torrent models.Torrent) error { + if client == nil { + err := initClient() + if err != nil { + return err + } + } + + t, _ := client.AddMagnet(magnet) + <-t.GotInfo() + + infoHash := t.InfoHash() + dst := make([]byte, hex.EncodedLen(len(t.InfoHash()))) + hex.Encode(dst, infoHash[:]) + + var UDP []string + + for _, tracker := range t.Metainfo().AnnounceList[0] { + if strings.HasPrefix(tracker, "udp") { + UDP = append(UDP, tracker) + } + } + if len(UDP) != 0 { + udpscrape := goscrape.NewBulk(UDP) + results := udpscrape.ScrapeBulk([]string{torrent.Hash})[0] + if results.Btih != "0" { + torrent.Scrape = &models.Scrape{torrent.ID, uint32(results.Seeders), uint32(results.Leechers),uint32(results.Completed), time.Now()} + } + } + torrent.FileList = []models.File{} + for i, file := range t.Files() { + log.Errorf("----- File %d / Path %s / Length %d", i, file.DisplayPath(), file.Length()) + torrent.FileList = append(torrent.FileList, models.File{uint(i), torrent.ID, file.DisplayPath(), file.Length()}) + } + torrent.Update(true) + t.Drop() + return nil +} + func contains(s []string, e string) bool { for _, a := range s { if a == e { From f226f1a1ca0a4ed3d5759735aec71179bb4a9977 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 17:39:29 +0100 Subject: [PATCH 02/45] Update stats.go --- controllers/torrent/stats.go | 114 +++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 52 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index ee787d435..e896e9cd3 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -44,7 +44,7 @@ func GetStatsHandler(c *gin.Context) { return } - torrent, err := torrents.FindRawByID(uint(id)) + updateTorrent, err := torrents.FindRawByID(uint(id)) if err != nil { return @@ -55,19 +55,19 @@ func GetStatsHandler(c *gin.Context) { if statsExists { //Stats already exist, we check if the torrent stats have been scraped already very recently and if so, we stop there to avoid abuse of the /stats/:id route - if (CurrentData.Seeders == 0 && CurrentData.Leechers == 0 && CurrentData.Completed == 0) && time.Since(CurrentData.LastScrape).Minutes() <= config.Get().Scrape.MaxStatScrapingFrequencyUnknown { + if isEmptyScrape(CurrentData) && time.Since(CurrentData.LastScrape).Minutes() <= config.Get().Scrape.MaxStatScrapingFrequencyUnknown { //Unknown stats but has been scraped less than X minutes ago (X being the limit set in the config file) return } - if (CurrentData.Seeders != 0 || CurrentData.Leechers != 0 || CurrentData.Completed != 0) && time.Since(CurrentData.LastScrape).Minutes() <= config.Get().Scrape.MaxStatScrapingFrequency { + if !isEmptyScrape(CurrentData) && time.Since(CurrentData.LastScrape).Minutes() <= config.Get().Scrape.MaxStatScrapingFrequency { //Known stats but has been scraped less than X minutes ago (X being the limit set in the config file) return } } var Trackers []string - if len(torrent.Trackers) > 3 { - for _, line := range strings.Split(torrent.Trackers[3:], "&tr=") { + if len(updateTorrent.Trackers) > 3 { + for _, line := range strings.Split(updateTorrent.Trackers[3:], "&tr=") { tracker, error := url.QueryUnescape(line) if error == nil && strings.HasPrefix(tracker, "udp") { Trackers = append(Trackers, tracker) @@ -82,21 +82,25 @@ func GetStatsHandler(c *gin.Context) { } } - err = ScrapeFiles(format.InfoHashToMagnet(strings.TrimSpace(torrent.Hash), torrent.Name, Trackers...), torrent) + var stats goscrape.Result + var torrentFiles []models.File - if err != nil { - return + if c.Request.URL.Query()["files"] != nil { + err, torrentFiles = ScrapeFiles(format.InfoHashToMagnet(strings.TrimSpace(updateTorrent.Hash), updateTorrent.Name, Trackers...), updateTorrent, CurrentData, statsExists) + if err != nil { + return + } + } else { + //Single() returns an array which contain results for each torrent Hash it is fed, since we only feed him one we want to directly access the results + stats = goscrape.Single(Trackers, []string{ + updateTorrent.Hash, + })[0] + UpdateTorrentStats(updateTorrent, stats, CurrentData, []torrent.File{}, statsExists) } - - return - stats := goscrape.Single(Trackers, []string{ - torrent.Hash, - })[0] - //Single() returns an array which contain results for each torrent Hash it is fed, since we only feed him one we want to directly access the results //If we put seeders on -1, the script instantly knows the fetching did not give any result, avoiding having to check all three stats below and in view.jet.html's javascript - if stats.Seeders == 0 && stats.Leechers == 0 && stats.Completed == 0 { + if isEmptyResult(stats) { stats.Seeders = -1 } @@ -104,43 +108,17 @@ func GetStatsHandler(c *gin.Context) { "seeders": stats.Seeders, "leechers": stats.Leechers, "downloads": stats.Completed, + "filelist": torrentFiles, }) - if stats.Seeders == -1 { - stats.Seeders = 0 - } - - if !statsExists { - torrent.Scrape = torrent.Scrape.Create(uint(id), uint32(stats.Seeders), uint32(stats.Leechers), uint32(stats.Completed), time.Now()) - //Create entry in the DB because none exist - } else { - //Entry in the DB already exists, simply update it - if (CurrentData.Seeders == 0 && CurrentData.Leechers == 0 && CurrentData.Completed == 0) || (stats.Seeders != 0 && stats.Leechers != 0 && stats.Completed != 0 ) { - torrent.Scrape = &models.Scrape{uint(id), uint32(stats.Seeders), uint32(stats.Leechers), uint32(stats.Completed), time.Now()} - } else { - torrent.Scrape = &models.Scrape{uint(id), uint32(CurrentData.Seeders), uint32(CurrentData.Leechers), uint32(CurrentData.Completed), time.Now()} - } - //Only overwrite stats if the old one are Unknown OR if the current ones are not unknown, preventing good stats from being turned into unknown own but allowing good stats to be updated to more reliable ones - torrent.Scrape.Update(false) - - } - return } -type TStruct struct { - Peers goscrape.Result - Trackers []string - //Files []metainfo.FileInfo - Files []torrent.File - Magnet string -} - -func ScrapeFiles(magnet string, torrent models.Torrent) error { +func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scrape, statsExists bool) (error, []models.File) { if client == nil { err := initClient() if err != nil { - return err + return err, []models.File{} } } @@ -158,21 +136,53 @@ func ScrapeFiles(magnet string, torrent models.Torrent) error { UDP = append(UDP, tracker) } } + var results goscrape.Result if len(UDP) != 0 { udpscrape := goscrape.NewBulk(UDP) - results := udpscrape.ScrapeBulk([]string{torrent.Hash})[0] + results = udpscrape.ScrapeBulk([]string{torrent.Hash})[0] if results.Btih != "0" { torrent.Scrape = &models.Scrape{torrent.ID, uint32(results.Seeders), uint32(results.Leechers),uint32(results.Completed), time.Now()} } } - torrent.FileList = []models.File{} - for i, file := range t.Files() { - log.Errorf("----- File %d / Path %s / Length %d", i, file.DisplayPath(), file.Length()) - torrent.FileList = append(torrent.FileList, models.File{uint(i), torrent.ID, file.DisplayPath(), file.Length()}) - } - torrent.Update(true) t.Drop() - return nil + return nil, UpdateTorrentStats(torrent, results, currentStats, t.Files(), statsExists) +} + +// UpdateTorrentStats : Update stats & filelist if files are specified, otherwise just stats +func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentStats models.Scrape, Files []torrent.File, statsExists bool) []models.File { + if stats.Seeders == -1 { + stats.Seeders = 0 + } + if !statsExists { + torrent.Scrape = torrent.Scrape.Create(torrent.ID, uint32(stats.Seeders), uint32(stats.Leechers), uint32(stats.Completed), time.Now()) + //Create a stat entry in the DB because none exist + } else { + //Entry in the DB already exists, simply update it + if isEmptyScrape(currentStats) || !isEmptyResult(stats) { + torrent.Scrape = &models.Scrape{torrent.ID, uint32(stats.Seeders), uint32(stats.Leechers), uint32(stats.Completed), time.Now()} + } else { + torrent.Scrape = &models.Scrape{torrent.ID, uint32(currentStats.Seeders), uint32(currentStats.Leechers), uint32(currentStats.Completed), time.Now()} + } + //Only overwrite stats if the old one are Unknown OR if the new ones are not unknown, preventing good stats from being turned into unknown but allowing good stats to be updated to more reliable ones + torrent.Scrape.Update(false) + } + if len(Files) > 0 { + torrent.FileList = []models.File{} + for i, file := range Files { + log.Errorf("----- File %d / Path %s / Length %d", i, file.DisplayPath(), file.Length()) + torrent.FileList = append(torrent.FileList, models.File{uint(i), torrent.ID, file.DisplayPath(), file.Length()}) + } + torrent.Update(true) + } + return []models.File{} +} + +func isEmptyResult(stats goscrape.Result) bool { + return stats.Seeders == 0 && stats.Leechers == 0 && stats.Completed == 0 +} + +func isEmptyScrape(stats models.Scrape) bool { + return stats.Seeders == 0 && stats.Leechers == 0 && stats.Completed == 0 } func contains(s []string, e string) bool { From 89fd690e6996435b74fe4e6a9f2506e0ab169934 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 17:50:05 +0100 Subject: [PATCH 03/45] Update stats.go --- controllers/torrent/stats.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index e896e9cd3..718b11365 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -83,7 +83,7 @@ func GetStatsHandler(c *gin.Context) { } var stats goscrape.Result - var torrentFiles []models.File + var torrentFiles []models.FileJSON if c.Request.URL.Query()["files"] != nil { err, torrentFiles = ScrapeFiles(format.InfoHashToMagnet(strings.TrimSpace(updateTorrent.Hash), updateTorrent.Name, Trackers...), updateTorrent, CurrentData, statsExists) @@ -114,11 +114,11 @@ func GetStatsHandler(c *gin.Context) { return } -func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scrape, statsExists bool) (error, []models.File) { +func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scrape, statsExists bool) (error, []models.FileJSON) { if client == nil { err := initClient() if err != nil { - return err, []models.File{} + return err, []models.FileJSON{} } } @@ -140,19 +140,17 @@ func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scra if len(UDP) != 0 { udpscrape := goscrape.NewBulk(UDP) results = udpscrape.ScrapeBulk([]string{torrent.Hash})[0] - if results.Btih != "0" { - torrent.Scrape = &models.Scrape{torrent.ID, uint32(results.Seeders), uint32(results.Leechers),uint32(results.Completed), time.Now()} - } } t.Drop() return nil, UpdateTorrentStats(torrent, results, currentStats, t.Files(), statsExists) } // UpdateTorrentStats : Update stats & filelist if files are specified, otherwise just stats -func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentStats models.Scrape, Files []torrent.File, statsExists bool) []models.File { +func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentStats models.Scrape, Files []torrent.File, statsExists bool) (JSONFilelist []models.FileJSON) { if stats.Seeders == -1 { stats.Seeders = 0 } + if !statsExists { torrent.Scrape = torrent.Scrape.Create(torrent.ID, uint32(stats.Seeders), uint32(stats.Leechers), uint32(stats.Completed), time.Now()) //Create a stat entry in the DB because none exist @@ -166,15 +164,17 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt //Only overwrite stats if the old one are Unknown OR if the new ones are not unknown, preventing good stats from being turned into unknown but allowing good stats to be updated to more reliable ones torrent.Scrape.Update(false) } + if len(Files) > 0 { torrent.FileList = []models.File{} for i, file := range Files { - log.Errorf("----- File %d / Path %s / Length %d", i, file.DisplayPath(), file.Length()) torrent.FileList = append(torrent.FileList, models.File{uint(i), torrent.ID, file.DisplayPath(), file.Length()}) + JSONFilelist = append(JSONFilelist, models.FileJSON{file.DisplayPath(), file.Length()}) } torrent.Update(true) } - return []models.File{} + + return } func isEmptyResult(stats goscrape.Result) bool { From 58c6d0983e8d5667157119000b49e6a0c94a7b43 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 18:33:39 +0100 Subject: [PATCH 04/45] add loading_file_list --- translations/en-us.all.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/translations/en-us.all.json b/translations/en-us.all.json index 3bce2abf8..dea9888dd 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -763,6 +763,10 @@ "id": "no_files", "translation": "No files found? That doesn't even make sense!" }, + { + "id": "loading_file_list", + "translation": "Loading the file list..." + }, { "id": "uploaded_by", "translation": "Uploaded by" From a740a236bcf365bb64048f92221ee99b71e5aa92 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 18:33:54 +0100 Subject: [PATCH 05/45] Update CHANGELOG.md --- translations/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/translations/CHANGELOG.md b/translations/CHANGELOG.md index c785a6abb..867dd19e4 100644 --- a/translations/CHANGELOG.md +++ b/translations/CHANGELOG.md @@ -91,3 +91,5 @@ ## 2017/11/04 * + nsfw_content * + generating_torrent_failed +## 2017/11/04 +* + loading_file_list From 68da680e47c082b04cecff8e17a65405f4767e2c Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 18:33:59 +0100 Subject: [PATCH 06/45] Update view.jet.html --- templates/site/torrents/view.jet.html | 41 +++++++++++++++++++-------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index a6c371389..b6fb4ef49 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -190,8 +190,6 @@

{{Torrent.Name}}

0}}checked{{end}}/>
- {{ if len(Torrent.FileList) > 0 }} - {* how do i concat lol *} @@ -200,12 +198,13 @@

{{Torrent.Name}}

- {{ yield make_treeview(treeviewData=makeTreeViewData(RootFolder, 0, "root")) }} + {{ if len(Torrent.FileList) > 0 }} + {{ yield make_treeview(treeviewData=makeTreeViewData(RootFolder, 0, "root")) }} + {{else}} + + {{end}}
{{ T("no_files") }}
- {{ else }} -

{{ T("no_files") }}

- {{ end }}

{{ T("comments")}}

@@ -336,9 +335,30 @@

{{ T("are_you_sure") }}

// order of apparition of the modals button: ["#reportPopup", "#tagPopup"] }); - + + var FileListContainer = document.querySelector("#filelist tbody"), + FileListLabel = document.getElementsByClassName("filelist-control")[0] + + if(FileListLabel.className.includes("hidden")) { + FileListLabel.addEventListener("click", function (e) { + FileListContainer.innerHTML = "{{T("loading_file_list")}}" + Query.Get('/stats/{{Torrent.ID}}?files', function (data) { + if(data.filelist != null) { + FileListContainer.innerHTML = "" + FileListLabel.style.opacity = 1 + document.getElementById("filelist").style.opacity = 1 + + for(var i = 0; i < data.filelist.length; i++) { + var file = data.filelist[i] + if(file.filesize == "0.0 B") file.filesize = "{{T("unknown")}}" + FileListContainer.innerHTML = FileListContainer.innerHTML + ''+ file.path +''+ file.filesize +'' + } + } + }) + }) + } + {{ if !torrentFileExists(Torrent.Hash, Torrent.TorrentLink)}} - {{end}} {{if Torrent.StatsObsolete[1] }} - {{end}} + {{ if User.ID > 0 }} From 1b1fd28cc67c48cb9150870e07266e3714cf9622 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 18:38:28 +0100 Subject: [PATCH 07/45] prevent filelist from being loaded --- controllers/torrent/stats.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index 718b11365..b861d1b51 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -1,6 +1,7 @@ package torrentController import ( + "html/template" "encoding/hex" "strconv" "strings" @@ -53,7 +54,7 @@ func GetStatsHandler(c *gin.Context) { var CurrentData models.Scrape statsExists := !(models.ORM.Where("torrent_id = ?", id).Find(&CurrentData).RecordNotFound()) - if statsExists { + if statsExists && c.Request.URL.Query()["files"] == nil { //Stats already exist, we check if the torrent stats have been scraped already very recently and if so, we stop there to avoid abuse of the /stats/:id route if isEmptyScrape(CurrentData) && time.Since(CurrentData.LastScrape).Minutes() <= config.Get().Scrape.MaxStatScrapingFrequencyUnknown { //Unknown stats but has been scraped less than X minutes ago (X being the limit set in the config file) @@ -83,9 +84,12 @@ func GetStatsHandler(c *gin.Context) { } var stats goscrape.Result - var torrentFiles []models.FileJSON + var torrentFiles []FileJSON if c.Request.URL.Query()["files"] != nil { + if len(updateTorrent.FileList) > 0 { + return + } err, torrentFiles = ScrapeFiles(format.InfoHashToMagnet(strings.TrimSpace(updateTorrent.Hash), updateTorrent.Name, Trackers...), updateTorrent, CurrentData, statsExists) if err != nil { return @@ -114,11 +118,11 @@ func GetStatsHandler(c *gin.Context) { return } -func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scrape, statsExists bool) (error, []models.FileJSON) { +func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scrape, statsExists bool) (error, []FileJSON) { if client == nil { err := initClient() if err != nil { - return err, []models.FileJSON{} + return err, []FileJSON{} } } @@ -146,7 +150,7 @@ func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scra } // UpdateTorrentStats : Update stats & filelist if files are specified, otherwise just stats -func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentStats models.Scrape, Files []torrent.File, statsExists bool) (JSONFilelist []models.FileJSON) { +func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentStats models.Scrape, Files []torrent.File, statsExists bool) (JSONFilelist []FileJSON) { if stats.Seeders == -1 { stats.Seeders = 0 } @@ -169,7 +173,7 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt torrent.FileList = []models.File{} for i, file := range Files { torrent.FileList = append(torrent.FileList, models.File{uint(i), torrent.ID, file.DisplayPath(), file.Length()}) - JSONFilelist = append(JSONFilelist, models.FileJSON{file.DisplayPath(), file.Length()}) + JSONFilelist = append(JSONFilelist, FileJSON{file.DisplayPath(), fileSize(file.Length()), "tr-file"}) } torrent.Update(true) } @@ -177,6 +181,13 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt return } +// FileJSON for file model in json, +type FileJSON struct { + Path string `json:"path"` + Filesize template.HTML `json:"filesize"` + Class string `json:"class"` +} + func isEmptyResult(stats goscrape.Result) bool { return stats.Seeders == 0 && stats.Leechers == 0 && stats.Completed == 0 } @@ -185,6 +196,10 @@ func isEmptyScrape(stats models.Scrape) bool { return stats.Seeders == 0 && stats.Leechers == 0 && stats.Completed == 0 } +func fileSize(filesize int64) template.HTML { + return template.HTML(format.FileSize(filesize)) +} + func contains(s []string, e string) bool { for _, a := range s { if a == e { From 8e3d53c702bee298589439d5edda2e983343fd09 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 18:42:48 +0100 Subject: [PATCH 08/45] Update view.jet.html --- templates/site/torrents/view.jet.html | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index b6fb4ef49..70cc1a96e 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -336,27 +336,27 @@

{{ T("are_you_sure") }}

button: ["#reportPopup", "#tagPopup"] }); +{{ if len(Torrent.FileList) == 0 }} var FileListContainer = document.querySelector("#filelist tbody"), FileListLabel = document.getElementsByClassName("filelist-control")[0] - if(FileListLabel.className.includes("hidden")) { - FileListLabel.addEventListener("click", function (e) { - FileListContainer.innerHTML = "{{T("loading_file_list")}}" - Query.Get('/stats/{{Torrent.ID}}?files', function (data) { - if(data.filelist != null) { - FileListContainer.innerHTML = "" - FileListLabel.style.opacity = 1 - document.getElementById("filelist").style.opacity = 1 + FileListLabel.addEventListener("click", function (e) { + FileListContainer.innerHTML = "{{T("loading_file_list")}}" + Query.Get('/stats/{{Torrent.ID}}?files', function (data) { + if(data.filelist != null) { + FileListContainer.innerHTML = "" + FileListLabel.style.opacity = 1 + document.getElementById("filelist").style.opacity = 1 - for(var i = 0; i < data.filelist.length; i++) { - var file = data.filelist[i] - if(file.filesize == "0.0 B") file.filesize = "{{T("unknown")}}" - FileListContainer.innerHTML = FileListContainer.innerHTML + ''+ file.path +''+ file.filesize +'' - } - } - }) - }) - } + for(var i = 0; i < data.filelist.length; i++) { + var file = data.filelist[i] + if(file.filesize == "0.0 B") file.filesize = "{{T("unknown")}}" + FileListContainer.innerHTML = FileListContainer.innerHTML + ''+ file.path +''+ file.filesize +'' + } + } + }) + }) + {{end}} {{ if !torrentFileExists(Torrent.Hash, Torrent.TorrentLink)}} var torrentLink = document.getElementById("torrent-download-link"), From c3cb627268a3e027ecf2c24fcb6b9d4b7ef65d0d Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 21:23:49 +0100 Subject: [PATCH 09/45] Update stats.go --- controllers/torrent/stats.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index b861d1b51..da9fa9488 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -113,6 +113,7 @@ func GetStatsHandler(c *gin.Context) { "leechers": stats.Leechers, "downloads": stats.Completed, "filelist": torrentFiles, + "totalsize": fileSize(updateTorrent.Filesize), }) return @@ -171,11 +172,13 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt if len(Files) > 0 { torrent.FileList = []models.File{} + torrent.Filesize = 0 for i, file := range Files { torrent.FileList = append(torrent.FileList, models.File{uint(i), torrent.ID, file.DisplayPath(), file.Length()}) JSONFilelist = append(JSONFilelist, FileJSON{file.DisplayPath(), fileSize(file.Length()), "tr-file"}) + torrent.Filesize += file.Length() } - torrent.Update(true) + torrent.Update(false) } return @@ -183,9 +186,9 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt // FileJSON for file model in json, type FileJSON struct { - Path string `json:"path"` - Filesize template.HTML `json:"filesize"` - Class string `json:"class"` + Path string `json:"path"` + Filesize template.HTML `json:"filesize"` + Class string `json:"class"` } func isEmptyResult(stats goscrape.Result) bool { From 557047c6ac9dba801837043ca66344d9cbc5e6f4 Mon Sep 17 00:00:00 2001 From: kilo Date: Tue, 7 Nov 2017 21:23:57 +0100 Subject: [PATCH 10/45] Update view.jet.html --- templates/site/torrents/view.jet.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index 70cc1a96e..668686d3a 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -52,7 +52,7 @@

{{Torrent.Name}}

{{ T("size")}}: - {{ fileSize(Torrent.Filesize, T, true) }} + {{ fileSize(Torrent.Filesize, T, true) }} {{ if len(Torrent.Languages) > 0 && Torrent.Languages[0] != "" }} @@ -347,7 +347,8 @@

{{ T("are_you_sure") }}

FileListContainer.innerHTML = "" FileListLabel.style.opacity = 1 document.getElementById("filelist").style.opacity = 1 - + if(data.totalsize != "0.0 B") document.getElementsByClassName("torrent-info-size")[0].innerHTML = data.totalsize + for(var i = 0; i < data.filelist.length; i++) { var file = data.filelist[i] if(file.filesize == "0.0 B") file.filesize = "{{T("unknown")}}" From 11567bf553b3d36cacad223b7eb300cf19c3d1d3 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 08:36:48 +0100 Subject: [PATCH 11/45] Update view.jet.html --- templates/site/torrents/view.jet.html | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index 668686d3a..b2cc16e3c 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -338,7 +338,8 @@

{{ T("are_you_sure") }}

{{ if len(Torrent.FileList) == 0 }} var FileListContainer = document.querySelector("#filelist tbody"), - FileListLabel = document.getElementsByClassName("filelist-control")[0] + FileListLabel = document.getElementsByClassName("filelist-control")[0], + FileListOldHtml = FileListContainer.innerHTML FileListLabel.addEventListener("click", function (e) { FileListContainer.innerHTML = "{{T("loading_file_list")}}" @@ -354,7 +355,9 @@

{{ T("are_you_sure") }}

if(file.filesize == "0.0 B") file.filesize = "{{T("unknown")}}" FileListContainer.innerHTML = FileListContainer.innerHTML + ''+ file.path +''+ file.filesize +'' } - } + } else { + FileListContainer.innerHTML = FileListOldHtml + } }) }) {{end}} From ef30870ce38cad25be87a423aa3ca05a893e4e76 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 08:50:52 +0100 Subject: [PATCH 12/45] Update torrent.go --- models/torrent.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/models/torrent.go b/models/torrent.go index e68ed7d03..157c73784 100644 --- a/models/torrent.go +++ b/models/torrent.go @@ -21,6 +21,7 @@ import ( "github.com/NyaaPantsu/nyaa/utils/format" "github.com/NyaaPantsu/nyaa/utils/log" "github.com/NyaaPantsu/nyaa/utils/sanitize" + "github.com/anacrolix/torrent" "github.com/bradfitz/slice" "github.com/fatih/structs" ) @@ -463,6 +464,24 @@ func (t *Torrent) Update(unscope bool) (int, error) { return http.StatusOK, nil } +func (t *Torrent) CreateFileList(Files []torrent.File) ([]File, error) { + var createdFilelist []File + t.Filesize = 0 + + for _, uploadedFile := range Files { + file := File{TorrentID: t.ID, Filesize: uploadedFile.Length()} + err := file.SetPath([]string{uploadedFile.DisplayPath(), ""}) + if err != nil { + return []File{}, err + } + createdFilelist = append(createdFilelist, file) + t.Filesize += uploadedFile.Length() + ORM.Create(&file) + } + + return createdFilelist, nil +} + // UpdateUnscope : Update a torrent based on model func (t *Torrent) UpdateUnscope() (int, error) { return t.Update(true) From 1219e6926012a1e0ef40fbf9d5c1066fb06d70a2 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 08:51:40 +0100 Subject: [PATCH 13/45] Update torrent.go --- models/torrent.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/torrent.go b/models/torrent.go index 157c73784..240755927 100644 --- a/models/torrent.go +++ b/models/torrent.go @@ -471,6 +471,7 @@ func (t *Torrent) CreateFileList(Files []torrent.File) ([]File, error) { for _, uploadedFile := range Files { file := File{TorrentID: t.ID, Filesize: uploadedFile.Length()} err := file.SetPath([]string{uploadedFile.DisplayPath(), ""}) + // Need to figure out what SetPath() is supposed to be fed because this ain't working if err != nil { return []File{}, err } From ceb02d1b769b2fb09aa8bce8657c6fca5bc8c082 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 09:39:40 +0100 Subject: [PATCH 14/45] Update file.go --- models/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/file.go b/models/file.go index f1bd4d8c9..a023659ce 100644 --- a/models/file.go +++ b/models/file.go @@ -74,7 +74,7 @@ func (f *File) FilenameExtension() string { fileName := path[len(path)-1] index := strings.LastIndex(fileName, ".") - if index == -1 { + if index == -1 || index+1 == len(fileName){ return "" } From ed8cf8c850367a938edacca2acfde6584eac9bea Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 10:05:10 +0100 Subject: [PATCH 15/45] Update stats.go --- controllers/torrent/stats.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index da9fa9488..3d9b9a289 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -1,6 +1,7 @@ package torrentController import ( + "path/filepath" "html/template" "encoding/hex" "strconv" @@ -18,6 +19,7 @@ import ( "github.com/anacrolix/dht" "github.com/anacrolix/torrent" + "github.com/bradfitz/slice" ) var client *torrent.Client @@ -171,14 +173,24 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt } if len(Files) > 0 { - torrent.FileList = []models.File{} - torrent.Filesize = 0 - for i, file := range Files { - torrent.FileList = append(torrent.FileList, models.File{uint(i), torrent.ID, file.DisplayPath(), file.Length()}) - JSONFilelist = append(JSONFilelist, FileJSON{file.DisplayPath(), fileSize(file.Length()), "tr-file"}) - torrent.Filesize += file.Length() + files, err := torrent.CreateFileList(Files) + + if err != nil { + return } - torrent.Update(false) + + JSONFilelist = make([]FileJSON, 0, len(files)) + for _, f := range files { + JSONFilelist = append(JSONFilelist, FileJSON{ + Path: filepath.Join(f.Path()...), + Filesize: fileSize(f.Filesize), + }) + } + + // Sort file list by lowercase filename + slice.Sort(JSONFilelist, func(i, j int) bool { + return strings.ToLower(JSONFilelist[i].Path) < strings.ToLower(JSONFilelist[j].Path) + }) } return @@ -188,7 +200,6 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt type FileJSON struct { Path string `json:"path"` Filesize template.HTML `json:"filesize"` - Class string `json:"class"` } func isEmptyResult(stats goscrape.Result) bool { From b6b14146475300708260174c19d076d9c22550aa Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 10:05:15 +0100 Subject: [PATCH 16/45] Update torrent.go --- models/torrent.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/models/torrent.go b/models/torrent.go index 240755927..d4696dc52 100644 --- a/models/torrent.go +++ b/models/torrent.go @@ -470,8 +470,7 @@ func (t *Torrent) CreateFileList(Files []torrent.File) ([]File, error) { for _, uploadedFile := range Files { file := File{TorrentID: t.ID, Filesize: uploadedFile.Length()} - err := file.SetPath([]string{uploadedFile.DisplayPath(), ""}) - // Need to figure out what SetPath() is supposed to be fed because this ain't working + err := file.SetPath(uploadedFile.FileInfo().Path) if err != nil { return []File{}, err } @@ -479,7 +478,9 @@ func (t *Torrent) CreateFileList(Files []torrent.File) ([]File, error) { t.Filesize += uploadedFile.Length() ORM.Create(&file) } - + + t.FileList = createdFilelist + t.Update(false) return createdFilelist, nil } From 1d8a3249c4a785a4e320e935c73577ce0c7a8584 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 10:25:29 +0100 Subject: [PATCH 17/45] constantly preload filelists --- models/torrents/find.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/models/torrents/find.go b/models/torrents/find.go index a7e915ee8..5c9d114cc 100644 --- a/models/torrents/find.go +++ b/models/torrents/find.go @@ -39,10 +39,8 @@ func FindByID(id uint) (*models.Torrent, error) { } - tmp := models.ORM.Where("torrent_id = ?", id).Preload("Scrape").Preload("Uploader").Preload("Comments") - if id > config.Get().Models.LastOldTorrentID { - tmp = tmp.Preload("FileList") - } + tmp := models.ORM.Where("torrent_id = ?", id).Preload("Scrape").Preload("Uploader").Preload("Comments").Preload("FileList") + if id <= config.Get().Models.LastOldTorrentID && !config.IsSukebei() { // only preload old comments if they could actually exist tmp = tmp.Preload("OldComments") From d3480adf61b0a7e28cf0ec264259e226068ced0b Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 10:30:32 +0100 Subject: [PATCH 18/45] Update treeview.jet.html --- templates/layouts/partials/helpers/treeview.jet.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/layouts/partials/helpers/treeview.jet.html b/templates/layouts/partials/helpers/treeview.jet.html index 61a472c1f..c84dd9704 100644 --- a/templates/layouts/partials/helpers/treeview.jet.html +++ b/templates/layouts/partials/helpers/treeview.jet.html @@ -4,7 +4,7 @@ {{ folderId := treeviewData.IdentifierChain+"_"+index }} - {{ fileSize(folder.TotalSize(), T) }} + {{ fileSize(folder.TotalSize(), T, false) }} From 479eb022479e7d29f5f5cefdbbc8a6b09cdf9db8 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 10:32:27 +0100 Subject: [PATCH 19/45] Update treeview.jet.html --- templates/layouts/partials/helpers/treeview.jet.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/layouts/partials/helpers/treeview.jet.html b/templates/layouts/partials/helpers/treeview.jet.html index c84dd9704..48bfc1279 100644 --- a/templates/layouts/partials/helpers/treeview.jet.html +++ b/templates/layouts/partials/helpers/treeview.jet.html @@ -2,7 +2,7 @@ {{ if isset(treeviewData)}} {{ range index, folder := treeviewData.Folder.Folders }} {{ folderId := treeviewData.IdentifierChain+"_"+index }} - {{ fileSize(folder.TotalSize(), T, false) }} From 49ead0321a94d75ab7e35bdf5c5ee7055ce0856e Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:43:44 +0100 Subject: [PATCH 20/45] Update stats.go --- controllers/torrent/stats.go | 83 ++++++++++-------------------------- 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index 3d9b9a289..0fed08188 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -2,8 +2,6 @@ package torrentController import ( "path/filepath" - "html/template" - "encoding/hex" "strconv" "strings" "net/url" @@ -47,8 +45,8 @@ func GetStatsHandler(c *gin.Context) { return } - updateTorrent, err := torrents.FindRawByID(uint(id)) - + updateTorrent, err := torrents.FindByID(uint(id)) + if err != nil { return } @@ -68,22 +66,7 @@ func GetStatsHandler(c *gin.Context) { } } - var Trackers []string - if len(updateTorrent.Trackers) > 3 { - for _, line := range strings.Split(updateTorrent.Trackers[3:], "&tr=") { - tracker, error := url.QueryUnescape(line) - if error == nil && strings.HasPrefix(tracker, "udp") { - Trackers = append(Trackers, tracker) - } - //Cannot scrape from http trackers only keep UDP ones - } - } - - for _, line := range config.Get().Torrents.Trackers.Default { - if !contains(Trackers, line) { - Trackers = append(Trackers, line) - } - } + Trackers := GetTorrentTrackers(updateTorrent) var stats goscrape.Result var torrentFiles []FileJSON @@ -121,39 +104,8 @@ func GetStatsHandler(c *gin.Context) { return } -func ScrapeFiles(magnet string, torrent models.Torrent, currentStats models.Scrape, statsExists bool) (error, []FileJSON) { - if client == nil { - err := initClient() - if err != nil { - return err, []FileJSON{} - } - } - - t, _ := client.AddMagnet(magnet) - <-t.GotInfo() - - infoHash := t.InfoHash() - dst := make([]byte, hex.EncodedLen(len(t.InfoHash()))) - hex.Encode(dst, infoHash[:]) - - var UDP []string - - for _, tracker := range t.Metainfo().AnnounceList[0] { - if strings.HasPrefix(tracker, "udp") { - UDP = append(UDP, tracker) - } - } - var results goscrape.Result - if len(UDP) != 0 { - udpscrape := goscrape.NewBulk(UDP) - results = udpscrape.ScrapeBulk([]string{torrent.Hash})[0] - } - t.Drop() - return nil, UpdateTorrentStats(torrent, results, currentStats, t.Files(), statsExists) -} - // UpdateTorrentStats : Update stats & filelist if files are specified, otherwise just stats -func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentStats models.Scrape, Files []torrent.File, statsExists bool) (JSONFilelist []FileJSON) { +func UpdateTorrentStats(torrent *models.Torrent, stats goscrape.Result, currentStats models.Scrape, Files []torrent.File, statsExists bool) (JSONFilelist []FileJSON) { if stats.Seeders == -1 { stats.Seeders = 0 } @@ -196,10 +148,25 @@ func UpdateTorrentStats(torrent models.Torrent, stats goscrape.Result, currentSt return } -// FileJSON for file model in json, -type FileJSON struct { - Path string `json:"path"` - Filesize template.HTML `json:"filesize"` +// GetTorrentTrackers : Get the torrent trackers and add the default ones if they are missing +func GetTorrentTrackers(torrent *models.Torrent) []string { + var Trackers []string + if len(torrent.Trackers) > 3 { + for _, line := range strings.Split(torrent.Trackers[3:], "&tr=") { + tracker, error := url.QueryUnescape(line) + if error == nil && strings.HasPrefix(tracker, "udp") { + Trackers = append(Trackers, tracker) + } + //Cannot scrape from http trackers only keep UDP ones + } + } + + for _, line := range config.Get().Torrents.Trackers.Default { + if !contains(Trackers, line) { + Trackers = append(Trackers, line) + } + } + return Trackers } func isEmptyResult(stats goscrape.Result) bool { @@ -210,10 +177,6 @@ func isEmptyScrape(stats models.Scrape) bool { return stats.Seeders == 0 && stats.Leechers == 0 && stats.Completed == 0 } -func fileSize(filesize int64) template.HTML { - return template.HTML(format.FileSize(filesize)) -} - func contains(s []string, e string) bool { for _, a := range s { if a == e { From 77e602b3e803a7d7ffaa204de06b1f611a718428 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:44:04 +0100 Subject: [PATCH 21/45] Update router.go --- controllers/torrent/router.go | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/torrent/router.go b/controllers/torrent/router.go index ce5c067be..e315cd1bd 100644 --- a/controllers/torrent/router.go +++ b/controllers/torrent/router.go @@ -8,6 +8,7 @@ import ( func init() { router.Get().Any("/download/:hash", DownloadTorrent) router.Get().Any("/stats/:id", GetStatsHandler) + router.Get().Any("/files/:id", GetFilesHandler) torrentRoutes := router.Get().Group("/torrent", middlewares.LoggedInMiddleware()) { From 8f28efcbc97836c738224308530a94cf21340b62 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:44:37 +0100 Subject: [PATCH 22/45] Add files via upload --- controllers/torrent/files.go | 78 ++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 controllers/torrent/files.go diff --git a/controllers/torrent/files.go b/controllers/torrent/files.go new file mode 100644 index 000000000..bbd160869 --- /dev/null +++ b/controllers/torrent/files.go @@ -0,0 +1,78 @@ +package torrentController + +import ( + "html/template" + "encoding/hex" + "net/http" + "strings" + "strconv" + + "github.com/NyaaPantsu/nyaa/models/torrents" + "github.com/NyaaPantsu/nyaa/models" + "github.com/NyaaPantsu/nyaa/templates" + "github.com/NyaaPantsu/nyaa/utils/format" + "github.com/NyaaPantsu/nyaa/utils/filelist" + "github.com/Stephen304/goscrape" + "github.com/gin-gonic/gin" +) + +func GetFilesHandler(c *gin.Context) { + id, _ := strconv.ParseInt(c.Param("id"), 10, 32) + torrent, err := torrents.FindByID(uint(id)) + + if err != nil { + c.Status(http.StatusNotFound) + return + } + + + if len(torrent.FileList) == 0 { + var blankScrape models.Scrape + ScrapeFiles(format.InfoHashToMagnet(strings.TrimSpace(torrent.Hash), torrent.Name, GetTorrentTrackers(torrent)...), torrent, blankScrape, true) + } + + folder := filelist.FileListToFolder(torrent.FileList, "root") + templates.TorrentFileList(c, torrent.ToJSON(), folder) +} + +// ScrapeFiles : Scrape torrent files +func ScrapeFiles(magnet string, torrent *models.Torrent, currentStats models.Scrape, statsExists bool) (error, []FileJSON) { + if client == nil { + err := initClient() + if err != nil { + return err, []FileJSON{} + } + } + + t, _ := client.AddMagnet(magnet) + <-t.GotInfo() + + infoHash := t.InfoHash() + dst := make([]byte, hex.EncodedLen(len(t.InfoHash()))) + hex.Encode(dst, infoHash[:]) + + var UDP []string + + for _, tracker := range t.Metainfo().AnnounceList[0] { + if strings.HasPrefix(tracker, "udp") { + UDP = append(UDP, tracker) + } + } + var results goscrape.Result + if len(UDP) != 0 { + udpscrape := goscrape.NewBulk(UDP) + results = udpscrape.ScrapeBulk([]string{torrent.Hash})[0] + } + t.Drop() + return nil, UpdateTorrentStats(torrent, results, currentStats, t.Files(), statsExists) +} + +// FileJSON for file model in json, +type FileJSON struct { + Path string `json:"path"` + Filesize template.HTML `json:"filesize"` +} + +func fileSize(filesize int64) template.HTML { + return template.HTML(format.FileSize(filesize)) +} \ No newline at end of file From c1658bc872bbffe327f7cf07410ab118687f7b28 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:45:00 +0100 Subject: [PATCH 23/45] Update template.go --- templates/template.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/templates/template.go b/templates/template.go index e0b9230d5..3c5b73d33 100644 --- a/templates/template.go +++ b/templates/template.go @@ -160,6 +160,14 @@ func Torrent(c *gin.Context, torrent models.TorrentJSON, rootFolder *filelist.Fi Render(c, path.Join(SiteDir, "torrents", "view.jet.html"), variables) } +// Torrent render a torrent view template +func TorrentFileList(c *gin.Context, torrent models.TorrentJSON, rootFolder *filelist.FileListFolder) { + variables := Commonvariables(c) + variables.Set("Torrent", torrent) + variables.Set("RootFolder", rootFolder) + Render(c, path.Join(SiteDir, "torrents", "filelist.jet.html"), variables) +} + // userProfilBase render the base for user profile func userProfileBase(c *gin.Context, templateName string, userProfile *models.User, variables jet.VarMap) { currentUser, _, _ := cookies.CurrentUser(c) From e703dea1e86263ee8e5bb19d3b9ac3fd969d57d0 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:45:06 +0100 Subject: [PATCH 24/45] Update template_test.go --- templates/template_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/templates/template_test.go b/templates/template_test.go index 7723f8a13..096d395bf 100644 --- a/templates/template_test.go +++ b/templates/template_test.go @@ -109,6 +109,11 @@ func walkDirTest(dir string, t *testing.T) { variables.Set("RootFolder", filelist.FileListToFolder(fakeTorrent.FileList, "root")) return variables }, + "filelist.jet.html": func(variables jet.VarMap) jet.VarMap { + variables.Set("Torrent", fakeTorrent.ToJSON()) + variables.Set("RootFolder", filelist.FileListToFolder(fakeTorrent.FileList, "root")) + return variables + }, "settings.jet.html": func(variables jet.VarMap) jet.VarMap { variables.Set("Form", &LanguagesJSONResponse{"test", publicSettings.Languages{*fakeLanguage, *fakeLanguage}}) return variables From ef49289115aa00f15ac6a644014ab1f455d20862 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:45:25 +0100 Subject: [PATCH 25/45] Add files via upload --- templates/site/torrents/filelist.jet.html | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 templates/site/torrents/filelist.jet.html diff --git a/templates/site/torrents/filelist.jet.html b/templates/site/torrents/filelist.jet.html new file mode 100644 index 000000000..463d8a0bc --- /dev/null +++ b/templates/site/torrents/filelist.jet.html @@ -0,0 +1,32 @@ +{{ extends "layouts/index_site" }} +{{ import "layouts/partials/helpers/csrf" }} +{{ import "layouts/partials/helpers/captcha" }} +{{ import "layouts/partials/helpers/errors" }} +{{ import "layouts/partials/helpers/tags" }} +{{ import "layouts/partials/helpers/treeview" }} +{{ import "layouts/partials/helpers/tag_form" }} +{{block title()}}{{Torrent.Name}}{{end}} +{{block content_body()}} +
+

{{T("torrent_filelist")}}

+ 0}}checked{{end}}/> + +
+ + + + + + + + + {{ if len(Torrent.FileList) > 0 }} + {{ yield make_treeview(treeviewData=makeTreeViewData(RootFolder, 0, "root")) }} + {{else}} + + {{end}} + +
{{ T("file_name")}}{{ T("size")}}
{{ T("no_files") }}
+
+
+{{end}} From 18000fbcd74778b0db6a82f0b00a04fdbd765ca5 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:45:52 +0100 Subject: [PATCH 26/45] Update CHANGELOG.md --- translations/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/translations/CHANGELOG.md b/translations/CHANGELOG.md index 867dd19e4..218c0e1f1 100644 --- a/translations/CHANGELOG.md +++ b/translations/CHANGELOG.md @@ -91,5 +91,6 @@ ## 2017/11/04 * + nsfw_content * + generating_torrent_failed -## 2017/11/04 +## 2017/11/08 * + loading_file_list +* + torrent_filelist From bd8a8b7185ffa1ca6a0d58ebc4e9e9d78bb88729 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:46:15 +0100 Subject: [PATCH 27/45] Update en-us.all.json --- translations/en-us.all.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/translations/en-us.all.json b/translations/en-us.all.json index dea9888dd..3efaebf13 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -767,6 +767,10 @@ "id": "loading_file_list", "translation": "Loading the file list..." }, + { + "id": "torrent_filelist", + "translation": "Torrent filelist" + }, { "id": "uploaded_by", "translation": "Uploaded by" From 9f7ee658e9d0620db93e0cd148812fe49a794b35 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:51:45 +0100 Subject: [PATCH 28/45] Update filelist.jet.html --- templates/site/torrents/filelist.jet.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/site/torrents/filelist.jet.html b/templates/site/torrents/filelist.jet.html index 463d8a0bc..592d120fd 100644 --- a/templates/site/torrents/filelist.jet.html +++ b/templates/site/torrents/filelist.jet.html @@ -9,7 +9,8 @@ {{block content_body()}}

{{T("torrent_filelist")}}

- 0}}checked{{end}}/> + «- {{T("back_to_torrent", Torrent.Name)}}"
+
From ba503c86937250e263953146b181aef7a300ee44 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:52:23 +0100 Subject: [PATCH 29/45] Update en-us.all.json --- translations/en-us.all.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/translations/en-us.all.json b/translations/en-us.all.json index 3efaebf13..608a93d4c 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -767,14 +767,14 @@ "id": "loading_file_list", "translation": "Loading the file list..." }, - { - "id": "torrent_filelist", - "translation": "Torrent filelist" - }, { "id": "uploaded_by", "translation": "Uploaded by" }, + { + "id": "back_to_torrent", + "translation": "Back to \"%s\"" + }, { "id": "report_btn", "translation": "Report" From 1d90dcf7e0a63c1e19ad9102d6ae58dc2604eb17 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:52:32 +0100 Subject: [PATCH 30/45] Update CHANGELOG.md --- translations/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/translations/CHANGELOG.md b/translations/CHANGELOG.md index 218c0e1f1..26bb6aae7 100644 --- a/translations/CHANGELOG.md +++ b/translations/CHANGELOG.md @@ -94,3 +94,4 @@ ## 2017/11/08 * + loading_file_list * + torrent_filelist +* + back_to_torrent From 9ffb5f1cde9cd9e2529be5990ddca96f15106243 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 11:59:44 +0100 Subject: [PATCH 31/45] Update view.jet.html --- templates/site/torrents/view.jet.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index b2cc16e3c..17e9bbf62 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -188,7 +188,13 @@

{{Torrent.Name}}

{{ T("no_description") }}

{{end}} 0}}checked{{end}}/> - +
@@ -340,6 +346,8 @@

{{ T("are_you_sure") }}

var FileListContainer = document.querySelector("#filelist tbody"), FileListLabel = document.getElementsByClassName("filelist-control")[0], FileListOldHtml = FileListContainer.innerHTML + + FileListLabel.innerHTML = FileListLabel.innerText FileListLabel.addEventListener("click", function (e) { FileListContainer.innerHTML = "" From cb8604d11aba13184b4993bff0556ec945021ed1 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 12:06:57 +0100 Subject: [PATCH 32/45] failsafe for empty names --- models/file.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/models/file.go b/models/file.go index a023659ce..31ee85c6a 100644 --- a/models/file.go +++ b/models/file.go @@ -52,12 +52,18 @@ func (f *File) SetPath(path []string) error { // Filename : Returns the filename of the file func (f *File) Filename() string { path := f.Path() + if len(path) == 0 { + return "" + } return path[len(path)-1] } // FilenameWithoutExtension : Returns the filename of the file without the extension func (f *File) FilenameWithoutExtension() string { path := f.Path() + if len(path) == 0 { + return "" + } fileName := path[len(path)-1] index := strings.LastIndex(fileName, ".") @@ -71,6 +77,9 @@ func (f *File) FilenameWithoutExtension() string { // FilenameExtension : Returns the extension of a filename, or an empty string func (f *File) FilenameExtension() string { path := f.Path() + if len(path) == 0 { + return "" + } fileName := path[len(path)-1] index := strings.LastIndex(fileName, ".") From c943839c069e788a1317b76a1a803e02a55137c3 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 16:39:18 +0100 Subject: [PATCH 33/45] remove " --- templates/site/torrents/filelist.jet.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/site/torrents/filelist.jet.html b/templates/site/torrents/filelist.jet.html index 592d120fd..53de4eb98 100644 --- a/templates/site/torrents/filelist.jet.html +++ b/templates/site/torrents/filelist.jet.html @@ -9,7 +9,7 @@ {{block content_body()}}

{{T("torrent_filelist")}}

- «- {{T("back_to_torrent", Torrent.Name)}}"
+ «- {{T("back_to_torrent", Torrent.Name)}}
From 1c828141ab64adce2f8f4de1573fdb582f446aa5 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 16:59:31 +0100 Subject: [PATCH 34/45] Update en-us.all.json --- translations/en-us.all.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/translations/en-us.all.json b/translations/en-us.all.json index 608a93d4c..819c5de41 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -768,13 +768,17 @@ "translation": "Loading the file list..." }, { - "id": "uploaded_by", - "translation": "Uploaded by" + "id": "torrent_filelist", + "translation": "Torrent filelist" }, { "id": "back_to_torrent", "translation": "Back to \"%s\"" }, + { + "id": "uploaded_by", + "translation": "Uploaded by" + }, { "id": "report_btn", "translation": "Report" From 95daf79ab159e38c82b01de9c0360d2b14b12999 Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 8 Nov 2017 17:02:15 +0100 Subject: [PATCH 35/45] Update en-us.all.json --- translations/en-us.all.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translations/en-us.all.json b/translations/en-us.all.json index 819c5de41..22aacf12a 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -765,7 +765,7 @@ }, { "id": "loading_file_list", - "translation": "Loading the file list..." + "translation": "Loading the file list, long file lists can take time to fetch..." }, { "id": "torrent_filelist", From 95cc7fd4fdbdee78d4c644133637d6f2d9031d19 Mon Sep 17 00:00:00 2001 From: kilo Date: Fri, 10 Nov 2017 22:46:22 +0100 Subject: [PATCH 36/45] ignore http trackers --- controllers/torrent/stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index 0fed08188..973f8fbf9 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -162,7 +162,7 @@ func GetTorrentTrackers(torrent *models.Torrent) []string { } for _, line := range config.Get().Torrents.Trackers.Default { - if !contains(Trackers, line) { + if !contains(Trackers, line) && strings.HasPrefix(tracker, "udp") { Trackers = append(Trackers, line) } } From 88a4966dbd3ecca5a4cce0871d0684086c32e66c Mon Sep 17 00:00:00 2001 From: kilo Date: Thu, 16 Nov 2017 19:51:58 +0100 Subject: [PATCH 37/45] fix travis --- controllers/torrent/stats.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index 973f8fbf9..af8bdff1f 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -161,8 +161,8 @@ func GetTorrentTrackers(torrent *models.Torrent) []string { } } - for _, line := range config.Get().Torrents.Trackers.Default { - if !contains(Trackers, line) && strings.HasPrefix(tracker, "udp") { + for _, tracker := range config.Get().Torrents.Trackers.Default { + if !contains(Trackers, tracker) && strings.HasPrefix(tracker, "udp") { Trackers = append(Trackers, line) } } From 52050b2c07d2a7ab5eb3b8aec67814465b735583 Mon Sep 17 00:00:00 2001 From: kilo Date: Thu, 16 Nov 2017 20:00:54 +0100 Subject: [PATCH 38/45] fix travis --- controllers/torrent/stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/torrent/stats.go b/controllers/torrent/stats.go index af8bdff1f..937f68995 100644 --- a/controllers/torrent/stats.go +++ b/controllers/torrent/stats.go @@ -163,7 +163,7 @@ func GetTorrentTrackers(torrent *models.Torrent) []string { for _, tracker := range config.Get().Torrents.Trackers.Default { if !contains(Trackers, tracker) && strings.HasPrefix(tracker, "udp") { - Trackers = append(Trackers, line) + Trackers = append(Trackers, tracker) } } return Trackers From 01865e805b865d551f51c607a93248a0b72e1624 Mon Sep 17 00:00:00 2001 From: kilo Date: Fri, 24 Nov 2017 12:39:21 +0100 Subject: [PATCH 39/45] fix missing +
{{T("loading_file_list")}}