2015-12-09 19:25:08 +00:00
|
|
|
package lockingstore
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/tus/tusd"
|
|
|
|
)
|
|
|
|
|
2015-12-09 19:25:08 +00:00
|
|
|
type Locker interface {
|
|
|
|
LockUpload(id string) error
|
|
|
|
UnlockUpload(id string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type LockingStore struct {
|
|
|
|
tusd.DataStore
|
2015-12-09 19:48:41 +00:00
|
|
|
Locker Locker
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) WriteChunk(id string, offset int64, src io.Reader) (n int64, err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.WriteChunk(id, offset, src)
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) GetInfo(id string) (info tusd.FileInfo, err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
|
|
|
return info, err
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.GetInfo(id)
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) GetReader(id string) (src io.Reader, err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.GetReader(id)
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) Terminate(id string) (err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.Terminate(id)
|
|
|
|
}
|