diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-04-10 22:13:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-10 22:13:42 +0800 |
commit | 9a5b227f3ee2b2a3854d3aec022cc9a0cf0868b3 (patch) | |
tree | 7600826f4affce28e95588de921d801f0414f046 /internal/db | |
parent | 3e055e329cf93eb5de77562d7795240808d31c08 (diff) |
lfsutil: add `Storager` interface and local storage (#6083)
* Add Storager interface
* Add tests
* Add back note
* Add tests for basic protocol routes
* Fix lint errors
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/lfs.go | 46 | ||||
-rw-r--r-- | internal/db/mocks.go | 7 |
2 files changed, 7 insertions, 46 deletions
diff --git a/internal/db/lfs.go b/internal/db/lfs.go index 8ae9e718..26a24df5 100644 --- a/internal/db/lfs.go +++ b/internal/db/lfs.go @@ -6,15 +6,10 @@ package db import ( "fmt" - "io" - "os" - "path/filepath" "time" "github.com/jinzhu/gorm" - "github.com/pkg/errors" - "gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/errutil" "gogs.io/gogs/internal/lfsutil" ) @@ -23,8 +18,8 @@ import ( // // NOTE: All methods are sorted in alphabetical order. type LFSStore interface { - // CreateObject streams io.ReadCloser to target storage and creates a record in database. - CreateObject(repoID int64, oid lfsutil.OID, rc io.ReadCloser, storage lfsutil.Storage) error + // CreateObject creates a LFS object record in database. + CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error // GetObjectByOID returns the LFS object with given OID. It returns ErrLFSObjectNotExist // when not found. GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error) @@ -48,44 +43,11 @@ type LFSObject struct { CreatedAt time.Time `gorm:"NOT NULL"` } -func (db *lfs) CreateObject(repoID int64, oid lfsutil.OID, rc io.ReadCloser, storage lfsutil.Storage) error { - if storage != lfsutil.StorageLocal { - return errors.New("only local storage is supported") - } - - var ioerr error - fpath := lfsutil.StorageLocalPath(conf.LFS.ObjectsPath, oid) - defer func() { - rc.Close() - - // NOTE: Only remove the file if there is an IO error, it is OK to - // leave the file when the whole operation failed with a DB error, - // a retry on client side can safely overwrite the same file as OID - // is seen as unique to every file. - if ioerr != nil { - _ = os.Remove(fpath) - } - }() - - ioerr = os.MkdirAll(filepath.Dir(fpath), os.ModePerm) - if ioerr != nil { - return errors.Wrap(ioerr, "create directories") - } - w, ioerr := os.Create(fpath) - if ioerr != nil { - return errors.Wrap(ioerr, "create file") - } - defer w.Close() - - written, ioerr := io.Copy(w, rc) - if ioerr != nil { - return errors.Wrap(ioerr, "copy file") - } - +func (db *lfs) CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error { object := &LFSObject{ RepoID: repoID, OID: oid, - Size: written, + Size: size, Storage: storage, } return db.DB.Create(object).Error diff --git a/internal/db/mocks.go b/internal/db/mocks.go index b81dab13..8cef2b96 100644 --- a/internal/db/mocks.go +++ b/internal/db/mocks.go @@ -5,7 +5,6 @@ package db import ( - "io" "testing" "gogs.io/gogs/internal/lfsutil" @@ -39,13 +38,13 @@ func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) { var _ LFSStore = (*MockLFSStore)(nil) type MockLFSStore struct { - MockCreateObject func(repoID int64, oid lfsutil.OID, rc io.ReadCloser, storage lfsutil.Storage) error + MockCreateObject func(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error MockGetObjectByOID func(repoID int64, oid lfsutil.OID) (*LFSObject, error) MockGetObjectsByOIDs func(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error) } -func (m *MockLFSStore) CreateObject(repoID int64, oid lfsutil.OID, rc io.ReadCloser, storage lfsutil.Storage) error { - return m.MockCreateObject(repoID, oid, rc, storage) +func (m *MockLFSStore) CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error { + return m.MockCreateObject(repoID, oid, size, storage) } func (m *MockLFSStore) GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error) { |