web/file2: add createfs.go
This commit is contained in:
parent
967b32fa31
commit
b8acc5f8af
|
@ -0,0 +1,59 @@
|
||||||
|
package file
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"dev.sum7.eu/genofire/golang-lib/web/file2/fs"
|
||||||
|
"dev.sum7.eu/genofire/golang-lib/web/file2/s3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fsType represents a type of file store.
|
||||||
|
type fsType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
typeFS fsType = iota
|
||||||
|
typeS3
|
||||||
|
)
|
||||||
|
|
||||||
|
var stringToType = map[string]fsType{
|
||||||
|
"fs": typeFS,
|
||||||
|
"s3": typeS3,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *fsType) UnmarshalText(input []byte) error {
|
||||||
|
val, ok := stringToType[string(input)]
|
||||||
|
if !ok {
|
||||||
|
return errors.New("invalid file store type")
|
||||||
|
}
|
||||||
|
*t = val
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FSInfo is a TOML structure storing access information about a file store.
|
||||||
|
type FSInfo struct {
|
||||||
|
fstype fsType `toml:"type"`
|
||||||
|
// file system
|
||||||
|
root string `toml:",omitempty"`
|
||||||
|
// s3
|
||||||
|
endpoint string `toml:",omitempty"`
|
||||||
|
secure bool `toml:",omitempty"`
|
||||||
|
id string `toml:",omitempty"`
|
||||||
|
secret string `toml:",omitempty"`
|
||||||
|
bucket string `toml:",omitempty"`
|
||||||
|
location string `toml:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create creates a file store from the information provided.
|
||||||
|
func (i *FSInfo) Create() (FS, error) {
|
||||||
|
switch i.fstype {
|
||||||
|
case typeFS:
|
||||||
|
if len(i.root) == 0 {
|
||||||
|
return nil, errors.New("no file store root")
|
||||||
|
}
|
||||||
|
return &fs.FS{Root: i.root}, nil
|
||||||
|
case typeS3:
|
||||||
|
return s3.New(i.endpoint, i.secure, i.id, i.secret, i.bucket, i.location)
|
||||||
|
default:
|
||||||
|
return nil, errors.New("FSInfo.Create not implemented for provided file store type")
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package fs
|
package fs_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
@ -10,11 +10,12 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"dev.sum7.eu/genofire/golang-lib/web/file2"
|
"dev.sum7.eu/genofire/golang-lib/web/file2"
|
||||||
|
"dev.sum7.eu/genofire/golang-lib/web/file2/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOpenStat(t *testing.T) {
|
func TestOpenStat(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
var fs file.FS = &FS{Root: "./testdata"}
|
var fs file.FS = &fs.FS{Root: "./testdata"}
|
||||||
assert.NoError(fs.Check())
|
assert.NoError(fs.Check())
|
||||||
|
|
||||||
f, err := fs.Open("glenda")
|
f, err := fs.Open("glenda")
|
||||||
|
@ -32,7 +33,7 @@ func TestOpenStat(t *testing.T) {
|
||||||
|
|
||||||
func TestCreateOpenUUIDRead(t *testing.T) {
|
func TestCreateOpenUUIDRead(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
var fs file.FS = &FS{Root: "./testdata"}
|
var fs file.FS = &fs.FS{Root: "./testdata"}
|
||||||
assert.NoError(fs.Check())
|
assert.NoError(fs.Check())
|
||||||
|
|
||||||
err := fs.Store(uuid.MustParse("f9375ccb-ee09-4ecf-917e-b88725efcb68"), "$name", "text/plain", strings.NewReader("hello, world\n"))
|
err := fs.Store(uuid.MustParse("f9375ccb-ee09-4ecf-917e-b88725efcb68"), "$name", "text/plain", strings.NewReader("hello, world\n"))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package s3
|
package s3_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -13,12 +13,12 @@ import (
|
||||||
func TestTypes(t *testing.T) {
|
func TestTypes(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
var fs file.FS
|
var fstore file.FS
|
||||||
fs, err := New("127.0.0.1", false, "", "", "", "")
|
fstore, err := s3.New("127.0.0.1", false, "", "", "", "")
|
||||||
_ = fs
|
_ = fstore
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNew() {
|
func ExampleNew() {
|
||||||
New("play.min.io", true, "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "file-store", "")
|
s3.New("play.min.io", true, "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "file-store", "")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue