From 35d4eb9ca81ad49b606cc3d5b0e049b92b89eb7c Mon Sep 17 00:00:00 2001 From: kilo Date: Wed, 6 Sep 2017 04:30:49 +0200 Subject: [PATCH] Add torrentFileExists function, check if torrent file exists and if it doesn't lower the opacity on the torrent download button (#1504) * Add torrentFileExists function * If torrent file doesn't exist, put torrent download button opacity at 50% * ditto for listing * Forgot an if * attempt at fixing travis by adding function into test templates * Attempt at fixing travis by having sex with my own mother * fix wrong error message * Add torrentFileExists as global function * Update template_functions.go --- templates/site/torrents/listing.jet.html | 4 +--- templates/site/torrents/view.jet.html | 4 +--- templates/template_functions.go | 14 +++++++++++++- templates/template_functions_test.go | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/templates/site/torrents/listing.jet.html b/templates/site/torrents/listing.jet.html index 539885c4..51afdb15 100644 --- a/templates/site/torrents/listing.jet.html +++ b/templates/site/torrents/listing.jet.html @@ -87,11 +87,9 @@
- {{if .TorrentLink != ""}} - +
- {{end}} {{ fileSize(.Filesize, T) }} diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index 791acb3d..411ddf94 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -133,11 +133,9 @@
{{ T("magnet_link")}}
- {{ if Torrent.TorrentLink != ""}} - +
{{ T("torrent_file")}}
- {{end}} {{ if User.ID > 0}} {{ T("report_btn") }} {{ if User.HasAdmin()}} diff --git a/templates/template_functions.go b/templates/template_functions.go index 552e2e85..441884e9 100644 --- a/templates/template_functions.go +++ b/templates/template_functions.go @@ -6,6 +6,8 @@ import ( "net/url" "strconv" "time" + "os" + "fmt" "strings" @@ -30,7 +32,7 @@ func templateFunctions(vars jet.VarMap) jet.VarMap { vars.Set("getDefaultLanguage", publicSettings.GetDefaultLanguage) vars.Set("FlagCode", flagCode) vars.Set("getAvatar", getAvatar) - + vars.Set("torrentFileExists", torrentFileExists) vars.Set("formatDateRFC", formatDateRFC) vars.Set("GetHostname", format.GetHostname) vars.Set("GetCategories", categories.GetSelect) @@ -292,3 +294,13 @@ func contains(arr interface{}, comp string) bool { } return false } + +func torrentFileExists(hash string) bool { + + Openfile, err := os.Open(fmt.Sprintf("%s%c%s.torrent", config.Get().Torrents.FileStorage, os.PathSeparator, hash)) + if err != nil { + return false + } + defer Openfile.Close() + return true +} diff --git a/templates/template_functions_test.go b/templates/template_functions_test.go index 049d67cb..a38fbd65 100644 --- a/templates/template_functions_test.go +++ b/templates/template_functions_test.go @@ -588,6 +588,24 @@ func TestContains(t *testing.T) { } } +func testTorrentFileExists(t *testing.T) { + var tests = []struct { + hash string + Expected bool + }{ + { + hash: "", + Expected: false, + }, + } + for _, test := range tests { + value := torrentFileExists(test.hash) + if value != test.Expected { + t.Errorf("Unexpected value from the function TorrentFileExists, got '%t', wanted '%t' for '%s'", value, test.Expected, test.hash) + } + } +} + func mockupTemplateT(t *testing.T) publicSettings.TemplateTfunc { conf := config.Get().I18n conf.Directory = path.Join("..", conf.Directory)