1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"net/http"
"github.com/dyatlov/go-opengraph/opengraph"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
const OPEN_GRAPH_METADATA_CACHE_SIZE = 10000
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) {
if !*c.App.Config().ServiceSettings.EnableLinkPreviews {
c.Err = model.NewAppError("getOpenGraphMetadata", "api.post.link_preview_disabled.app_error", nil, "", http.StatusNotImplemented)
return
}
props := model.StringInterfaceFromJson(r.Body)
url := ""
ok := false
if url, ok = props["url"].(string); len(url) == 0 || !ok {
c.SetInvalidParam("url")
return
}
ogJSONGeneric, ok := openGraphDataCache.Get(url)
if ok {
w.Write(ogJSONGeneric.([]byte))
return
}
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 {
w.Write([]byte(`{"url": ""}`))
return
}
w.Write(ogJSON)
}
|