1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 18:10:49 +09:00

add another utf check

Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
This commit is contained in:
AnastasiaShemyakinskaya 2024-02-01 19:37:44 +03:00
parent 85c1d942b6
commit e2b6856a15
No known key found for this signature in database
GPG key ID: CCD60ED83B103281

View file

@ -22,7 +22,10 @@ import (
"github.com/anyproto/anytype-heart/util/uri"
)
const CName = "linkpreview"
const (
CName = "linkpreview"
utfEncoding = "utf-8"
)
func New() LinkPreview {
return &linkPreview{}
@ -86,9 +89,6 @@ func (l *linkPreview) Fetch(ctx context.Context, fetchUrl string) (model.LinkPre
if !utf8.ValidString(res.Description) {
res.Description = ""
}
if utf8.Valid(rt.lastBody) {
return res, rt.lastBody, err
}
decodedResponse, err := decodeResponse(rt)
if err != nil {
log.Errorf("failed to decode request %s", err)
@ -98,7 +98,10 @@ func (l *linkPreview) Fetch(ctx context.Context, fetchUrl string) (model.LinkPre
func decodeResponse(response *proxyRoundTripper) ([]byte, error) {
contentType := response.lastResponse.Header.Get("Content-Type")
enc, _, _ := charset.DetermineEncoding(response.lastBody, contentType)
enc, name, _ := charset.DetermineEncoding(response.lastBody, contentType)
if name == utfEncoding {
return response.lastBody, nil
}
decodedResponse, err := enc.NewDecoder().Bytes(response.lastBody)
if err != nil {
return response.lastBody, err