From 149877a939656f3780fb73f5add26e5e205cf26f Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Fri, 18 Dec 2020 19:33:24 +1100 Subject: [PATCH] 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 --- VERSION | 2 +- protocol/urls/urls.go | 47 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/VERSION b/VERSION index afed694ee..e40e4fc33 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.65.0 +0.66.0 diff --git a/protocol/urls/urls.go b/protocol/urls/urls.go index 888861c4a..1c713c4ac 100644 --- a/protocol/urls/urls.go +++ b/protocol/urls/urls.go @@ -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())