MM-12009 Resolve relative links in posts as the web app does (#9896)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
9730b46bca
Коммит
1607d18e55
@@ -7,6 +7,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dyatlov/go-opengraph/opengraph"
|
"github.com/dyatlov/go-opengraph/opengraph"
|
||||||
@@ -315,6 +316,8 @@ func getImagesInMessageAttachments(post *model.Post) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) getLinkMetadata(requestURL string, useCache bool) (*opengraph.OpenGraph, *model.PostImage, error) {
|
func (a *App) getLinkMetadata(requestURL string, useCache bool) (*opengraph.OpenGraph, *model.PostImage, error) {
|
||||||
|
requestURL = resolveMetadataURL(requestURL, a.GetSiteURL())
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
if useCache {
|
if useCache {
|
||||||
og, image, ok := getLinkMetadataFromCache(requestURL)
|
og, image, ok := getLinkMetadataFromCache(requestURL)
|
||||||
@@ -349,6 +352,21 @@ func (a *App) getLinkMetadata(requestURL string, useCache bool) (*opengraph.Open
|
|||||||
return og, image, err
|
return og, image, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveMetadataURL resolves a given URL relative to the server's site URL.
|
||||||
|
func resolveMetadataURL(requestURL string, siteURL string) string {
|
||||||
|
base, err := url.Parse(siteURL)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
resolved, err := base.Parse(requestURL)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolved.String()
|
||||||
|
}
|
||||||
|
|
||||||
func getLinkMetadataFromCache(requestURL string) (*opengraph.OpenGraph, *model.PostImage, bool) {
|
func getLinkMetadataFromCache(requestURL string) (*opengraph.OpenGraph, *model.PostImage, bool) {
|
||||||
cached, ok := linkCache.Get(requestURL)
|
cached, ok := linkCache.Get(requestURL)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -994,6 +994,47 @@ func TestGetImagesInMessageAttachments(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResolveMetadataURL(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
Name string
|
||||||
|
RequestURL string
|
||||||
|
SiteURL string
|
||||||
|
Expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "with HTTPS",
|
||||||
|
RequestURL: "https://example.com/file?param=1",
|
||||||
|
Expected: "https://example.com/file?param=1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "with HTTP",
|
||||||
|
RequestURL: "http://example.com/file?param=1",
|
||||||
|
Expected: "http://example.com/file?param=1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "with FTP",
|
||||||
|
RequestURL: "ftp://example.com/file?param=1",
|
||||||
|
Expected: "ftp://example.com/file?param=1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "relative to root",
|
||||||
|
RequestURL: "/file?param=1",
|
||||||
|
SiteURL: "https://mattermost.example.com:123",
|
||||||
|
Expected: "https://mattermost.example.com:123/file?param=1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "relative to root with subpath",
|
||||||
|
RequestURL: "/file?param=1",
|
||||||
|
SiteURL: "https://mattermost.example.com:123/subpath",
|
||||||
|
Expected: "https://mattermost.example.com:123/file?param=1",
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
|
assert.Equal(t, resolveMetadataURL(test.RequestURL, test.SiteURL), test.Expected)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseLinkMetadata(t *testing.T) {
|
func TestParseLinkMetadata(t *testing.T) {
|
||||||
th := Setup().InitBasic()
|
th := Setup().InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user