diff --git a/display.go b/display.go index 4d3896b..f26b665 100644 --- a/display.go +++ b/display.go @@ -113,13 +113,15 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { } err = renderTemplate(tpl, pongo2.Context{ - "mime": metadata.Mimetype, - "filename": fileName, - "size": sizeHuman, - "expiry": expiryHuman, - "extra": extra, - "lines": lines, - "files": metadata.ArchiveFiles, + "mime": metadata.Mimetype, + "filename": fileName, + "size": sizeHuman, + "expiry": expiryHuman, + "extra": extra, + "lines": lines, + "files": metadata.ArchiveFiles, + "shorturlEnabled": Config.googleShorterAPIKey != "", + "shorturl": metadata.ShortURL, }, r, w) if err != nil { diff --git a/server.go b/server.go index ae209f5..3ff5c7f 100644 --- a/server.go +++ b/server.go @@ -184,7 +184,11 @@ func setup() *web.Mux { mux.Get(selifRe, fileServeHandler) mux.Get(selifIndexRe, unauthorizedHandler) mux.Get(torrentRe, fileTorrentHandler) - mux.Get(shortRe, shortURLHandler) + + if Config.googleShorterAPIKey != "" { + mux.Get(shortRe, shortURLHandler) + } + mux.NotFound(notFoundHandler) return mux diff --git a/shorturl.go b/shorturl.go index 6c1a21b..afdaf00 100644 --- a/shorturl.go +++ b/shorturl.go @@ -62,11 +62,7 @@ func shortURLHandler(c web.C, w http.ResponseWriter, r *http.Request) { } func shortenURL(url string) (string, error) { - apiURL := "https://www.googleapis.com/urlshortener/v1/url" - if Config.googleShorterAPIKey != "" { - apiURL += "?key=" + Config.googleShorterAPIKey - } - + apiURL := "https://www.googleapis.com/urlshortener/v1/url?key=" + Config.googleShorterAPIKey jsonStr, _ := json.Marshal(shortenerRequest{LongURL: url}) req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonStr)) diff --git a/static/js/shorturl.js b/static/js/shorturl.js index c17154a..26e0c77 100644 --- a/static/js/shorturl.js +++ b/static/js/shorturl.js @@ -14,8 +14,6 @@ document.getElementById('shorturl').addEventListener('click', function (e) { e.target.innerText = resp.shortUrl; e.target.href = resp.shortUrl; e.target.setAttribute('aria-label', 'Click to copy into clipboard') - - copy(resp.shortUrl); } else { e.target.setAttribute('aria-label', resp.error) } @@ -24,18 +22,18 @@ document.getElementById('shorturl').addEventListener('click', function (e) { xhr.send(); }); -function copy(someText) { - var clipboard = new Clipboard('#shorturl', { - text: function () { - return someText; - } - }); +var clipboard = new Clipboard("#shorturl", { + text: function (trigger) { + if (trigger.href == null) return; + + return trigger.href; + } +}); - clipboard.on('success', function (e) { - e.trigger.setAttribute('aria-label', 'Successfully copied') - }); +clipboard.on('success', function (e) { + e.trigger.setAttribute('aria-label', 'Successfully copied') +}); - clipboard.on('error', function (e) { - e.trigger.setAttribute('aria-label', 'Your browser does not support coping to clipboard') - }); -} +clipboard.on('error', function (e) { + e.trigger.setAttribute('aria-label', 'Your browser does not support coping to clipboard') +}); diff --git a/templates/display/base.html b/templates/display/base.html index 1cb3383..587e76f 100644 --- a/templates/display/base.html +++ b/templates/display/base.html @@ -16,8 +16,16 @@ file expires in {{ expiry }} | {% endif %} {% block infomore %}{% endblock %} - {{ size }} | - short url | + {{ size }} | + {% if shorturlEnabled %} + {% if shorturl %} + {{shorturl}} | + {% else %} + short url | + {% endif %} + {% endif %} torrent | get @@ -35,5 +43,8 @@ + + {% if shorturlEnabled %} + {% endif %} {% endblock %}