rename image.Store.SaveWithID to Save
This commit is contained in:
committed by
Umputun
parent
36cee9cc66
commit
2acb00d424
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user