mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-11 02:13:41 +09:00
linkpreview pkg
This commit is contained in:
parent
0ad2ccc431
commit
319eb5e591
4 changed files with 191 additions and 0 deletions
93
util/linkpreview/linkpreview.go
Normal file
93
util/linkpreview/linkpreview.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
package linkpreview
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/otiai10/opengraph"
|
||||
)
|
||||
|
||||
func New() LinkPreview {
|
||||
return &linkPreview{}
|
||||
}
|
||||
|
||||
type LinkType string
|
||||
|
||||
const (
|
||||
LinkTypeHtml LinkType = "html"
|
||||
LinkTypeImage LinkType = "image"
|
||||
LinkTypeVideo LinkType = "video"
|
||||
LinkTypeText LinkType = "text"
|
||||
LinkTypeUnexpected LinkType = "unexpected"
|
||||
|
||||
// read no more than 400 kb
|
||||
maxBytesToRead = 400000
|
||||
)
|
||||
|
||||
type LinkPreview interface {
|
||||
Fetch(ctx context.Context, url string) (Info, error)
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
Title string
|
||||
Description string
|
||||
ImageUrl string
|
||||
Type LinkType
|
||||
}
|
||||
|
||||
type linkPreview struct{}
|
||||
|
||||
func (l *linkPreview) Fetch(ctx context.Context, url string) (Info, error) {
|
||||
rt := &proxyRoundTripper{RoundTripper: http.DefaultTransport}
|
||||
client := &http.Client{Transport: rt}
|
||||
og, err := opengraph.FetchWithContext(ctx, url, client)
|
||||
if err != nil {
|
||||
if resp := rt.lastResponse; resp != nil && resp.StatusCode == http.StatusOK {
|
||||
return l.makeNonHtml(url, resp)
|
||||
}
|
||||
return Info{}, err
|
||||
}
|
||||
return l.convertOGToInfo(og), nil
|
||||
}
|
||||
|
||||
func (l *linkPreview) convertOGToInfo(og *opengraph.OpenGraph) (i Info) {
|
||||
i = Info{
|
||||
Title: og.Title,
|
||||
Description: og.Description,
|
||||
Type: LinkTypeHtml,
|
||||
}
|
||||
if len(og.Image) != 0 {
|
||||
i.ImageUrl = og.Image[0].URL
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (l *linkPreview) makeNonHtml(url string, resp *http.Response) (i Info, err error) {
|
||||
ct := resp.Header.Get("Content-Type")
|
||||
i.Title = filepath.Base(url)
|
||||
if strings.HasPrefix(ct, "image/") {
|
||||
i.Type = LinkTypeImage
|
||||
i.ImageUrl = url
|
||||
} else if strings.HasPrefix(ct, "text/") {
|
||||
i.Type = LinkTypeText
|
||||
} else {
|
||||
i.Type = LinkTypeUnexpected
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type proxyRoundTripper struct {
|
||||
http.RoundTripper
|
||||
lastResponse *http.Response
|
||||
}
|
||||
|
||||
func (p *proxyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
resp, err := p.RoundTripper.RoundTrip(req)
|
||||
if err == nil {
|
||||
p.lastResponse = resp
|
||||
resp.Body = http.MaxBytesReader(nil, resp.Body, maxBytesToRead)
|
||||
}
|
||||
return resp, err
|
||||
}
|
83
util/linkpreview/linkpreview_test.go
Normal file
83
util/linkpreview/linkpreview_test.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package linkpreview
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func TestLinkPreview_Fetch(t *testing.T) {
|
||||
t.Run("html page", func(t *testing.T) {
|
||||
ts := newTestServer("text/html", strings.NewReader(tetsHtml))
|
||||
defer ts.Close()
|
||||
lp := New()
|
||||
|
||||
info, err := lp.Fetch(ctx, ts.URL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, Info{
|
||||
Title: "Title",
|
||||
Description: "Description",
|
||||
ImageUrl: "http://site.com/images/example.jpg",
|
||||
Type: LinkTypeHtml,
|
||||
}, info)
|
||||
})
|
||||
|
||||
t.Run("binary image", func(t *testing.T) {
|
||||
tr := testReader(0)
|
||||
ts := newTestServer("image/jpg", &tr)
|
||||
defer ts.Close()
|
||||
url := ts.URL + "/filename.jpg"
|
||||
lp := New()
|
||||
info, err := lp.Fetch(ctx, url)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, Info{
|
||||
Title: "filename.jpg",
|
||||
ImageUrl: url,
|
||||
Type: LinkTypeImage,
|
||||
}, info)
|
||||
assert.True(t, int(tr) <= maxBytesToRead)
|
||||
})
|
||||
|
||||
t.Run("binary", func(t *testing.T) {
|
||||
tr := testReader(0)
|
||||
ts := newTestServer("binary/octed-stream", &tr)
|
||||
defer ts.Close()
|
||||
url := ts.URL + "/filename.jpg"
|
||||
lp := New()
|
||||
info, err := lp.Fetch(ctx, url)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, Info{
|
||||
Title: "filename.jpg",
|
||||
Type: LinkTypeUnexpected,
|
||||
}, info)
|
||||
assert.True(t, int(tr) <= maxBytesToRead)
|
||||
})
|
||||
}
|
||||
|
||||
func newTestServer(contentType string, data io.Reader) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
io.Copy(w, data)
|
||||
}))
|
||||
}
|
||||
|
||||
const tetsHtml = `<html><head>
|
||||
<title>Title</title>
|
||||
<meta name="description" content="Description">
|
||||
<meta property="og:image" content="http://site.com/images/example.jpg" />
|
||||
</head></html>`
|
||||
|
||||
type testReader int
|
||||
|
||||
func (t *testReader) Read(p []byte) (n int, err error) {
|
||||
*t += testReader(len(p))
|
||||
return len(p), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue