feat: allow getPreviewLinkData for all whitelisted links (#2094)

* feat: allow getPreviewLinkData for all links

Only YouTube links were supported in `getPreviewLinkData` previously.

Now, any whitelisted links can have their data retreived using this function, returning the true content type (by examining the header bytes of the content).

feat: add tenor.com and giphy.com to whitelisted urls list

* Fix json format

* bump VERSION

* Lint urls.go

Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Eric Mastro 2020-12-18 19:33:24 +11:00 committed by GitHub
parent cdca42b90f
commit 149877a939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 10 deletions

View File

@ -1 +1 @@
0.65.0
0.66.0

View File

@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type OembedData struct {
@ -18,11 +19,13 @@ type LinkPreviewData struct {
Site string `json:"site"`
Title string `json:"title"`
ThumbnailURL string `json:"thumbnailUrl"`
ContentType string `json:"contentType"`
}
type Site struct {
Title string `json:"title"`
Address string `json:"address"`
Title string `json:"title"`
Address string `json:"address"`
ImageSite bool `json:"imageSite"`
}
const YouTubeOembedLink = "https://www.youtube.com/oembed?format=json&url=%s"
@ -30,12 +33,24 @@ const YouTubeOembedLink = "https://www.youtube.com/oembed?format=json&url=%s"
func LinkPreviewWhitelist() []Site {
return []Site{
Site{
Title: "YouTube",
Address: "youtube.com",
Title: "YouTube",
Address: "youtube.com",
ImageSite: false,
},
Site{
Title: "YouTube shortener",
Address: "youtu.be",
Title: "YouTube shortener",
Address: "youtu.be",
ImageSite: false,
},
Site{
Title: "Tenor GIFs",
Address: "tenor.com",
ImageSite: true,
},
Site{
Title: "GIPHY GIFs",
Address: "giphy.com",
ImageSite: true,
},
}
}
@ -87,9 +102,23 @@ func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
return previewData, fmt.Errorf("Cant't parse link %s", link)
}
switch url.Hostname() {
case "youtube.com", "www.youtube.com", "youtu.be":
return GetYoutubePreviewData(link)
hostname := strings.ToLower(url.Hostname())
youtubeHostnames := []string{"youtube.com", "www.youtube.com", "youtu.be"}
for _, youtubeHostname := range youtubeHostnames {
if youtubeHostname == hostname {
return GetYoutubePreviewData(link)
}
}
for _, site := range LinkPreviewWhitelist() {
if strings.HasSuffix(hostname, site.Address) && site.ImageSite {
content, contentErr := GetURLContent(link)
if contentErr != nil {
return previewData, contentErr
}
previewData.ThumbnailURL = link
previewData.ContentType = http.DetectContentType(content)
return previewData, nil
}
}
return previewData, fmt.Errorf("Link %s isn't whitelisted. Hostname - %s", link, url.Hostname())