mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 10:18:38 +00:00
Provide self-registering storage system (#12978)
* Provide self-registering storage system Signed-off-by: Andrew Thornton <art27@cantab.net> * More simplification Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove old strings from setting Signed-off-by: Andrew Thornton <art27@cantab.net> * oops attachments not attachment Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
@@ -22,6 +22,38 @@ var (
|
||||
ErrIterateObjectsNotSupported = errors.New("iterateObjects method not supported")
|
||||
)
|
||||
|
||||
// ErrInvalidConfiguration is called when there is invalid configuration for a storage
|
||||
type ErrInvalidConfiguration struct {
|
||||
cfg interface{}
|
||||
err error
|
||||
}
|
||||
|
||||
func (err ErrInvalidConfiguration) Error() string {
|
||||
if err.err != nil {
|
||||
return fmt.Sprintf("Invalid Configuration Argument: %v: Error: %v", err.cfg, err.err)
|
||||
}
|
||||
return fmt.Sprintf("Invalid Configuration Argument: %v", err.cfg)
|
||||
}
|
||||
|
||||
// IsErrInvalidConfiguration checks if an error is an ErrInvalidConfiguration
|
||||
func IsErrInvalidConfiguration(err error) bool {
|
||||
_, ok := err.(ErrInvalidConfiguration)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Type is a type of Storage
|
||||
type Type string
|
||||
|
||||
// NewStorageFunc is a function that creates a storage
|
||||
type NewStorageFunc func(ctx context.Context, cfg interface{}) (ObjectStorage, error)
|
||||
|
||||
var storageMap = map[Type]NewStorageFunc{}
|
||||
|
||||
// RegisterStorageType registers a provided storage type with a function to create it
|
||||
func RegisterStorageType(typ Type, fn func(ctx context.Context, cfg interface{}) (ObjectStorage, error)) {
|
||||
storageMap[typ] = fn
|
||||
}
|
||||
|
||||
// Object represents the object on the storage
|
||||
type Object interface {
|
||||
io.ReadCloser
|
||||
@@ -67,41 +99,25 @@ func Init() error {
|
||||
return initLFS()
|
||||
}
|
||||
|
||||
func initStorage(storageCfg setting.Storage) (ObjectStorage, error) {
|
||||
var err error
|
||||
var s ObjectStorage
|
||||
switch storageCfg.Type {
|
||||
case setting.LocalStorageType:
|
||||
s, err = NewLocalStorage(storageCfg.Path)
|
||||
case setting.MinioStorageType:
|
||||
minio := storageCfg.Minio
|
||||
s, err = NewMinioStorage(
|
||||
context.Background(),
|
||||
minio.Endpoint,
|
||||
minio.AccessKeyID,
|
||||
minio.SecretAccessKey,
|
||||
minio.Bucket,
|
||||
minio.Location,
|
||||
minio.BasePath,
|
||||
minio.UseSSL,
|
||||
)
|
||||
default:
|
||||
return nil, fmt.Errorf("Unsupported attachment store type: %s", storageCfg.Type)
|
||||
// NewStorage takes a storage type and some config and returns an ObjectStorage or an error
|
||||
func NewStorage(typStr string, cfg interface{}) (ObjectStorage, error) {
|
||||
if len(typStr) == 0 {
|
||||
typStr = string(LocalStorageType)
|
||||
}
|
||||
fn, ok := storageMap[Type(typStr)]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Unsupported storage type: %s", typStr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s, nil
|
||||
return fn(context.Background(), cfg)
|
||||
}
|
||||
|
||||
func initAttachments() (err error) {
|
||||
Attachments, err = initStorage(setting.Attachment.Storage)
|
||||
Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage)
|
||||
return
|
||||
}
|
||||
|
||||
func initLFS() (err error) {
|
||||
LFS, err = initStorage(setting.LFS.Storage)
|
||||
LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user