diff --git a/backend/app/rest/api/rest_private.go b/backend/app/rest/api/rest_private.go index 5385f756d9..4185e38eb4 100644 --- a/backend/app/rest/api/rest_private.go +++ b/backend/app/rest/api/rest_private.go @@ -90,8 +90,8 @@ func (s *private) previewCommentCtrl(w http.ResponseWriter, r *http.Request) { comment = s.commentFormatter.Format(comment) comment.Sanitize() - // check if images are valid - for _, id := range s.imageService.ExtractPictures(comment.Text) { + // check if images are valid, omit proxied images as they are lazy-loaded + for _, id := range s.imageService.ExtractPictures(comment.Text, false) { err := s.imageService.ResetCleanupTimer(id) if err != nil { rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't load picture from the comment", rest.ErrImgNotFound) @@ -129,8 +129,8 @@ func (s *private) createCommentCtrl(w http.ResponseWriter, r *http.Request) { } comment = s.commentFormatter.Format(comment) - // check if images are valid - for _, id := range s.imageService.ExtractPictures(comment.Text) { + // check if images are valid, omit proxied images as they are lazy-loaded + for _, id := range s.imageService.ExtractPictures(comment.Text, false) { _, 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 a5dfaee138..9365fc1625 100644 --- a/backend/app/store/image/image.go +++ b/backend/app/store/image/image.go @@ -140,7 +140,10 @@ 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 -func (s *Service) ExtractPictures(commentHTML string) (ids []string) { +// Proxied images are returned only if the flag is set: this is used in image check on post preview and load, +// as proxied images have lazy loading and wouldn't be present on disk but still valid as they will be loaded +// the first time someone requests them. +func (s *Service) ExtractPictures(commentHTML string, returnProxied bool) (ids []string) { doc, err := goquery.NewDocumentFromReader(strings.NewReader(commentHTML)) if err != nil { log.Printf("[ERROR] can't parse commentHTML to parse images: %q, error: %v", commentHTML, err) @@ -155,7 +158,7 @@ func (s *Service) ExtractPictures(commentHTML string) (ids []string) { ids = append(ids, id) } } - if strings.Contains(im, s.ProxyAPI) { + if strings.Contains(im, s.ProxyAPI) && returnProxied { proxiedURL, err := url.Parse(im) if err != nil { return diff --git a/backend/app/store/image/image_test.go b/backend/app/store/image/image_test.go index 274a6fc88e..e10d783e82 100644 --- a/backend/app/store/image/image_test.go +++ b/backend/app/store/image/image_test.go @@ -84,7 +84,7 @@ func TestService_ExtractPictures(t *testing.T) { svc := Service{ServiceParams: ServiceParams{ImageAPI: "/blah/", ProxyAPI: "/non_existent"}} html := `blah foo xyz
123
` - ids := svc.ExtractPictures(html) + ids := svc.ExtractPictures(html, false) require.Equal(t, 2, len(ids), "two images") assert.Equal(t, "user1/pic1.png", ids[0]) assert.Equal(t, "user2/pic3.png", ids[1]) @@ -96,30 +96,33 @@ func TestService_ExtractPictures(t *testing.T) { \n\n
По форме все верно, это все packages, но по сути это все одна библиотека организованная таким образом. При ее импорте, например посредством go mod, она выглядит как один модуль, т.е.
github.com/go-pkgz/auth v0.5.2
.