exclude unused filename from image.Store.Save() parameters

This commit is contained in:
Dmitry Verkhoturov
2020-03-26 23:04:49 +01:00
parent d714e32668
commit ef6634e9b3
7 changed files with 30 additions and 34 deletions
+2 -2
View File
@@ -568,14 +568,14 @@ func (s *private) savePictureCtrl(w http.ResponseWriter, r *http.Request) {
return
}
file, header, err := r.FormFile("file")
file, _, err := r.FormFile("file")
if err != nil {
rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "can't get image file from the request", rest.ErrInternal)
return
}
defer func() { _ = file.Close() }()
id, err := s.imageService.Save(header.Filename, user.ID, file)
id, err := s.imageService.Save(user.ID, file)
if err != nil {
rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't save image", rest.ErrInternal)
return
+1 -1
View File
@@ -86,7 +86,7 @@ func (b *Bolt) SaveWithID(id string, r io.Reader) (string, error) {
}
// Save data from reader to staging bucket in DB
func (b *Bolt) Save(_ string, userID string, r io.Reader) (id string, err error) {
func (b *Bolt) Save(userID string, r io.Reader) (id string, err error) {
id = path.Join(userID, guid())
return b.SaveWithID(id, r)
}
+3 -3
View File
@@ -18,7 +18,7 @@ func TestBoltStore_SaveCommit(t *testing.T) {
svc, teardown := prepareBoltImageStorageTest(t)
defer teardown()
id, err := svc.Save("file1.png", "user1", gopherPNG())
id, err := svc.Save("user1", gopherPNG())
assert.NoError(t, err)
assert.Contains(t, id, "user1")
t.Log(id)
@@ -48,7 +48,7 @@ func TestBoltStore_LoadAfterSave(t *testing.T) {
svc, teardown := prepareBoltImageStorageTest(t)
defer teardown()
id, err := svc.Save("file1.png", "user1", gopherPNG())
id, err := svc.Save("user1", gopherPNG())
assert.NoError(t, err)
assert.Contains(t, id, "user1")
t.Log(id)
@@ -66,7 +66,7 @@ func TestBoltStore_Cleanup(t *testing.T) {
defer teardown()
save := func(file string, user string) (id string) {
id, err := svc.Save(file, user, gopherPNG())
id, err := svc.Save(user, gopherPNG())
require.NoError(t, err)
checkBoltImgData(t, svc.db, imagesStagedBktName, id, func(data []byte) error {
+3 -7
View File
@@ -58,15 +58,11 @@ func (f *FileSystem) SaveWithID(id string, r io.Reader) (string, error) {
return id, nil
}
// Save data from a reader for given file name to local FS, staging directory. Returns id as user/uuid
// Save data from a reader to local FS, staging directory. Returns id as user/uuid
// Files partitioned across multiple subdirectories, and the final path includes part, i.e. /location/user1/03/123-4567
func (f *FileSystem) Save(fileName string, userID string, r io.Reader) (id string, err error) {
func (f *FileSystem) Save(userID string, r io.Reader) (id string, err error) {
tempId := path.Join(userID, guid()) // make id as user/uuid
id, err = f.SaveWithID(tempId, r)
if err != nil {
err = errors.Wrapf(err, "can't save image file %s", fileName)
}
return id, err
return f.SaveWithID(tempId, r)
}
// Commit file stored in staging location by moving it to permanent location
+10 -10
View File
@@ -44,7 +44,7 @@ func TestFsStore_Save(t *testing.T) {
svc, teardown := prepareImageTest(t)
defer teardown()
id, err := svc.Save("file1.png", "user1", gopherPNG())
id, err := svc.Save("user1", gopherPNG())
assert.NoError(t, err)
assert.Contains(t, id, "user1/")
t.Log(id)
@@ -61,7 +61,7 @@ func TestFsStore_SaveWithResize(t *testing.T) {
defer teardown()
svc.MaxWidth, svc.MaxHeight = 32, 32
id, err := svc.Save("file1.png", "user1", gopherPNG())
id, err := svc.Save("user1", gopherPNG())
assert.NoError(t, err)
assert.Contains(t, id, "user1/")
t.Log(id)
@@ -82,7 +82,7 @@ func TestFsStore_SaveWithResizeJpeg(t *testing.T) {
fh, err := os.Open("testdata/circles.jpg")
defer func() { assert.NoError(t, fh.Close()) }()
assert.NoError(t, err)
id, err := svc.Save("circles.jpg", "user1", fh)
id, err := svc.Save("user1", fh)
assert.NoError(t, err)
assert.Contains(t, id, "user1/")
t.Log(id)
@@ -103,7 +103,7 @@ func TestFsStore_SaveNoResizeJpeg(t *testing.T) {
fh, err := os.Open("testdata/circles.jpg")
defer func() { assert.NoError(t, fh.Close()) }()
assert.NoError(t, err)
id, err := svc.Save("circles.jpg", "user1", fh)
id, err := svc.Save("user1", fh)
assert.NoError(t, err)
assert.Contains(t, id, "user1/")
t.Log(id)
@@ -119,7 +119,7 @@ func TestFsStore_WrongFormat(t *testing.T) {
svc, teardown := prepareImageTest(t)
defer teardown()
_, err := svc.Save("file1.png", "user1", strings.NewReader("blah blah bad image"))
_, err := svc.Save("user1", strings.NewReader("blah blah bad image"))
assert.Error(t, err)
}
@@ -127,7 +127,7 @@ func TestFsStore_SaveAndCommit(t *testing.T) {
svc, teardown := prepareImageTest(t)
defer teardown()
id, err := svc.Save("file1.png", "user1", gopherPNG())
id, err := svc.Save("user1", gopherPNG())
require.NoError(t, err)
err = svc.Commit(id)
require.NoError(t, err)
@@ -147,7 +147,7 @@ func TestFsStore_SaveTooLarge(t *testing.T) {
svc, teardown := prepareImageTest(t)
defer teardown()
svc.MaxSize = 2000
_, err := svc.Save("blah_ff1.png", "user2", io.MultiReader(gopherPNG(), gopherPNG()))
_, err := svc.Save("user2", io.MultiReader(gopherPNG(), gopherPNG()))
assert.Error(t, err)
assert.Contains(t, err.Error(), "is too large")
}
@@ -157,7 +157,7 @@ func TestFsStore_LoadAfterSave(t *testing.T) {
svc, teardown := prepareImageTest(t)
defer teardown()
id, err := svc.Save("blah_ff1.png", "user1", gopherPNG())
id, err := svc.Save("user1", gopherPNG())
assert.NoError(t, err)
t.Log(id)
@@ -173,7 +173,7 @@ func TestFsStore_LoadAfterCommit(t *testing.T) {
svc, teardown := prepareImageTest(t)
defer teardown()
id, err := svc.Save("blah_ff1.png", "user1", gopherPNG())
id, err := svc.Save("user1", gopherPNG())
assert.NoError(t, err)
t.Log(id)
err = svc.Commit(id)
@@ -235,7 +235,7 @@ func TestFsStore_Cleanup(t *testing.T) {
defer teardown()
save := func(file string, user string) (path string) {
id, err := svc.Save(file, user, gopherPNG())
id, err := svc.Save(user, gopherPNG())
require.NoError(t, err)
img := svc.location(svc.Staging, id)
data, err := ioutil.ReadFile(img)
+4 -4
View File
@@ -47,10 +47,10 @@ type Service struct {
// Store defines interface for saving and loading pictures.
// Declares two-stage save with Commit. Save stores to staging area and Commit moves to the final location
type Store interface {
Save(fileName string, userID string, r io.Reader) (id string, err error) // get name and reader and returns ID of stored (staging) image
SaveWithID(id string, r io.Reader) (string, error) // store image for passed id to staging
Load(id string) ([]byte, error) // load image by ID. Caller has to close the reader.
SizeLimit() int // max image size
Save(userID string, r io.Reader) (id string, err error) // get name and reader and returns ID of stored (staging) image
SaveWithID(id string, r io.Reader) (string, error) // store image for passed id to staging
Load(id string) ([]byte, error) // load image by ID. Caller has to close the reader.
SizeLimit() int // max image size
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
+7 -7
View File
@@ -63,20 +63,20 @@ func (_m *MockStore) Load(id string) ([]byte, error) {
return r0, r1
}
// Save provides a mock function with given fields: fileName, userID, r
func (_m *MockStore) Save(fileName string, userID string, r io.Reader) (string, error) {
ret := _m.Called(fileName, userID, r)
// Save provides a mock function with given fields: userID, r
func (_m *MockStore) Save(userID string, r io.Reader) (string, error) {
ret := _m.Called(userID, r)
var r0 string
if rf, ok := ret.Get(0).(func(string, string, io.Reader) string); ok {
r0 = rf(fileName, userID, r)
if rf, ok := ret.Get(0).(func(string, io.Reader) string); ok {
r0 = rf(userID, r)
} else {
r0 = ret.Get(0).(string)
}
var r1 error
if rf, ok := ret.Get(1).(func(string, string, io.Reader) error); ok {
r1 = rf(fileName, userID, r)
if rf, ok := ret.Get(1).(func(string, io.Reader) error); ok {
r1 = rf(userID, r)
} else {
r1 = ret.Error(1)
}