mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-11 02:13:41 +09:00
GO-2584: add ObjectCreateFromUrl command
Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
This commit is contained in:
parent
d660425cc5
commit
1504c5ccbc
19 changed files with 2803 additions and 1670 deletions
|
@ -32,11 +32,11 @@ func (c *cache) Name() string {
|
|||
return CName
|
||||
}
|
||||
|
||||
func (c *cache) Fetch(ctx context.Context, url string) (lp model.LinkPreview, err error) {
|
||||
func (c *cache) Fetch(ctx context.Context, url string) (lp model.LinkPreview, body []byte, err error) {
|
||||
if res, ok := c.cache.Get(url); ok {
|
||||
return res.(model.LinkPreview), nil
|
||||
return res.(model.LinkPreview), nil, nil
|
||||
}
|
||||
lp, err = c.lp.Fetch(ctx, url)
|
||||
lp, body, err = c.lp.Fetch(ctx, url)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ func TestCache_Fetch(t *testing.T) {
|
|||
ts := newTestServer("text/html", strings.NewReader(tetsHtml))
|
||||
lp := NewWithCache()
|
||||
lp.Init(nil)
|
||||
info, err := lp.Fetch(ctx, ts.URL)
|
||||
info, _, err := lp.Fetch(ctx, ts.URL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: ts.URL,
|
||||
|
@ -27,7 +27,7 @@ func TestCache_Fetch(t *testing.T) {
|
|||
|
||||
ts.Close()
|
||||
|
||||
info, err = lp.Fetch(ctx, ts.URL)
|
||||
info, _, err = lp.Fetch(ctx, ts.URL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: ts.URL,
|
||||
|
|
|
@ -33,7 +33,7 @@ const (
|
|||
)
|
||||
|
||||
type LinkPreview interface {
|
||||
Fetch(ctx context.Context, url string) (model.LinkPreview, error)
|
||||
Fetch(ctx context.Context, url string) (model.LinkPreview, []byte, error)
|
||||
app.Component
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ func (l *linkPreview) Name() (name string) {
|
|||
return CName
|
||||
}
|
||||
|
||||
func (l *linkPreview) Fetch(ctx context.Context, fetchUrl string) (model.LinkPreview, error) {
|
||||
func (l *linkPreview) Fetch(ctx context.Context, fetchUrl string) (model.LinkPreview, []byte, error) {
|
||||
rt := &proxyRoundTripper{RoundTripper: http.DefaultTransport}
|
||||
client := &http.Client{Transport: rt}
|
||||
og := opengraph.New(fetchUrl)
|
||||
|
@ -60,13 +60,17 @@ func (l *linkPreview) Fetch(ctx context.Context, fetchUrl string) (model.LinkPre
|
|||
err := og.Fetch()
|
||||
if err != nil {
|
||||
if resp := rt.lastResponse; resp != nil && resp.StatusCode == http.StatusOK {
|
||||
return l.makeNonHtml(fetchUrl, resp)
|
||||
preview, err := l.makeNonHtml(fetchUrl, resp)
|
||||
if err != nil {
|
||||
return preview, nil, err
|
||||
}
|
||||
return preview, rt.lastBody, nil
|
||||
}
|
||||
return model.LinkPreview{}, err
|
||||
return model.LinkPreview{}, nil, err
|
||||
}
|
||||
|
||||
if resp := rt.lastResponse; resp != nil && resp.StatusCode != http.StatusOK {
|
||||
return model.LinkPreview{}, fmt.Errorf("invalid http code %d", resp.StatusCode)
|
||||
return model.LinkPreview{}, nil, fmt.Errorf("invalid http code %d", resp.StatusCode)
|
||||
}
|
||||
res := l.convertOGToInfo(fetchUrl, og)
|
||||
if len(res.Description) == 0 {
|
||||
|
@ -78,7 +82,7 @@ func (l *linkPreview) Fetch(ctx context.Context, fetchUrl string) (model.LinkPre
|
|||
if !utf8.ValidString(res.Description) {
|
||||
res.Description = ""
|
||||
}
|
||||
return res, nil
|
||||
return res, rt.lastBody, nil
|
||||
}
|
||||
|
||||
func (l *linkPreview) convertOGToInfo(fetchUrl string, og *opengraph.OpenGraph) (i model.LinkPreview) {
|
||||
|
|
|
@ -23,7 +23,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
|
|||
lp := New()
|
||||
lp.Init(nil)
|
||||
|
||||
info, err := lp.Fetch(ctx, ts.URL)
|
||||
info, _, err := lp.Fetch(ctx, ts.URL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: ts.URL,
|
||||
|
@ -41,7 +41,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
|
|||
lp := New()
|
||||
lp.Init(nil)
|
||||
|
||||
info, err := lp.Fetch(ctx, ts.URL)
|
||||
info, _, err := lp.Fetch(ctx, ts.URL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: ts.URL,
|
||||
|
@ -60,7 +60,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
|
|||
url := ts.URL + "/filename.jpg"
|
||||
lp := New()
|
||||
lp.Init(nil)
|
||||
info, err := lp.Fetch(ctx, url)
|
||||
info, _, err := lp.Fetch(ctx, url)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: url,
|
||||
|
@ -78,7 +78,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
|
|||
url := ts.URL + "/filename.jpg"
|
||||
lp := New()
|
||||
lp.Init(nil)
|
||||
info, err := lp.Fetch(ctx, url)
|
||||
info, _, err := lp.Fetch(ctx, url)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, model.LinkPreview{
|
||||
Url: url,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue