filestore: Add information about saved upload
This commit is contained in:
parent
e8fb3a431b
commit
c0c3a4d1d0
|
@ -2,6 +2,7 @@ package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/tus/tusd/pkg/filestore"
|
"github.com/tus/tusd/pkg/filestore"
|
||||||
"github.com/tus/tusd/pkg/gcsstore"
|
"github.com/tus/tusd/pkg/gcsstore"
|
||||||
|
@ -61,7 +62,10 @@ func CreateComposer() {
|
||||||
locker := memorylocker.New()
|
locker := memorylocker.New()
|
||||||
locker.UseIn(Composer)
|
locker.UseIn(Composer)
|
||||||
} else {
|
} 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)
|
stdout.Printf("Using '%s' as directory storage.\n", dir)
|
||||||
if err := os.MkdirAll(dir, os.FileMode(0774)); err != nil {
|
if err := os.MkdirAll(dir, os.FileMode(0774)); err != nil {
|
||||||
|
|
|
@ -60,10 +60,15 @@ func (store FileStore) UseIn(composer *handler.StoreComposer) {
|
||||||
|
|
||||||
func (store FileStore) NewUpload(info handler.FileInfo) (id string, err error) {
|
func (store FileStore) NewUpload(info handler.FileInfo) (id string, err error) {
|
||||||
id = uid.Uid()
|
id = uid.Uid()
|
||||||
|
binPath := store.binPath(id)
|
||||||
info.ID = id
|
info.ID = id
|
||||||
|
info.Storage = map[string]string{
|
||||||
|
"Type": "filestore",
|
||||||
|
"Path": binPath,
|
||||||
|
}
|
||||||
|
|
||||||
// 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(binPath, os.O_CREATE|os.O_WRONLY, defaultFilePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = fmt.Errorf("upload directory does not exist: %s", store.Path)
|
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
|
return info, err
|
||||||
}
|
}
|
||||||
|
|
||||||
stat, err := os.Stat(store.binPath(id))
|
binPath := store.binPath(id)
|
||||||
|
stat, err := os.Stat(binPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return info, err
|
return info, err
|
||||||
}
|
}
|
||||||
|
|
||||||
info.Offset = stat.Size()
|
info.Offset = stat.Size()
|
||||||
|
info.Storage = map[string]string{
|
||||||
|
"Type": "filestore",
|
||||||
|
"Path": binPath,
|
||||||
|
}
|
||||||
|
|
||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -44,6 +45,9 @@ func TestFilestore(t *testing.T) {
|
||||||
a.EqualValues(42, info.Size)
|
a.EqualValues(42, info.Size)
|
||||||
a.EqualValues(0, info.Offset)
|
a.EqualValues(0, info.Offset)
|
||||||
a.Equal(handler.MetaData{"hello": "world"}, info.MetaData)
|
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
|
// Write data to upload
|
||||||
bytesWritten, err := store.WriteChunk(id, 0, strings.NewReader("hello world"))
|
bytesWritten, err := store.WriteChunk(id, 0, strings.NewReader("hello world"))
|
||||||
|
|
|
@ -26,6 +26,10 @@ type FileInfo struct {
|
||||||
// ordered slice containing the ids of the uploads of which the final upload
|
// ordered slice containing the ids of the uploads of which the final upload
|
||||||
// will consist after concatenation.
|
// will consist after concatenation.
|
||||||
PartialUploads []string
|
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
|
// stopUpload is the cancel function for the upload's context.Context. When
|
||||||
// invoked it will interrupt the writes to DataStore#WriteChunk.
|
// invoked it will interrupt the writes to DataStore#WriteChunk.
|
||||||
|
|
|
@ -303,6 +303,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Should we use GetInfo here?
|
||||||
info.ID = id
|
info.ID = id
|
||||||
|
|
||||||
// Add the Location header directly after creating the new resource to even
|
// Add the Location header directly after creating the new resource to even
|
||||||
|
|
Loading…
Reference in New Issue