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 tusd/data
cover.out 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 // 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 // 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 // (e.g. Riak) specifes the id you may want to use the uid package to
// generate one. // generate one. The properties Size and MetaData will be filled.
NewUpload(size int64, metaData MetaData) (id string, err error) NewUpload(info FileInfo) (id string, err error)
// Write the chunk read from src into the file specified by the id at the // 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 // 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 // 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 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() id = uid.Uid()
info := tusd.FileInfo{ info.Id = id
Id: id,
Size: size,
Offset: 0,
MetaData: metaData,
}
// Create .bin file with no content // Create .bin file with no content
file, err := os.OpenFile(store.binPath(id), os.O_CREATE|os.O_WRONLY, defaultFilePerm) file, err := os.OpenFile(store.binPath(id), os.O_CREATE|os.O_WRONLY, defaultFilePerm)

View File

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

View File

@ -168,7 +168,12 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
// Parse metadata // Parse metadata
meta := parseMeta(r.Header.Get("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 { if err != nil {
handler.sendError(w, err) handler.sendError(w, err)
return return

View File

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

View File

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