Skip to content

Commit

Permalink
reset image cleanup timer on comment preview
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed May 8, 2021
1 parent 899eb73 commit c950d70
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
13 changes: 13 additions & 0 deletions backend/app/rest/api/rest_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ func (s *public) previewCommentCtrl(w http.ResponseWriter, r *http.Request) {

comment = s.commentFormatter.Format(comment)
comment.Sanitize()

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.ResetCleanupTimer(id)
if err != nil {
rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't renew staged picture cleanup timer", rest.ErrAssetNotFound)
return
}
}
render.HTML(w, r, comment.Text)
}

Expand Down
19 changes: 18 additions & 1 deletion backend/app/rest/api/rest_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestRest_Ping(t *testing.T) {
}

func TestRest_Preview(t *testing.T) {
ts, _, teardown := startupT(t)
ts, srv, teardown := startupT(t)
defer teardown()

resp, err := post(t, ts.URL+"/api/v1/preview", `{"text": "test 123", "locator":{"url": "https://radio-t.com/blah1", "site": "radio-t"}}`)
Expand All @@ -43,6 +43,23 @@ func TestRest_Preview(t *testing.T) {
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())
assert.Equal(t, 400, resp.StatusCode)

resp, err = post(t, ts.URL+"/api/v1/preview", fmt.Sprintf(`{"text": "![non-existent.jpg](%s/api/v1/picture/dev_user/bad_picture)", "locator":{"url": "https://radio-t.com/blah1", "site": "radio-t"}}`, srv.RemarkURL))
assert.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
b, err = ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())
assert.Contains(t,
string(b),
"{\"code\":18,\"details\":\"can't renew staged picture cleanup timer\","+
"\"error\":\"can't get image stats for dev_user/bad_picture: stat",
)
assert.Contains(t,
string(b),
"/pics-remark42/staging/dev_user/62/bad_picture: no such file or directory\"}\n",
)

}

func TestRest_PreviewWithMD(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions backend/app/store/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ func (s *Service) Cleanup(ctx context.Context) {
}
}

// ResetCleanupTimer resets cleanup timer for the image
func (s *Service) ResetCleanupTimer(id string) error {
return s.store.ResetCleanupTimer(id)
}

// Info returns meta information about storage
func (s *Service) Info() (StoreInfo, error) {
return s.store.Info()
Expand Down

0 comments on commit c950d70

Please sign in to comment.