use tusd.FileInfo in DataStore.NewUpload

This commit is contained in:
Acconut 2015-02-16 17:53:50 +01:00
parent 0f79383af2
commit 0c16aedc29
7 changed files with 23 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
tusd/data
cover.out
data/

View File

@ -19,8 +19,8 @@ type DataStore interface {
// Create a new upload using the size as the file's length. The method must
// return an unique id which is used to identify the upload. If no backend
// (e.g. Riak) specifes the id you may want to use the uid package to
// generate one.
NewUpload(size int64, metaData MetaData) (id string, err error)
// generate one. The properties Size and MetaData will be filled.
NewUpload(info FileInfo) (id string, err error)
// Write the chunk read from src into the file specified by the id at the
// given offset. The handler will take care of validating the offset and
// limiting the size of the src to not overflow the file's size. It may

View File

@ -26,14 +26,9 @@ type FileStore struct {
Path string
}
func (store FileStore) NewUpload(size int64, metaData tusd.MetaData) (id string, err error) {
func (store FileStore) NewUpload(info tusd.FileInfo) (id string, err error) {
id = uid.Uid()
info := tusd.FileInfo{
Id: id,
Size: size,
Offset: 0,
MetaData: metaData,
}
info.Id = id
// Create .bin file with no content
file, err := os.OpenFile(store.binPath(id), os.O_CREATE|os.O_WRONLY, defaultFilePerm)

View File

@ -4,6 +4,8 @@ import (
"io/ioutil"
"strings"
"testing"
"github.com/tus/tusd"
)
func TestFilestore(t *testing.T) {
@ -15,8 +17,11 @@ func TestFilestore(t *testing.T) {
store := FileStore{tmp}
// Create new upload
id, err := store.NewUpload(42, map[string]string{
"hello": "world",
id, err := store.NewUpload(tusd.FileInfo{
Size: 42,
MetaData: map[string]string{
"hello": "world",
},
})
if err != nil {
t.Fatal(err)

View File

@ -168,7 +168,12 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
// Parse metadata
meta := parseMeta(r.Header.Get("Metadata"))
id, err := handler.dataStore.NewUpload(size, meta)
info := FileInfo{
Size: size,
MetaData: meta,
}
id, err := handler.dataStore.NewUpload(info)
if err != nil {
handler.sendError(w, err)
return

View File

@ -6,7 +6,7 @@ import (
type zeroStore struct{}
func (store zeroStore) NewUpload(size int64, metaData MetaData) (string, error) {
func (store zeroStore) NewUpload(info FileInfo) (string, error) {
return "", nil
}
func (store zeroStore) WriteChunk(id string, offset int64, src io.Reader) error {

View File

@ -11,11 +11,12 @@ type postStore struct {
zeroStore
}
func (s postStore) NewUpload(size int64, metaData MetaData) (string, error) {
if size != 300 {
s.t.Errorf("Expected size to be 300 (got %v)", size)
func (s postStore) NewUpload(info FileInfo) (string, error) {
if info.Size != 300 {
s.t.Errorf("Expected size to be 300 (got %v)", info.Size)
}
metaData := info.MetaData
if len(metaData) != 2 {
s.t.Errorf("Expected two elements in metadata")
}