mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08: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:
		@@ -5,6 +5,7 @@
 | 
			
		||||
package storage
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"os"
 | 
			
		||||
@@ -17,19 +18,35 @@ var (
 | 
			
		||||
	_ ObjectStorage = &LocalStorage{}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// LocalStorageType is the type descriptor for local storage
 | 
			
		||||
const LocalStorageType Type = "local"
 | 
			
		||||
 | 
			
		||||
// LocalStorageConfig represents the configuration for a local storage
 | 
			
		||||
type LocalStorageConfig struct {
 | 
			
		||||
	Path string `ini:"PATH"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LocalStorage represents a local files storage
 | 
			
		||||
type LocalStorage struct {
 | 
			
		||||
	ctx context.Context
 | 
			
		||||
	dir string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewLocalStorage returns a local files
 | 
			
		||||
func NewLocalStorage(bucket string) (*LocalStorage, error) {
 | 
			
		||||
	if err := os.MkdirAll(bucket, os.ModePerm); err != nil {
 | 
			
		||||
func NewLocalStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error) {
 | 
			
		||||
	configInterface, err := toConfig(LocalStorageConfig{}, cfg)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	config := configInterface.(LocalStorageConfig)
 | 
			
		||||
 | 
			
		||||
	if err := os.MkdirAll(config.Path, os.ModePerm); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &LocalStorage{
 | 
			
		||||
		dir: bucket,
 | 
			
		||||
		ctx: ctx,
 | 
			
		||||
		dir: config.Path,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -80,6 +97,11 @@ func (l *LocalStorage) IterateObjects(fn func(path string, obj Object) error) er
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		select {
 | 
			
		||||
		case <-l.ctx.Done():
 | 
			
		||||
			return l.ctx.Err()
 | 
			
		||||
		default:
 | 
			
		||||
		}
 | 
			
		||||
		if path == l.dir {
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
@@ -98,3 +120,7 @@ func (l *LocalStorage) IterateObjects(fn func(path string, obj Object) error) er
 | 
			
		||||
		return fn(relPath, obj)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	RegisterStorageType(LocalStorageType, NewLocalStorage)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user