Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should use storage key for delete on error #473

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading