forked from tetrafolium/mattermost-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengraph.go
125 lines (98 loc) · 3.45 KB
/
opengraph.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"html"
"io"
"net/url"
"github.com/dyatlov/go-opengraph/opengraph"
"golang.org/x/net/html/charset"
"github.com/mattermost/mattermost-server/mlog"
)
const MaxOpenGraphResponseSize = 1024 * 1024 * 50
func (a *App) GetOpenGraphMetadata(requestURL string) *opengraph.OpenGraph {
res, err := a.HTTPService.MakeClient(false).Get(requestURL)
if err != nil {
mlog.Error("GetOpenGraphMetadata request failed", mlog.String("requestURL", requestURL), mlog.Any("err", err))
return nil
}
defer res.Body.Close()
return a.ParseOpenGraphMetadata(requestURL, res.Body, res.Header.Get("Content-Type"))
}
func (a *App) ParseOpenGraphMetadata(requestURL string, body io.Reader, contentType string) *opengraph.OpenGraph {
og := opengraph.NewOpenGraph()
body = forceHTMLEncodingToUTF8(io.LimitReader(body, MaxOpenGraphResponseSize), contentType)
if err := og.ProcessHTML(body); err != nil {
mlog.Error("ParseOpenGraphMetadata processing failed", mlog.String("requestURL", requestURL), mlog.Any("err", err))
}
makeOpenGraphURLsAbsolute(og, requestURL)
openGraphDecodeHtmlEntities(og)
// If image proxy enabled modify open graph data to feed though proxy
if toProxyURL := a.ImageProxyAdder(); toProxyURL != nil {
og = OpenGraphDataWithProxyAddedToImageURLs(og, toProxyURL)
}
// The URL should be the link the user provided in their message, not a redirected one.
if og.URL != "" {
og.URL = requestURL
}
return og
}
func forceHTMLEncodingToUTF8(body io.Reader, contentType string) io.Reader {
r, err := charset.NewReader(body, contentType)
if err != nil {
mlog.Error("forceHTMLEncodingToUTF8 failed to convert", mlog.String("contentType", contentType), mlog.Any("err", err))
return body
}
return r
}
func makeOpenGraphURLsAbsolute(og *opengraph.OpenGraph, requestURL string) {
parsedRequestURL, err := url.Parse(requestURL)
if err != nil {
mlog.Warn("makeOpenGraphURLsAbsolute failed to parse url", mlog.String("requestURL", requestURL), mlog.Any("err", err))
return
}
makeURLAbsolute := func(resultURL string) string {
if resultURL == "" {
return resultURL
}
parsedResultURL, err := url.Parse(resultURL)
if err != nil {
mlog.Warn("makeOpenGraphURLsAbsolute failed to parse result", mlog.String("requestURL", requestURL), mlog.Any("err", err))
return resultURL
}
if parsedResultURL.IsAbs() {
return resultURL
}
return parsedRequestURL.ResolveReference(parsedResultURL).String()
}
og.URL = makeURLAbsolute(og.URL)
for _, image := range og.Images {
image.URL = makeURLAbsolute(image.URL)
image.SecureURL = makeURLAbsolute(image.SecureURL)
}
for _, audio := range og.Audios {
audio.URL = makeURLAbsolute(audio.URL)
audio.SecureURL = makeURLAbsolute(audio.SecureURL)
}
for _, video := range og.Videos {
video.URL = makeURLAbsolute(video.URL)
video.SecureURL = makeURLAbsolute(video.SecureURL)
}
}
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 openGraphDecodeHtmlEntities(og *opengraph.OpenGraph) {
og.Title = html.UnescapeString(og.Title)
og.Description = html.UnescapeString(og.Description)
}