simplify image.Store interface
This commit is contained in:
committed by
Umputun
parent
b5e31f3081
commit
36cee9cc66
@@ -8,13 +8,11 @@ package accessor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/go-pkgz/lgr"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/xid"
|
||||
)
|
||||
|
||||
// MemImage implements image.Store with memory backend
|
||||
@@ -35,18 +33,13 @@ func NewMemImageStore() *MemImage {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MemImage) Save(userID string, img []byte) (id string, err error) {
|
||||
id = path.Join(userID, guid())
|
||||
return m.SaveWithID(id, img)
|
||||
}
|
||||
|
||||
func (m *MemImage) SaveWithID(id string, img []byte) (string, error) {
|
||||
func (m *MemImage) SaveWithID(id string, img []byte) error {
|
||||
m.Lock()
|
||||
m.imagesStaging[id] = img
|
||||
m.insertTime[id] = time.Now()
|
||||
m.Unlock()
|
||||
|
||||
return id, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemImage) Load(id string) ([]byte, error) {
|
||||
@@ -98,8 +91,3 @@ func (m *MemImage) Cleanup(_ context.Context, ttl time.Duration) error {
|
||||
m.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// guid makes a globally unique id
|
||||
func guid() string {
|
||||
return xid.New().String()
|
||||
}
|
||||
|
||||
@@ -41,20 +41,6 @@ const gopher = "iVBORw0KGgoAAAANSUhEUgAAAEsAAAA8CAAAAAALAhhPAAAFfUlEQVRYw62XeWwU
|
||||
|
||||
func gopherPNG() io.Reader { return base64.NewDecoder(base64.StdEncoding, strings.NewReader(gopher)) }
|
||||
|
||||
func TestMemImage_Save(t *testing.T) {
|
||||
svc := NewMemImageStore()
|
||||
id, err := svc.Save("user1", []byte(gopher))
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, id, "user1/")
|
||||
}
|
||||
|
||||
func TestMemImage_SaveWithIDFail(t *testing.T) {
|
||||
svc := NewMemImageStore()
|
||||
id, err := svc.SaveWithID("test_id", []byte(gopher))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, id, "test_id")
|
||||
}
|
||||
|
||||
func TestMemImage_LoadAfterSave(t *testing.T) {
|
||||
svc := NewMemImageStore()
|
||||
gopher, err := ioutil.ReadAll(gopherPNG())
|
||||
@@ -64,7 +50,8 @@ func TestMemImage_LoadAfterSave(t *testing.T) {
|
||||
assert.EqualError(t, err, "image test_id not found")
|
||||
assert.Empty(t, img)
|
||||
|
||||
id, err := svc.Save("user1", gopher)
|
||||
id := "test_img"
|
||||
err = svc.SaveWithID(id, gopher)
|
||||
assert.NoError(t, err)
|
||||
|
||||
img, err = svc.Load(id)
|
||||
|
||||
@@ -15,19 +15,6 @@ import (
|
||||
"github.com/go-pkgz/jrpc"
|
||||
)
|
||||
|
||||
func (s *RPC) imgSaveHndl(id uint64, params json.RawMessage) (rr jrpc.Response) {
|
||||
var req [2]string
|
||||
if err := json.Unmarshal(params, &req); err != nil {
|
||||
return jrpc.Response{Error: err.Error()}
|
||||
}
|
||||
img, err := base64.StdEncoding.DecodeString(req[1])
|
||||
if err != nil {
|
||||
return jrpc.Response{Error: err.Error()}
|
||||
}
|
||||
value, err := s.img.Save(req[0], img)
|
||||
return jrpc.EncodeResponse(id, value, err)
|
||||
}
|
||||
|
||||
func (s *RPC) imgSaveWithIDHndl(id uint64, params json.RawMessage) (rr jrpc.Response) {
|
||||
var req [2]string
|
||||
if err := json.Unmarshal(params, &req); err != nil {
|
||||
@@ -37,8 +24,8 @@ func (s *RPC) imgSaveWithIDHndl(id uint64, params json.RawMessage) (rr jrpc.Resp
|
||||
if err != nil {
|
||||
return jrpc.Response{Error: err.Error()}
|
||||
}
|
||||
value, err := s.img.SaveWithID(req[0], img)
|
||||
return jrpc.EncodeResponse(id, value, err)
|
||||
err = s.img.SaveWithID(req[0], img)
|
||||
return jrpc.EncodeResponse(id, nil, err)
|
||||
}
|
||||
|
||||
func (s *RPC) imgLoadHndl(id uint64, params json.RawMessage) (rr jrpc.Response) {
|
||||
|
||||
@@ -50,31 +50,6 @@ func gopherPNGBytes() []byte {
|
||||
return img
|
||||
}
|
||||
|
||||
func TestRPC_imgSaveHndl(t *testing.T) {
|
||||
_, port, teardown := prepTestStore(t)
|
||||
defer teardown()
|
||||
api := fmt.Sprintf("http://localhost:%d/test", port)
|
||||
|
||||
ri := image.RPC{Client: jrpc.Client{API: api, Client: http.Client{Timeout: 1 * time.Second}}}
|
||||
id, err := ri.Save("admin", gopherPNGBytes())
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, id, "admin/", "id contains username")
|
||||
|
||||
err = ri.Commit(id)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRPC_imgSaveWithIDHndl(t *testing.T) {
|
||||
_, port, teardown := prepTestStore(t)
|
||||
defer teardown()
|
||||
api := fmt.Sprintf("http://localhost:%d/test", port)
|
||||
|
||||
ri := image.RPC{Client: jrpc.Client{API: api, Client: http.Client{Timeout: 1 * time.Second}}}
|
||||
id, err := ri.SaveWithID("test_id", gopherPNGBytes())
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, id, "test_id")
|
||||
}
|
||||
|
||||
func TestRPC_imgLoadHndl(t *testing.T) {
|
||||
_, port, teardown := prepTestStore(t)
|
||||
defer teardown()
|
||||
@@ -82,7 +57,8 @@ func TestRPC_imgLoadHndl(t *testing.T) {
|
||||
|
||||
ri := image.RPC{Client: jrpc.Client{API: api, Client: http.Client{Timeout: 1 * time.Second}}}
|
||||
// save
|
||||
id, err := ri.Save("admin", gopherPNGBytes())
|
||||
id := "test_img"
|
||||
err := ri.SaveWithID(id, gopherPNGBytes())
|
||||
assert.NoError(t, err)
|
||||
|
||||
// load
|
||||
@@ -100,6 +76,16 @@ func TestRPC_imgLoadHndl(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1462, len(img))
|
||||
assert.Equal(t, gopherPNGBytes(), img)
|
||||
|
||||
// cleanup
|
||||
err = ri.Cleanup(nil, time.Second)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// load after cleanup
|
||||
img, err = ri.Load(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1462, len(img))
|
||||
assert.Equal(t, gopherPNGBytes(), img)
|
||||
}
|
||||
|
||||
func TestRPC_imgCommitHndlFail(t *testing.T) {
|
||||
@@ -120,12 +106,15 @@ func TestRPC_imgCleanupHndl(t *testing.T) {
|
||||
ri := image.RPC{Client: jrpc.Client{API: api, Client: http.Client{Timeout: 1 * time.Second}}}
|
||||
|
||||
// save
|
||||
id, err := ri.Save("admin", gopherPNGBytes())
|
||||
id := "test_img"
|
||||
err := ri.SaveWithID(id, gopherPNGBytes())
|
||||
assert.NoError(t, err)
|
||||
|
||||
// load
|
||||
_, err = ri.Load(id)
|
||||
img, err := ri.Load(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1462, len(img))
|
||||
assert.Equal(t, gopherPNGBytes(), img)
|
||||
|
||||
// cleanup
|
||||
err = ri.Cleanup(context.TODO(), time.Nanosecond)
|
||||
@@ -133,7 +122,5 @@ func TestRPC_imgCleanupHndl(t *testing.T) {
|
||||
|
||||
// load after cleanup should fail
|
||||
_, err = ri.Load(id)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "image admin/")
|
||||
assert.Contains(t, err.Error(), "not found")
|
||||
assert.EqualError(t, err, "image test_img not found")
|
||||
}
|
||||
|
||||
@@ -57,7 +57,6 @@ func (s *RPC) addHandlers() {
|
||||
|
||||
// image store handlers
|
||||
s.Group("image", jrpc.HandlersGroup{
|
||||
"save": s.imgSaveHndl,
|
||||
"save_with_id": s.imgSaveWithIDHndl,
|
||||
"load": s.imgLoadHndl,
|
||||
"commit": s.imgCommitHndl,
|
||||
|
||||
Reference in New Issue
Block a user