[MM-50827] Detect content-type if missing in response during link metadata generation (#22885)
* Detect content-type if missing in response during link metadata generation * Initialize buffer only when necessary
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
62d758f485
Коммит
b00ca20abe
@@ -4,6 +4,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
@@ -764,7 +765,24 @@ func cacheLinkMetadata(requestURL string, timestamp int64, og *opengraph.OpenGra
|
|||||||
platform.LinkCache().SetWithExpiry(strconv.FormatInt(model.GenerateLinkMetadataHash(requestURL, timestamp), 16), metadata, platform.LinkCacheDuration)
|
platform.LinkCache().SetWithExpiry(strconv.FormatInt(model.GenerateLinkMetadataHash(requestURL, timestamp), 16), metadata, platform.LinkCacheDuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// peekContentType peeks at the first 512 bytes of p, and attempts to detect
|
||||||
|
// the content type. Returns empty string if error occurs.
|
||||||
|
func peekContentType(p *bufio.Reader) string {
|
||||||
|
byt, err := p.Peek(512)
|
||||||
|
if err != nil && err != bufio.ErrBufferFull && err != io.EOF {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return http.DetectContentType(byt)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) parseLinkMetadata(requestURL string, body io.Reader, contentType string) (*opengraph.OpenGraph, *model.PostImage, error) {
|
func (a *App) parseLinkMetadata(requestURL string, body io.Reader, contentType string) (*opengraph.OpenGraph, *model.PostImage, error) {
|
||||||
|
if contentType == "" {
|
||||||
|
bufRd := bufio.NewReader(body)
|
||||||
|
// If the content-type is missing we try to detect it from the actual data.
|
||||||
|
contentType = peekContentType(bufRd)
|
||||||
|
body = bufRd
|
||||||
|
}
|
||||||
|
|
||||||
if contentType == "image/svg+xml" {
|
if contentType == "image/svg+xml" {
|
||||||
image := &model.PostImage{
|
image := &model.PostImage{
|
||||||
Format: "svg",
|
Format: "svg",
|
||||||
|
|||||||
@@ -2595,6 +2595,18 @@ func TestParseLinkMetadata(t *testing.T) {
|
|||||||
}, dimensions)
|
}, dimensions)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("image with no content-type given", func(t *testing.T) {
|
||||||
|
og, dimensions, err := th.App.parseLinkMetadata(imageURL, makeImageReader(), "")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Nil(t, og)
|
||||||
|
assert.Equal(t, &model.PostImage{
|
||||||
|
Format: "png",
|
||||||
|
Width: 408,
|
||||||
|
Height: 336,
|
||||||
|
}, dimensions)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("malformed image", func(t *testing.T) {
|
t.Run("malformed image", func(t *testing.T) {
|
||||||
og, dimensions, err := th.App.parseLinkMetadata(imageURL, makeOpenGraphReader(), "image/png")
|
og, dimensions, err := th.App.parseLinkMetadata(imageURL, makeOpenGraphReader(), "image/png")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user