filestore: Add information about saved upload

This commit is contained in:
Marius 2019-08-19 09:29:56 +02:00
parent e8fb3a431b
commit c0c3a4d1d0
5 changed files with 26 additions and 3 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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"))

View File

@ -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.

View File

@ -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