From c52e5b392033f6d12635bbfcb7e34de277c6c274 Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 12 Apr 2019 10:13:14 -0500 Subject: [PATCH] expose max image size via config rest #307 --- backend/app/rest/api/rest_public.go | 2 + backend/app/rest/api/rest_public_test.go | 1 + backend/app/store/image/fs_store.go | 5 ++ backend/app/store/image/image.go | 1 + backend/app/store/image/image_mock.go | 78 +++++++++++------------- 5 files changed, 43 insertions(+), 44 deletions(-) diff --git a/backend/app/rest/api/rest_public.go b/backend/app/rest/api/rest_public.go index 92f26ad5..0e6ea5b4 100644 --- a/backend/app/rest/api/rest_public.go +++ b/backend/app/rest/api/rest_public.go @@ -227,6 +227,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { CriticalScore int `json:"critical_score"` PositiveScore bool `json:"positive_score"` ReadOnlyAge int `json:"readonly_age"` + MaxImageSize int `json:"max_image_size"` } cnf := config{ @@ -239,6 +240,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { CriticalScore: s.ScoreThresholds.Critical, PositiveScore: s.DataService.PositiveScore, ReadOnlyAge: s.ReadOnlyAge, + MaxImageSize: s.ImageService.Store.SizeLimit(), } cnf.Auth = []string{} diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index e3343a8f..e5029988 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -425,6 +425,7 @@ func TestRest_Config(t *testing.T) { assert.Equal(t, -10., j["critical_score"]) assert.False(t, j["positive_score"].(bool)) assert.Equal(t, 10., j["readonly_age"]) + assert.Equal(t, 10000., j["max_image_size"]) t.Logf("%+v", j) } diff --git a/backend/app/store/image/fs_store.go b/backend/app/store/image/fs_store.go index cc99aec7..fc35fd75 100644 --- a/backend/app/store/image/fs_store.go +++ b/backend/app/store/image/fs_store.go @@ -136,6 +136,11 @@ func (f *FileSystem) Cleanup(ctx context.Context, ttl time.Duration) error { return errors.Wrap(err, "failed to cleanup images") } +// SizeLimit returns max size of allowed image +func (f *FileSystem) SizeLimit() int { + return f.MaxSize +} + // location gets full path for id by adding partition to the final path in order to keep files in different subdirectories // and avoid too many files in a single place. // the end result is a full path like this - /tmp/images/user1/92/xxx-yyy.png. diff --git a/backend/app/store/image/image.go b/backend/app/store/image/image.go index 69230375..112fbd81 100644 --- a/backend/app/store/image/image.go +++ b/backend/app/store/image/image.go @@ -25,6 +25,7 @@ type Store interface { Commit(id string) error // move image from staging to permanent Load(id string) (io.ReadCloser, int64, error) // load image by ID. Caller has to close the reader. Cleanup(ctx context.Context, ttl time.Duration) error // run removal loop for old images on staging + SizeLimit() int // max image size } // Service extends Store with common functions needed for any store implementation diff --git a/backend/app/store/image/image_mock.go b/backend/app/store/image/image_mock.go index 0049e558..7447bbae 100644 --- a/backend/app/store/image/image_mock.go +++ b/backend/app/store/image/image_mock.go @@ -1,95 +1,85 @@ -// Code generated by MockGen. DO NOT EDIT. +// Automatically generated by MockGen. DO NOT EDIT! // Source: image.go -// Package image is a generated GoMock package. package image import ( context "context" gomock "github.com/golang/mock/gomock" io "io" - reflect "reflect" time "time" ) -// MockStore is a mock of Store interface +// Mock of Store interface type MockStore struct { ctrl *gomock.Controller - recorder *MockStoreMockRecorder + recorder *_MockStoreRecorder } -// MockStoreMockRecorder is the mock recorder for MockStore -type MockStoreMockRecorder struct { +// Recorder for MockStore (not exported) +type _MockStoreRecorder struct { mock *MockStore } -// NewMockStore creates a new mock instance func NewMockStore(ctrl *gomock.Controller) *MockStore { mock := &MockStore{ctrl: ctrl} - mock.recorder = &MockStoreMockRecorder{mock} + mock.recorder = &_MockStoreRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockStore) EXPECT() *MockStoreMockRecorder { - return m.recorder +func (_m *MockStore) EXPECT() *_MockStoreRecorder { + return _m.recorder } -// Save mocks base method -func (m *MockStore) Save(fileName, userID string, r io.Reader) (string, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Save", fileName, userID, r) +func (_m *MockStore) Save(fileName string, userID string, r io.Reader) (string, error) { + ret := _m.ctrl.Call(_m, "Save", fileName, userID, r) ret0, _ := ret[0].(string) ret1, _ := ret[1].(error) return ret0, ret1 } -// Save indicates an expected call of Save -func (mr *MockStoreMockRecorder) Save(fileName, userID, r interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockStore)(nil).Save), fileName, userID, r) +func (_mr *_MockStoreRecorder) Save(arg0, arg1, arg2 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Save", arg0, arg1, arg2) } -// Commit mocks base method -func (m *MockStore) Commit(id string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Commit", id) +func (_m *MockStore) Commit(id string) error { + ret := _m.ctrl.Call(_m, "Commit", id) ret0, _ := ret[0].(error) return ret0 } -// Commit indicates an expected call of Commit -func (mr *MockStoreMockRecorder) Commit(id interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Commit", reflect.TypeOf((*MockStore)(nil).Commit), id) +func (_mr *_MockStoreRecorder) Commit(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Commit", arg0) } -// Load mocks base method -func (m *MockStore) Load(id string) (io.ReadCloser, int64, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Load", id) +func (_m *MockStore) Load(id string) (io.ReadCloser, int64, error) { + ret := _m.ctrl.Call(_m, "Load", id) ret0, _ := ret[0].(io.ReadCloser) ret1, _ := ret[1].(int64) ret2, _ := ret[2].(error) return ret0, ret1, ret2 } -// Load indicates an expected call of Load -func (mr *MockStoreMockRecorder) Load(id interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Load", reflect.TypeOf((*MockStore)(nil).Load), id) +func (_mr *_MockStoreRecorder) Load(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Load", arg0) } -// Cleanup mocks base method -func (m *MockStore) Cleanup(ctx context.Context, ttl time.Duration) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Cleanup", ctx, ttl) +func (_m *MockStore) Cleanup(ctx context.Context, ttl time.Duration) error { + ret := _m.ctrl.Call(_m, "Cleanup", ctx, ttl) ret0, _ := ret[0].(error) return ret0 } -// Cleanup indicates an expected call of Cleanup -func (mr *MockStoreMockRecorder) Cleanup(ctx, ttl interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cleanup", reflect.TypeOf((*MockStore)(nil).Cleanup), ctx, ttl) +func (_mr *_MockStoreRecorder) Cleanup(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Cleanup", arg0, arg1) +} + +func (_m *MockStore) SizeLimit() int { + ret := _m.ctrl.Call(_m, "SizeLimit") + ret0, _ := ret[0].(int) + return ret0 +} + +func (_mr *_MockStoreRecorder) SizeLimit() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "SizeLimit") }