From de292a4146b86057edc0d94b51097a854c4fa25c Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 23 Mar 2019 03:47:07 -0500 Subject: [PATCH] add extraction of image ids --- backend/app/store/image/image.go | 24 ++++++++++++++++++++++++ backend/app/store/image/image_test.go | 10 ++++++++++ 2 files changed, 34 insertions(+) diff --git a/backend/app/store/image/image.go b/backend/app/store/image/image.go index d9e58a98..16fa144d 100644 --- a/backend/app/store/image/image.go +++ b/backend/app/store/image/image.go @@ -16,6 +16,7 @@ import ( "sync" "time" + "github.com/PuerkitoBio/goquery" log "github.com/go-pkgz/lgr" "github.com/google/uuid" "github.com/pkg/errors" @@ -186,3 +187,26 @@ func (f *FileSystem) location(base string, id string) string { return path.Join(base, user, partition(id), file) } + +// ExtractPictures gets list of images from the doc html and convert from urls to ids, i.e. user/pic.png +func ExtractPictures(commentHTML string, match string) (ids []string, err error) { + + doc, err := goquery.NewDocumentFromReader(strings.NewReader(commentHTML)) + if err != nil { + return nil, errors.Wrap(err, "can't create document") + } + result := []string{} + doc.Find("img").Each(func(i int, s *goquery.Selection) { + if im, ok := s.Attr("src"); ok { + if strings.Contains(im, match) { + elems := strings.Split(im, "/") + if len(elems) >= 2 { + id := elems[len(elems)-2] + "/" + elems[len(elems)-1] + result = append(result, id) + } + } + } + }) + + return result, nil +} diff --git a/backend/app/store/image/image_test.go b/backend/app/store/image/image_test.go index 0179df1d..ef3ec135 100644 --- a/backend/app/store/image/image_test.go +++ b/backend/app/store/image/image_test.go @@ -183,6 +183,16 @@ func TestImage_Cleanup(t *testing.T) { assert.NotNil(t, err, "no file on staging anymore") } +func TestExtractPictures(t *testing.T) { + html := `blah foo + xyz

123

` + ids, err := ExtractPictures(html, "/blah/") + require.NoError(t, err) + assert.Equal(t, 2, len(ids), "two images") + assert.Equal(t, "user1/pic1.png", ids[0]) + assert.Equal(t, "user2/pic3.png", ids[1]) +} + func prepareImageTest(t *testing.T) (svc FileSystem, teardown func()) { loc, err := ioutil.TempDir("", "test_image_r42") require.NoError(t, err, "failed to make temp dir")