From c0c3a4d1d03fba70f14065dc61046920cf52ea2c Mon Sep 17 00:00:00 2001 From: Marius Date: Mon, 19 Aug 2019 09:29:56 +0200 Subject: [PATCH] filestore: Add information about saved upload --- cmd/tusd/cli/composer.go | 6 +++++- pkg/filestore/filestore.go | 14 ++++++++++++-- pkg/filestore/filestore_test.go | 4 ++++ pkg/handler/datastore.go | 4 ++++ pkg/handler/unrouted_handler.go | 1 + 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/tusd/cli/composer.go b/cmd/tusd/cli/composer.go index b774851..0eea9cb 100644 --- a/cmd/tusd/cli/composer.go +++ b/cmd/tusd/cli/composer.go @@ -2,6 +2,7 @@ package cli import ( "os" + "path/filepath" "github.com/tus/tusd/pkg/filestore" "github.com/tus/tusd/pkg/gcsstore" @@ -61,7 +62,10 @@ func CreateComposer() { locker := memorylocker.New() locker.UseIn(Composer) } else { - dir := Flags.UploadDir + dir, err := filepath.Abs(Flags.UploadDir) + if err != nil { + stderr.Fatalf("Unable to make absolute path: %s", err) + } stdout.Printf("Using '%s' as directory storage.\n", dir) if err := os.MkdirAll(dir, os.FileMode(0774)); err != nil { diff --git a/pkg/filestore/filestore.go b/pkg/filestore/filestore.go index 4b0657b..8fdf534 100644 --- a/pkg/filestore/filestore.go +++ b/pkg/filestore/filestore.go @@ -60,10 +60,15 @@ func (store FileStore) UseIn(composer *handler.StoreComposer) { func (store FileStore) NewUpload(info handler.FileInfo) (id string, err error) { id = uid.Uid() + binPath := store.binPath(id) info.ID = id + info.Storage = map[string]string{ + "Type": "filestore", + "Path": binPath, + } // Create .bin file with no content - file, err := os.OpenFile(store.binPath(id), os.O_CREATE|os.O_WRONLY, defaultFilePerm) + file, err := os.OpenFile(binPath, os.O_CREATE|os.O_WRONLY, defaultFilePerm) if err != nil { if os.IsNotExist(err) { err = fmt.Errorf("upload directory does not exist: %s", store.Path) @@ -107,12 +112,17 @@ func (store FileStore) GetInfo(id string) (handler.FileInfo, error) { return info, err } - stat, err := os.Stat(store.binPath(id)) + binPath := store.binPath(id) + stat, err := os.Stat(binPath) if err != nil { return info, err } info.Offset = stat.Size() + info.Storage = map[string]string{ + "Type": "filestore", + "Path": binPath, + } return info, nil } diff --git a/pkg/filestore/filestore_test.go b/pkg/filestore/filestore_test.go index 99a6dec..e886c1c 100644 --- a/pkg/filestore/filestore_test.go +++ b/pkg/filestore/filestore_test.go @@ -4,6 +4,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "strings" "testing" @@ -44,6 +45,9 @@ func TestFilestore(t *testing.T) { a.EqualValues(42, info.Size) a.EqualValues(0, info.Offset) a.Equal(handler.MetaData{"hello": "world"}, info.MetaData) + a.Equal(2, len(info.Storage)) + a.Equal("filestore", info.Storage["Type"]) + a.Equal(filepath.Join(tmp, id+".bin"), info.Storage["Path"]) // Write data to upload bytesWritten, err := store.WriteChunk(id, 0, strings.NewReader("hello world")) diff --git a/pkg/handler/datastore.go b/pkg/handler/datastore.go index 14a2706..6e9353c 100644 --- a/pkg/handler/datastore.go +++ b/pkg/handler/datastore.go @@ -26,6 +26,10 @@ type FileInfo struct { // ordered slice containing the ids of the uploads of which the final upload // will consist after concatenation. PartialUploads []string + // Storage contains information about where the data storage saves the upload, + // for example a file path. The available values vary depending on what data + // store is used. This map may also be nil. + Storage map[string]string // stopUpload is the cancel function for the upload's context.Context. When // invoked it will interrupt the writes to DataStore#WriteChunk. diff --git a/pkg/handler/unrouted_handler.go b/pkg/handler/unrouted_handler.go index 0dbb8d4..693c840 100644 --- a/pkg/handler/unrouted_handler.go +++ b/pkg/handler/unrouted_handler.go @@ -303,6 +303,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request) return } + // TODO: Should we use GetInfo here? info.ID = id // Add the Location header directly after creating the new resource to even