Browse Source

api key required, if url already shortened -> show directly

pull/85/head
Atrox 10 years ago
parent
commit
9e694eb16a
  1. 2
      display.go
  2. 4
      server.go
  3. 6
      shorturl.go
  4. 12
      static/js/shorturl.js
  5. 13
      templates/display/base.html

2
display.go

@ -120,6 +120,8 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
"extra": extra,
"lines": lines,
"files": metadata.ArchiveFiles,
"shorturlEnabled": Config.googleShorterAPIKey != "",
"shorturl": metadata.ShortURL,
}, r, w)
if err != nil {

4
server.go

@ -184,7 +184,11 @@ func setup() *web.Mux {
mux.Get(selifRe, fileServeHandler)
mux.Get(selifIndexRe, unauthorizedHandler)
mux.Get(torrentRe, fileTorrentHandler)
if Config.googleShorterAPIKey != "" {
mux.Get(shortRe, shortURLHandler)
}
mux.NotFound(notFoundHandler)
return mux

6
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))

12
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,10 +22,11 @@ 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;
}
});
@ -38,4 +37,3 @@ function copy(someText) {
clipboard.on('error', function (e) {
e.trigger.setAttribute('aria-label', 'Your browser does not support coping to clipboard')
});
}

13
templates/display/base.html

@ -17,7 +17,15 @@
{% endif %}
{% block infomore %}{% endblock %}
<span>{{ size }}</span> |
<a class="hint--top" aria-label="Click to retrieve shortened url" id="shorturl" data-url="{{ sitepath }}{{filename}}/short" style="cursor: pointer;">short url</a> |
{% if shorturlEnabled %}
{% if shorturl %}
<a class="hint--top" aria-label="Click to copy into clipboard" id="shorturl"
style="cursor: pointer;" href="{{shorturl}}">{{shorturl}}</a> |
{% else %}
<a class="hint--top" aria-label="Click to retrieve shortened url" id="shorturl"
data-url="{{ sitepath }}{{filename}}/short" style="cursor: pointer;">short url</a> |
{% endif %}
{% endif %}
<a href="{{ filename }}/torrent" download>torrent</a> |
<a href="{{ sitepath }}selif/{{ filename }}" download>get</a>
</div>
@ -35,5 +43,8 @@
</div>
<script src="{{ sitepath }}static/js/clipboard.js"></script>
{% if shorturlEnabled %}
<script src="{{ sitepath }}static/js/shorturl.js"></script>
{% endif %}
{% endblock %}
Loading…
Cancel
Save