From 7a98a6f6adfc4aaba08986dd999df4898b31fc09 Mon Sep 17 00:00:00 2001 From: Adrian Shum Date: Sun, 25 Aug 2024 00:51:47 +0800 Subject: [PATCH] fix: should use storage key for delete on error (#473) * fix: should use storage key for delete on error * add test case --- imagor.go | 6 +++++- imagor_test.go | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) 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 }) 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) {