mutantmonkey
6 years ago
committed by
Andrei Marcu
8 changed files with 7 additions and 163 deletions
-
1README.md
-
1backends/meta.go
-
3backends/metajson/metajson.go
-
16display.go
-
8server.go
-
89shorturl.go
-
39static/js/shorturl.js
-
13templates/display/base.html
@ -1,89 +0,0 @@ |
|||||
package main |
|
||||
|
|
||||
import ( |
|
||||
"bytes" |
|
||||
"encoding/json" |
|
||||
"errors" |
|
||||
"net/http" |
|
||||
|
|
||||
"github.com/zenazn/goji/web" |
|
||||
) |
|
||||
|
|
||||
type shortenerRequest struct { |
|
||||
LongURL string `json:"longUrl"` |
|
||||
} |
|
||||
|
|
||||
type shortenerResponse struct { |
|
||||
Kind string `json:"kind"` |
|
||||
ID string `json:"id"` |
|
||||
LongURL string `json:"longUrl"` |
|
||||
Error struct { |
|
||||
Code int `json:"code"` |
|
||||
Message string `json:"message"` |
|
||||
} `json:"error"` |
|
||||
} |
|
||||
|
|
||||
func shortURLHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
||||
fileName := c.URLParams["name"] |
|
||||
|
|
||||
err := checkFile(fileName) |
|
||||
if err == NotFoundErr { |
|
||||
notFoundHandler(c, w, r) |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
metadata, err := metadataRead(fileName) |
|
||||
if err != nil { |
|
||||
oopsHandler(c, w, r, RespJSON, "Corrupt metadata.") |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
if metadata.ShortURL == "" { |
|
||||
url, err := shortenURL(getSiteURL(r) + fileName) |
|
||||
if err != nil { |
|
||||
oopsHandler(c, w, r, RespJSON, err.Error()) |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
metadata.ShortURL = url |
|
||||
|
|
||||
err = metadataWrite(fileName, &metadata) |
|
||||
if err != nil { |
|
||||
oopsHandler(c, w, r, RespJSON, "Corrupt metadata.") |
|
||||
return |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
js, _ := json.Marshal(map[string]string{ |
|
||||
"shortUrl": metadata.ShortURL, |
|
||||
}) |
|
||||
w.Write(js) |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
func shortenURL(url string) (string, error) { |
|
||||
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)) |
|
||||
req.Header.Set("Content-Type", "application/json") |
|
||||
|
|
||||
client := &http.Client{} |
|
||||
resp, err := client.Do(req) |
|
||||
if err != nil { |
|
||||
return "", err |
|
||||
} |
|
||||
defer resp.Body.Close() |
|
||||
|
|
||||
shortenerResponse := new(shortenerResponse) |
|
||||
err = json.NewDecoder(resp.Body).Decode(shortenerResponse) |
|
||||
if err != nil { |
|
||||
return "", err |
|
||||
} |
|
||||
|
|
||||
if shortenerResponse.Error.Message != "" { |
|
||||
return "", errors.New(shortenerResponse.Error.Message) |
|
||||
} |
|
||||
|
|
||||
return shortenerResponse.ID, nil |
|
||||
} |
|
@ -1,39 +0,0 @@ |
|||||
document.getElementById('shorturl').addEventListener('click', function (e) { |
|
||||
e.preventDefault(); |
|
||||
|
|
||||
if (e.target.href !== "") return; |
|
||||
|
|
||||
xhr = new XMLHttpRequest(); |
|
||||
xhr.open("GET", e.target.dataset.url, true); |
|
||||
xhr.setRequestHeader('Accept', 'application/json'); |
|
||||
xhr.onreadystatechange = function () { |
|
||||
if (xhr.readyState === 4) { |
|
||||
var resp = JSON.parse(xhr.responseText); |
|
||||
|
|
||||
if (xhr.status === 200 && resp.error == null) { |
|
||||
e.target.innerText = resp.shortUrl; |
|
||||
e.target.href = resp.shortUrl; |
|
||||
e.target.setAttribute('aria-label', 'Click to copy into clipboard') |
|
||||
} else { |
|
||||
e.target.setAttribute('aria-label', resp.error) |
|
||||
} |
|
||||
} |
|
||||
}; |
|
||||
xhr.send(); |
|
||||
}); |
|
||||
|
|
||||
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('error', function (e) { |
|
||||
e.trigger.setAttribute('aria-label', 'Your browser does not support coping to clipboard') |
|
||||
}); |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue