diff --git a/backend/app/rest/api/rest_private.go b/backend/app/rest/api/rest_private.go
index 13ec3c0f..b9dd0015 100644
--- a/backend/app/rest/api/rest_private.go
+++ b/backend/app/rest/api/rest_private.go
@@ -89,13 +89,8 @@ func (s *private) createCommentCtrl(w http.ResponseWriter, r *http.Request) {
comment = s.commentFormatter.Format(comment)
// check if images are valid
- imgIDs, err := s.imageService.ExtractPictures(comment.Text)
- if err != nil {
- rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't extract pictures from comment text", rest.ErrCommentValidation)
- return
- }
- for _, id := range imgIDs {
- _, err = s.imageService.Load(id)
+ for _, id := range s.imageService.ExtractPictures(comment.Text) {
+ _, err := s.imageService.Load(id)
if err != nil {
rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't load picture from the comment", rest.ErrImgNotFound)
return
diff --git a/backend/app/rest/api/rest_public.go b/backend/app/rest/api/rest_public.go
index b61e37e5..e1dabc89 100644
--- a/backend/app/rest/api/rest_public.go
+++ b/backend/app/rest/api/rest_public.go
@@ -134,12 +134,7 @@ func (s *public) previewCommentCtrl(w http.ResponseWriter, r *http.Request) {
comment.Sanitize()
// check if images are valid
- imgIDs, err := s.imageService.ExtractPictures(comment.Text)
- if err != nil {
- rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't extract pictures from comment text", rest.ErrCommentValidation)
- return
- }
- for _, id := range imgIDs {
+ for _, id := range s.imageService.ExtractPictures(comment.Text) {
_, err = s.imageService.Load(id)
if err != nil {
rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't load picture from the comment", rest.ErrImgNotFound)
diff --git a/backend/app/store/image/image.go b/backend/app/store/image/image.go
index 7b7ed23c..96d2c046 100644
--- a/backend/app/store/image/image.go
+++ b/backend/app/store/image/image.go
@@ -137,13 +137,12 @@ func (s *Service) Submit(idsFn func() []string) {
}
// ExtractPictures gets list of images from the doc html and convert from urls to ids, i.e. user/pic.png
-// It doesn't return possible errors parsing the cached images, and will try to return as many parsed ids
-// as possible instead.
-func (s *Service) ExtractPictures(commentHTML string) (ids []string, err error) {
+func (s *Service) ExtractPictures(commentHTML string) (ids []string) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(commentHTML))
if err != nil {
- return nil, errors.Wrap(err, "can't create document")
+ log.Printf("[ERROR] can't parse commentHTML to parse images: %q, error: %v", commentHTML, err)
+ return nil
}
doc.Find("img").Each(func(i int, sl *goquery.Selection) {
if im, ok := sl.Attr("src"); ok {
@@ -172,7 +171,7 @@ func (s *Service) ExtractPictures(commentHTML string) (ids []string, err error)
}
})
- return ids, nil
+ return ids
}
// Cleanup runs periodic cleanup with 2.5*ServiceParams.EditDuration. Blocking loop, should be called inside of goroutine by consumer
diff --git a/backend/app/store/image/image_test.go b/backend/app/store/image/image_test.go
index 41589450..229819f4 100644
--- a/backend/app/store/image/image_test.go
+++ b/backend/app/store/image/image_test.go
@@ -77,8 +77,7 @@ func TestService_ExtractPictures(t *testing.T) {
svc := Service{ServiceParams: ServiceParams{ImageAPI: "/blah/", ProxyAPI: "/non_existent"}}
html := `blah
foo
xyz
123
`
- ids, err := svc.ExtractPictures(html)
- require.NoError(t, err)
+ ids := svc.ExtractPictures(html)
require.Equal(t, 2, len(ids), "two images")
assert.Equal(t, "user1/pic1.png", ids[0])
assert.Equal(t, "user2/pic3.png", ids[1])
@@ -90,35 +89,30 @@ func TestService_ExtractPictures(t *testing.T) {

По форме все верно, это все packages, но по сути это все одна библиотека организованная таким образом. При ее импорте, например посредством go mod, она выглядит как один модуль, т.е.
github.com/go-pkgz/auth v0.5.2.