2021-07-30 13:30:40 +02:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2023-10-23 21:40:10 +02:00
|
|
|
"codeberg.org/genofire/golang-lib/web/file/fs"
|
|
|
|
"codeberg.org/genofire/golang-lib/web/file/s3"
|
2021-07-30 13:30:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2021-07-30 18:01:45 +02:00
|
|
|
return ErrInvalidFSType
|
2021-07-30 13:30:40 +02:00
|
|
|
}
|
|
|
|
*t = val
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FSInfo is a TOML structure storing access information about a file store.
|
|
|
|
type FSInfo struct {
|
2022-08-14 16:50:52 +02:00
|
|
|
FSType fsType `config:"type" toml:"type"`
|
2021-07-30 13:30:40 +02:00
|
|
|
// file system
|
2022-08-14 16:50:52 +02:00
|
|
|
Root string `config:",omitempty" toml:",omitempty"`
|
2021-07-30 13:30:40 +02:00
|
|
|
// s3
|
2022-08-14 16:50:52 +02:00
|
|
|
Endpoint string `config:",omitempty" toml:",omitempty"`
|
|
|
|
Secure bool `config:",omitempty" toml:",omitempty"`
|
|
|
|
ID string `config:",omitempty" toml:",omitempty"`
|
|
|
|
Secret string `config:",omitempty" toml:",omitempty"`
|
|
|
|
Bucket string `config:",omitempty" toml:",omitempty"`
|
|
|
|
Location string `config:",omitempty" toml:",omitempty"`
|
2021-07-30 13:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a file store from the information provided.
|
|
|
|
func (i *FSInfo) Create() (FS, error) {
|
2021-07-30 18:01:45 +02:00
|
|
|
switch i.FSType {
|
2021-07-30 13:30:40 +02:00
|
|
|
case typeFS:
|
2021-07-30 18:01:45 +02:00
|
|
|
if len(i.Root) == 0 {
|
|
|
|
return nil, ErrNoFSRoot
|
2021-07-30 13:30:40 +02:00
|
|
|
}
|
2021-07-30 18:01:45 +02:00
|
|
|
return &fs.FS{Root: i.Root}, nil
|
2021-07-30 13:30:40 +02:00
|
|
|
case typeS3:
|
2021-07-30 18:01:45 +02:00
|
|
|
return s3.New(i.Endpoint, i.Secure, i.ID, i.Secret, i.Bucket, i.Location)
|
2021-07-30 13:30:40 +02:00
|
|
|
default:
|
2021-07-30 18:01:45 +02:00
|
|
|
return nil, ErrNotImplementedFSType
|
2021-07-30 13:30:40 +02:00
|
|
|
}
|
|
|
|
}
|