Skip to content

Commit

Permalink
fix: should use storage key for delete on error (#473)
Browse files Browse the repository at this point in the history
* fix: should use storage key for delete on error

* add test case
  • Loading branch information
cshum authored Aug 24, 2024
1 parent a510db8 commit 7a98a6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion imagor.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,11 @@ func (app *Imagor) Do(r *http.Request, p imagorpath.Params) (blob *Blob, err err
app.save(ctx, app.ResultStorages, resultKey, blob)
}
if err != nil && shouldSave {
app.del(ctx, app.Storages, p.Image)
var storageKey = p.Image
if app.StoragePathStyle != nil {
storageKey = app.StoragePathStyle.Hash(p.Image)
}
app.del(ctx, app.Storages, storageKey)
}
return blob, err
})
Expand Down
18 changes: 16 additions & 2 deletions imagor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func TestWithInternal(t *testing.T) {
return NewBlobFromBytes([]byte("foo")), nil
})),
WithProcessors(processorFunc(func(ctx context.Context, blob *Blob, p imagorpath.Params, load LoadFunc) (*Blob, error) {
fmt.Println(p.Path, p.Image)
assert.Contains(t, p.Path, p.Image)
assert.Positive(t, p.Width)
assert.Positive(t, p.Height)
Expand Down Expand Up @@ -1040,6 +1039,12 @@ func TestWithStorageHasher(t *testing.T) {
}
return "storage:" + img
})),
WithProcessors(processorFunc(func(ctx context.Context, blob *Blob, p imagorpath.Params, load LoadFunc) (*Blob, error) {
if p.Image == "err" {
return nil, ErrInternal
}
return blob, nil
})),
WithUnsafe(true),
WithModifiedTimeCheck(true),
)
Expand Down Expand Up @@ -1074,12 +1079,21 @@ func TestWithStorageHasher(t *testing.T) {
assert.Equal(t, 200, w.Code)
assert.Equal(t, "bar", w.Body.String())

w = httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/err", nil))
time.Sleep(time.Millisecond * 10) // make sure storage reached
assert.Equal(t, 500, w.Code)
assert.Equal(t, jsonStr(ErrInternal), w.Body.String())

assert.Equal(t, 0, store.LoadCnt["storage:bar"])
assert.Equal(t, 0, store.SaveCnt["storage:bar"])
assert.Equal(t, 1, len(store.LoadCnt))
assert.Equal(t, 1, len(store.SaveCnt))
assert.Equal(t, 2, len(store.SaveCnt))
assert.Equal(t, 1, len(store.DelCnt))
assert.Equal(t, 1, loadCnt["foo"], 1)
assert.Equal(t, 2, loadCnt["bar"], 2)
assert.Equal(t, 1, store.DelCnt["storage:err"])
}

func TestClientCancel(t *testing.T) {
Expand Down

0 comments on commit 7a98a6f

Please sign in to comment.