summaryrefslogtreecommitdiffstats
path: root/api4/openGraph.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2018-04-16 06:00:59 -0700
committerHarrison Healey <harrisonmhealey@gmail.com>2018-04-16 09:00:59 -0400
commitba6f9075c1fcc3fbfa5ba94e97df6a4dd9e70b50 (patch)
treeac2cc309c84816e8f031abde3ae7d30ca6add21d /api4/openGraph.go
parent6e2cb00008cbf09e556b00f87603797fcaa47e09 (diff)
downloadchat-ba6f9075c1fcc3fbfa5ba94e97df6a4dd9e70b50.tar.gz
chat-ba6f9075c1fcc3fbfa5ba94e97df6a4dd9e70b50.tar.bz2
chat-ba6f9075c1fcc3fbfa5ba94e97df6a4dd9e70b50.zip
Fix opengraph images not going through the image proxy. (#8627)
Diffstat (limited to 'api4/openGraph.go')
-rw-r--r--api4/openGraph.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/api4/openGraph.go b/api4/openGraph.go
index 646cce883..af0989ab9 100644
--- a/api4/openGraph.go
+++ b/api4/openGraph.go
@@ -6,6 +6,7 @@ package api4
import (
"net/http"
+ "github.com/dyatlov/go-opengraph/opengraph"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
@@ -16,6 +17,30 @@ var openGraphDataCache = utils.NewLru(OPEN_GRAPH_METADATA_CACHE_SIZE)
func (api *API) InitOpenGraph() {
api.BaseRoutes.OpenGraph.Handle("", api.ApiSessionRequired(getOpenGraphMetadata)).Methods("POST")
+
+ // Dump the image cache if the proxy settings have changed. (need switch URLs to the correct proxy)
+ api.App.AddConfigListener(func(before, after *model.Config) {
+ if (before.ServiceSettings.ImageProxyType != after.ServiceSettings.ImageProxyType) ||
+ (before.ServiceSettings.ImageProxyURL != after.ServiceSettings.ImageProxyType) {
+ openGraphDataCache.Purge()
+ }
+ })
+}
+
+func OpenGraphDataWithProxyAddedToImageURLs(ogdata *opengraph.OpenGraph, toProxyURL func(string) string) *opengraph.OpenGraph {
+ for _, image := range ogdata.Images {
+ var url string
+ if image.SecureURL != "" {
+ url = image.SecureURL
+ } else {
+ url = image.URL
+ }
+
+ image.URL = ""
+ image.SecureURL = toProxyURL(url)
+ }
+
+ return ogdata
}
func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -41,6 +66,11 @@ func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
og := c.App.GetOpenGraphMetadata(url)
+ // If image proxy enabled modify open graph data to feed though proxy
+ if toProxyURL := c.App.ImageProxyAdder(); toProxyURL != nil {
+ og = OpenGraphDataWithProxyAddedToImageURLs(og, toProxyURL)
+ }
+
ogJSON, err := og.ToJSON()
openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 hour
if err != nil {