mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 17:44:59 +09:00
fixes
This commit is contained in:
parent
a62a24092f
commit
64f08ec52a
3 changed files with 32 additions and 14 deletions
|
@ -203,6 +203,10 @@ func loadImage(stor anytype.Anytype, url string) (hash string, err error) {
|
|||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("can't download '%s': %s", url, resp.Status)
|
||||
}
|
||||
|
||||
im, err := stor.ImageAddWithReader(resp.Body, filepath.Base(url))
|
||||
if err != nil {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
@ -32,13 +33,13 @@ type linkPreview struct {
|
|||
bmPolicy *bluemonday.Policy
|
||||
}
|
||||
|
||||
func (l *linkPreview) Fetch(ctx context.Context, url string) (model.LinkPreview, error) {
|
||||
func (l *linkPreview) Fetch(ctx context.Context, fetchUrl string) (model.LinkPreview, error) {
|
||||
rt := &proxyRoundTripper{RoundTripper: http.DefaultTransport}
|
||||
client := &http.Client{Transport: rt}
|
||||
og, err := opengraph.FetchWithContext(ctx, url, client)
|
||||
og, err := opengraph.FetchWithContext(ctx, fetchUrl, client)
|
||||
if err != nil {
|
||||
if resp := rt.lastResponse; resp != nil && resp.StatusCode == http.StatusOK {
|
||||
return l.makeNonHtml(url, resp)
|
||||
return l.makeNonHtml(fetchUrl, resp)
|
||||
}
|
||||
return model.LinkPreview{}, err
|
||||
}
|
||||
|
@ -58,6 +59,11 @@ func (l *linkPreview) convertOGToInfo(og *opengraph.OpenGraph) (i model.LinkPrev
|
|||
Type: model.LinkPreview_Page,
|
||||
FaviconUrl: og.Favicon,
|
||||
}
|
||||
if len(i.FaviconUrl) == 0 {
|
||||
og.URL.Path = "favicon.ico"
|
||||
og.URL.RawQuery = ""
|
||||
i.FaviconUrl = og.URL.String()
|
||||
}
|
||||
if len(og.Image) != 0 {
|
||||
i.ImageUrl = og.Image[0].URL
|
||||
}
|
||||
|
@ -83,18 +89,24 @@ func (l *linkPreview) findContent(data []byte) (content string) {
|
|||
return
|
||||
}
|
||||
|
||||
func (l *linkPreview) makeNonHtml(url string, resp *http.Response) (i model.LinkPreview, err error) {
|
||||
func (l *linkPreview) makeNonHtml(fetchUrl string, resp *http.Response) (i model.LinkPreview, err error) {
|
||||
ct := resp.Header.Get("Content-Type")
|
||||
i.Url = url
|
||||
i.Title = filepath.Base(url)
|
||||
i.Url = fetchUrl
|
||||
i.Title = filepath.Base(fetchUrl)
|
||||
if strings.HasPrefix(ct, "image/") {
|
||||
i.Type = model.LinkPreview_Image
|
||||
i.ImageUrl = url
|
||||
i.ImageUrl = fetchUrl
|
||||
} else if strings.HasPrefix(ct, "text/") {
|
||||
i.Type = model.LinkPreview_Text
|
||||
} else {
|
||||
i.Type = model.LinkPreview_Unknown
|
||||
}
|
||||
pUrl, e := url.Parse(fetchUrl)
|
||||
if e == nil {
|
||||
pUrl.Path = "favicon.ico"
|
||||
pUrl.RawQuery = ""
|
||||
i.FaviconUrl = pUrl.String()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -59,10 +59,11 @@ func TestLinkPreview_Fetch(t *testing.T) {
|
|||
info, err := lp.Fetch(ctx, url)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: url,
|
||||
Title: "filename.jpg",
|
||||
ImageUrl: url,
|
||||
Type: model.LinkPreview_Image,
|
||||
Url: url,
|
||||
Title: "filename.jpg",
|
||||
FaviconUrl: ts.URL + "/favicon.ico",
|
||||
ImageUrl: url,
|
||||
Type: model.LinkPreview_Image,
|
||||
}, info)
|
||||
})
|
||||
|
||||
|
@ -75,9 +76,10 @@ func TestLinkPreview_Fetch(t *testing.T) {
|
|||
info, err := lp.Fetch(ctx, url)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: url,
|
||||
Title: "filename.jpg",
|
||||
Type: model.LinkPreview_Unknown,
|
||||
Url: url,
|
||||
Title: "filename.jpg",
|
||||
FaviconUrl: ts.URL + "/favicon.ico",
|
||||
Type: model.LinkPreview_Unknown,
|
||||
}, info)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue