Add length deferrer support to FileStore

This commit is contained in:
Adam Jensen 2018-05-13 10:28:02 -04:00
parent d40c50f80a
commit 5bf2133b3c
2 changed files with 37 additions and 0 deletions

View File

@ -55,6 +55,7 @@ func (store FileStore) UseIn(composer *tusd.StoreComposer) {
composer.UseTerminater(store) composer.UseTerminater(store)
composer.UseLocker(store) composer.UseLocker(store)
composer.UseConcater(store) composer.UseConcater(store)
composer.UseLengthDeferrer(store)
} }
func (store FileStore) NewUpload(info tusd.FileInfo) (id string, err error) { func (store FileStore) NewUpload(info tusd.FileInfo) (id string, err error) {
@ -142,6 +143,16 @@ func (store FileStore) ConcatUploads(dest string, uploads []string) (err error)
return return
} }
func (store FileStore) DeclareLength(id string, length int64) error {
info, err := store.GetInfo(id)
if err != nil {
return err
}
info.Size = length
info.SizeIsDeferred = false
return store.writeInfo(id, info)
}
func (store FileStore) LockUpload(id string) error { func (store FileStore) LockUpload(id string) error {
lock, err := store.newLock(id) lock, err := store.newLock(id)
if err != nil { if err != nil {

View File

@ -18,6 +18,7 @@ var _ tusd.GetReaderDataStore = FileStore{}
var _ tusd.TerminaterDataStore = FileStore{} var _ tusd.TerminaterDataStore = FileStore{}
var _ tusd.LockerDataStore = FileStore{} var _ tusd.LockerDataStore = FileStore{}
var _ tusd.ConcaterDataStore = FileStore{} var _ tusd.ConcaterDataStore = FileStore{}
var _ tusd.LengthDeferrerDataStore = FileStore{}
func TestFilestore(t *testing.T) { func TestFilestore(t *testing.T) {
a := assert.New(t) a := assert.New(t)
@ -146,3 +147,28 @@ func TestConcatUploads(t *testing.T) {
a.Equal("abcdefghi", string(content)) a.Equal("abcdefghi", string(content))
reader.(io.Closer).Close() reader.(io.Closer).Close()
} }
func TestDeclareLength(t *testing.T) {
a := assert.New(t)
tmp, err := ioutil.TempDir("", "tusd-filestore-declare-length-")
a.NoError(err)
store := FileStore{tmp}
originalInfo := tusd.FileInfo{Size: 0, SizeIsDeferred: true}
id, err := store.NewUpload(originalInfo)
a.NoError(err)
info, err := store.GetInfo(id)
a.Equal(info.Size, originalInfo.Size)
a.Equal(info.SizeIsDeferred, originalInfo.SizeIsDeferred)
size := int64(100)
err = store.DeclareLength(id, size)
a.NoError(err)
updatedInfo, err := store.GetInfo(id)
a.Equal(updatedInfo.Size, size)
a.False(updatedInfo.SizeIsDeferred)
}