From 2acb00d4243f7ac190f30f76e9df04547a2a3479 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Sun, 19 Apr 2020 20:23:41 +0200 Subject: [PATCH] rename image.Store.SaveWithID to Save --- backend/_example/memory_store/accessor/image.go | 2 +- backend/_example/memory_store/accessor/image_test.go | 2 +- backend/_example/memory_store/server/image.go | 2 +- backend/_example/memory_store/server/image_test.go | 4 ++-- backend/app/rest/proxy/image_test.go | 4 ++-- backend/app/store/image/bolt_store.go | 4 ++-- backend/app/store/image/bolt_store_test.go | 6 +++--- backend/app/store/image/fs_store.go | 4 ++-- backend/app/store/image/fs_store_test.go | 12 ++++++------ backend/app/store/image/image.go | 8 ++++---- backend/app/store/image/image_mock.go | 4 ++-- backend/app/store/image/image_test.go | 2 +- backend/app/store/image/remote_store.go | 2 +- backend/app/store/image/remote_store_test.go | 2 +- 14 files changed, 29 insertions(+), 29 deletions(-) diff --git a/backend/_example/memory_store/accessor/image.go b/backend/_example/memory_store/accessor/image.go index 257c97a6..c6b2e02d 100644 --- a/backend/_example/memory_store/accessor/image.go +++ b/backend/_example/memory_store/accessor/image.go @@ -33,7 +33,7 @@ func NewMemImageStore() *MemImage { } } -func (m *MemImage) SaveWithID(id string, img []byte) error { +func (m *MemImage) Save(id string, img []byte) error { m.Lock() m.imagesStaging[id] = img m.insertTime[id] = time.Now() diff --git a/backend/_example/memory_store/accessor/image_test.go b/backend/_example/memory_store/accessor/image_test.go index a0499563..bf496d0b 100644 --- a/backend/_example/memory_store/accessor/image_test.go +++ b/backend/_example/memory_store/accessor/image_test.go @@ -51,7 +51,7 @@ func TestMemImage_LoadAfterSave(t *testing.T) { assert.Empty(t, img) id := "test_img" - err = svc.SaveWithID(id, gopher) + err = svc.Save(id, gopher) assert.NoError(t, err) img, err = svc.Load(id) diff --git a/backend/_example/memory_store/server/image.go b/backend/_example/memory_store/server/image.go index c158429e..9b550206 100644 --- a/backend/_example/memory_store/server/image.go +++ b/backend/_example/memory_store/server/image.go @@ -24,7 +24,7 @@ func (s *RPC) imgSaveWithIDHndl(id uint64, params json.RawMessage) (rr jrpc.Resp if err != nil { return jrpc.Response{Error: err.Error()} } - err = s.img.SaveWithID(req[0], img) + err = s.img.Save(req[0], img) return jrpc.EncodeResponse(id, nil, err) } diff --git a/backend/_example/memory_store/server/image_test.go b/backend/_example/memory_store/server/image_test.go index 03929356..18b94dcb 100644 --- a/backend/_example/memory_store/server/image_test.go +++ b/backend/_example/memory_store/server/image_test.go @@ -58,7 +58,7 @@ func TestRPC_imgLoadHndl(t *testing.T) { ri := image.RPC{Client: jrpc.Client{API: api, Client: http.Client{Timeout: 1 * time.Second}}} // save id := "test_img" - err := ri.SaveWithID(id, gopherPNGBytes()) + err := ri.Save(id, gopherPNGBytes()) assert.NoError(t, err) // load @@ -107,7 +107,7 @@ func TestRPC_imgCleanupHndl(t *testing.T) { // save id := "test_img" - err := ri.SaveWithID(id, gopherPNGBytes()) + err := ri.Save(id, gopherPNGBytes()) assert.NoError(t, err) // load diff --git a/backend/app/rest/proxy/image_test.go b/backend/app/rest/proxy/image_test.go index 6c69cb97..fd0458e0 100644 --- a/backend/app/rest/proxy/image_test.go +++ b/backend/app/rest/proxy/image_test.go @@ -175,7 +175,7 @@ func TestImage_RoutesCachingImage(t *testing.T) { encodedImgURL := base64.URLEncoding.EncodeToString([]byte(imgURL)) imageStore.On("Load", mock.Anything).Once().Return(nil, nil) - imageStore.On("SaveWithID", mock.Anything, mock.Anything).Once().Return(nil) + imageStore.On("Save", mock.Anything, mock.Anything).Once().Return(nil) imageStore.On("Commit", mock.Anything).Once().Return(nil) resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL) @@ -185,7 +185,7 @@ func TestImage_RoutesCachingImage(t *testing.T) { assert.Equal(t, "image/png", resp.Header["Content-Type"][0]) imageStore.AssertCalled(t, "Load", mock.Anything) - imageStore.AssertCalled(t, "SaveWithID", "cached_images/4b84b15bff6ee5796152495a230e45e3d7e947d9-"+sha1Str(imgURL), gopherPNGBytes()) + imageStore.AssertCalled(t, "Save", "cached_images/4b84b15bff6ee5796152495a230e45e3d7e947d9-"+sha1Str(imgURL), gopherPNGBytes()) imageStore.AssertCalled(t, "Commit", mock.Anything) } diff --git a/backend/app/store/image/bolt_store.go b/backend/app/store/image/bolt_store.go index a24cda25..5ed8d68c 100644 --- a/backend/app/store/image/bolt_store.go +++ b/backend/app/store/image/bolt_store.go @@ -51,8 +51,8 @@ func NewBoltStorage(fileName string, options bolt.Options) (*Bolt, error) { }, nil } -// SaveWithID saves image for given id to staging bucket in DB -func (b *Bolt) SaveWithID(id string, img []byte) error { +// Save saves image for given id to staging bucket in DB +func (b *Bolt) Save(id string, img []byte) error { err := b.db.Update(func(tx *bolt.Tx) error { if err := tx.Bucket([]byte(imagesStagedBktName)).Put([]byte(id), img); err != nil { return errors.Wrapf(err, "can't put to bucket with %s", id) diff --git a/backend/app/store/image/bolt_store_test.go b/backend/app/store/image/bolt_store_test.go index a252d1ee..2dce4cc5 100644 --- a/backend/app/store/image/bolt_store_test.go +++ b/backend/app/store/image/bolt_store_test.go @@ -20,7 +20,7 @@ func TestBoltStore_SaveCommit(t *testing.T) { id := "test_img" - err := svc.SaveWithID(id, gopherPNGBytes()) + err := svc.Save(id, gopherPNGBytes()) assert.NoError(t, err) err = svc.db.View(func(tx *bolt.Tx) error { @@ -49,7 +49,7 @@ func TestBoltStore_LoadAfterSave(t *testing.T) { defer teardown() id := "test_img" - err := svc.SaveWithID(id, gopherPNGBytes()) + err := svc.Save(id, gopherPNGBytes()) assert.NoError(t, err) data, err := svc.Load(id) @@ -66,7 +66,7 @@ func TestBoltStore_Cleanup(t *testing.T) { defer teardown() save := func(file string) (id string) { - err := svc.SaveWithID(file, gopherPNGBytes()) + err := svc.Save(file, gopherPNGBytes()) require.NoError(t, err) checkBoltImgData(t, svc.db, imagesStagedBktName, file, func(data []byte) error { diff --git a/backend/app/store/image/fs_store.go b/backend/app/store/image/fs_store.go index 913cdccc..420dde59 100644 --- a/backend/app/store/image/fs_store.go +++ b/backend/app/store/image/fs_store.go @@ -32,9 +32,9 @@ type FileSystem struct { } } -// SaveWithID saves image with given id to local FS, staging directory. +// Save saves image with given id to local FS, staging directory. // Files partitioned across multiple subdirectories, and the final path includes part, i.e. /location/user1/03/123-4567 -func (f *FileSystem) SaveWithID(id string, img []byte) error { +func (f *FileSystem) Save(id string, img []byte) error { dst := f.location(f.Staging, id) if err := os.MkdirAll(path.Dir(dst), 0700); err != nil { diff --git a/backend/app/store/image/fs_store_test.go b/backend/app/store/image/fs_store_test.go index 7d8deb9b..8171b808 100644 --- a/backend/app/store/image/fs_store_test.go +++ b/backend/app/store/image/fs_store_test.go @@ -49,7 +49,7 @@ func TestFsStore_Save(t *testing.T) { defer teardown() id := "test_img" - err := svc.SaveWithID(id, gopherPNGBytes()) + err := svc.Save(id, gopherPNGBytes()) assert.NoError(t, err) img := svc.location(svc.Staging, id) @@ -68,7 +68,7 @@ func TestFsStore_SaveNoResizeJpeg(t *testing.T) { img, err := ioutil.ReadAll(fh) assert.NoError(t, err) id := "test_img" - err = svc.SaveWithID(id, img) + err = svc.Save(id, img) assert.NoError(t, err) imgPath := svc.location(svc.Staging, id) @@ -83,7 +83,7 @@ func TestFsStore_SaveAndCommit(t *testing.T) { defer teardown() id := "test_img" - err := svc.SaveWithID(id, gopherPNGBytes()) + err := svc.Save(id, gopherPNGBytes()) require.NoError(t, err) err = svc.Commit(id) require.NoError(t, err) @@ -105,7 +105,7 @@ func TestFsStore_LoadAfterSave(t *testing.T) { defer teardown() id := "test_img" - err := svc.SaveWithID(id, gopherPNGBytes()) + err := svc.Save(id, gopherPNGBytes()) assert.NoError(t, err) t.Log(id) @@ -123,7 +123,7 @@ func TestFsStore_LoadAfterCommit(t *testing.T) { defer teardown() id := "test_img" - err := svc.SaveWithID(id, gopherPNGBytes()) + err := svc.Save(id, gopherPNGBytes()) assert.NoError(t, err) t.Log(id) err = svc.Commit(id) @@ -186,7 +186,7 @@ func TestFsStore_Cleanup(t *testing.T) { save := func(file string, user string) (filePath string) { id := path.Join(user, file) - err := svc.SaveWithID(id, gopherPNGBytes()) + err := svc.Save(id, gopherPNGBytes()) require.NoError(t, err) img := svc.location(svc.Staging, id) data, err := ioutil.ReadFile(img) diff --git a/backend/app/store/image/image.go b/backend/app/store/image/image.go index d3068a45..9ec5cac2 100644 --- a/backend/app/store/image/image.go +++ b/backend/app/store/image/image.go @@ -59,8 +59,8 @@ type ServiceParams struct { // Two-stage commit scheme is used for not storing images which are uploaded but later never used in the comments, // e.g. when somebody uploaded a picture but did not sent the comment. type Store interface { - SaveWithID(id string, img []byte) error // store image with passed id to staging - Load(id string) ([]byte, error) // load image by ID. Caller has to close the reader. + Save(id string, img []byte) error // store image with passed id to staging + Load(id string) ([]byte, error) // load image by ID. Caller has to close the reader. Commit(id string) error // move image from staging to permanent Cleanup(ctx context.Context, ttl time.Duration) error // run removal loop for old images on staging @@ -186,13 +186,13 @@ func (s *Service) Save(userID string, r io.Reader) (id string, err error) { return id, s.SaveWithID(id, r) } -// SaveWithID wraps storage SaveWithID function, validating and resizing the image before calling it. +// Save wraps storage Save function, validating and resizing the image before calling it. func (s *Service) SaveWithID(id string, r io.Reader) error { img, err := s.prepareImage(r) if err != nil { return err } - return s.store.SaveWithID(id, img) + return s.store.Save(id, img) } func (s *Service) ImgContentType(img []byte) string { diff --git a/backend/app/store/image/image_mock.go b/backend/app/store/image/image_mock.go index 8e0ea5e7..25e6775d 100644 --- a/backend/app/store/image/image_mock.go +++ b/backend/app/store/image/image_mock.go @@ -62,8 +62,8 @@ func (_m *MockStore) Load(id string) ([]byte, error) { return r0, r1 } -// SaveWithID provides a mock function with given fields: id, img -func (_m *MockStore) SaveWithID(id string, img []byte) error { +// Save provides a mock function with given fields: id, img +func (_m *MockStore) Save(id string, img []byte) error { ret := _m.Called(id, img) var r0 error diff --git a/backend/app/store/image/image_test.go b/backend/app/store/image/image_test.go index 47472b32..42157b15 100644 --- a/backend/app/store/image/image_test.go +++ b/backend/app/store/image/image_test.go @@ -21,7 +21,7 @@ func TestService_SaveAndLoad(t *testing.T) { store := MockStore{} svc := NewService(&store, ServiceParams{MaxSize: 1500, MaxWidth: 32, MaxHeight: 32}) - store.On("SaveWithID", "test_id", mock.Anything).Return(nil) + store.On("Save", "test_id", mock.Anything).Return(nil) err := svc.SaveWithID("test_id", gopherPNG()) assert.NoError(t, err) diff --git a/backend/app/store/image/remote_store.go b/backend/app/store/image/remote_store.go index 2073214e..1beed983 100644 --- a/backend/app/store/image/remote_store.go +++ b/backend/app/store/image/remote_store.go @@ -16,7 +16,7 @@ type RPC struct { jrpc.Client } -func (r *RPC) SaveWithID(id string, img []byte) error { +func (r *RPC) Save(id string, img []byte) error { _, err := r.Call("image.save_with_id", id, img) return err } diff --git a/backend/app/store/image/remote_store_test.go b/backend/app/store/image/remote_store_test.go index 43b99ddf..e0e214f2 100644 --- a/backend/app/store/image/remote_store_test.go +++ b/backend/app/store/image/remote_store_test.go @@ -23,7 +23,7 @@ func TestRemote_SaveWithID(t *testing.T) { var a Store = &c _ = a - err := c.SaveWithID("54321", gopherPNGBytes()) + err := c.Save("54321", gopherPNGBytes()) assert.NoError(t, err) }