From b5d4c77ffbbba33cdc1c83f8cf0949260123604b Mon Sep 17 00:00:00 2001 From: Adrian Shum Date: Sat, 24 Aug 2024 23:50:53 +0800 Subject: [PATCH 1/2] fix: should use storage key for delete on error --- imagor.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/imagor.go b/imagor.go index 3428dc6f3..49e51ba32 100644 --- a/imagor.go +++ b/imagor.go @@ -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 }) From 3c13ec5b8fd2f3f173b55f6d806434fce5453664 Mon Sep 17 00:00:00 2001 From: Adrian Shum Date: Sun, 25 Aug 2024 00:42:10 +0800 Subject: [PATCH 2/2] add test case --- imagor_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/imagor_test.go b/imagor_test.go index 11c9c48fb..5c85f546e 100644 --- a/imagor_test.go +++ b/imagor_test.go @@ -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) @@ -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), ) @@ -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) {