Documentation
¶
Overview ¶
Package storage is part of the GoFastr framework. See https://github.com/DonaldMurillo/gofastr for documentation.
Index ¶
- func Register(t StorageType, factory BackendFactory)
- type BackendFactory
- type Battery
- type DefaultKeyValidator
- type FileMeta
- type KeyValidator
- type LocalOption
- type LocalStorage
- func (ls *LocalStorage) Delete(ctx context.Context, key string) error
- func (ls *LocalStorage) Exists(ctx context.Context, key string) (bool, error)
- func (ls *LocalStorage) Get(ctx context.Context, key string) (io.ReadCloser, error)
- func (ls *LocalStorage) Save(ctx context.Context, key string, r io.Reader) error
- type MemoryStorage
- func (ms *MemoryStorage) Delete(_ context.Context, key string) error
- func (ms *MemoryStorage) Exists(_ context.Context, key string) (bool, error)
- func (ms *MemoryStorage) Get(_ context.Context, key string) (io.ReadCloser, error)
- func (ms *MemoryStorage) Save(_ context.Context, key string, r io.Reader) error
- type Presigner
- type ReadCloser
- type S3Client
- type S3Option
- type S3Storage
- func (s *S3Storage) Delete(ctx context.Context, key string) error
- func (s *S3Storage) Exists(ctx context.Context, key string) (bool, error)
- func (s *S3Storage) Get(ctx context.Context, key string) (io.ReadCloser, error)
- func (s *S3Storage) PresignedGetURL(ctx context.Context, key string, expires time.Duration) (*url.URL, error)
- func (s *S3Storage) PresignedPutURL(ctx context.Context, key string, expires time.Duration) (*url.URL, error)
- func (s *S3Storage) Save(ctx context.Context, key string, r io.Reader) error
- type Storage
- type StorageType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
func Register(t StorageType, factory BackendFactory)
Register adds a backend factory for the given storage type. Panics on duplicate registration (a programming error caught at boot). A nil factory is rejected so a later New call doesn't nil-pointer.
Types ¶
type BackendFactory ¶
BackendFactory creates a Storage instance from a type and generic config.
type Battery ¶ added in v0.3.2
type Battery struct {
// contains filtered or unexported fields
}
Battery is the framework.Battery adapter for the storage battery. It participates in the App's dependency-resolved lifecycle so host apps can declare that other batteries depend on the file-storage backend being available.
Construct via NewBattery and register:
st := storage.NewLocalStorage("/var/uploads")
app.Batteries.Register(storage.NewBattery(st))
The battery's Init is a no-op — the backend is fully constructed before registration. None of the built-in backends (LocalStorage, MemoryStorage, S3Storage) run background goroutines, so no OnStop hook is needed. If a future backend does, implement io.Closer on that type.
func NewBattery ¶ added in v0.3.2
NewBattery wraps s in a framework lifecycle adapter.
func (*Battery) Init ¶ added in v0.3.2
Init implements framework.Battery. The backend is fully constructed by the caller before registration so Init is a no-op.
type DefaultKeyValidator ¶
type DefaultKeyValidator struct{}
DefaultKeyValidator implements basic key validation.
func (DefaultKeyValidator) ValidateKey ¶
func (DefaultKeyValidator) ValidateKey(key string) error
ValidateKey checks that a key does not contain path traversal sequences.
type KeyValidator ¶
KeyValidator validates storage keys to prevent path traversal and other attacks.
type LocalOption ¶
type LocalOption func(*LocalStorage)
LocalOption configures a LocalStorage instance.
func WithPermissions ¶
func WithPermissions(mode os.FileMode) LocalOption
WithPermissions sets the file permission mode for saved files.
func WithTempDir ¶
func WithTempDir(dir string) LocalOption
WithTempDir sets a custom temporary directory for atomic writes.
type LocalStorage ¶
type LocalStorage struct {
BaseDir string
// contains filtered or unexported fields
}
LocalStorage implements Storage backed by the local filesystem. Writes are atomic: data is first written to a temporary file, then renamed to the final path.
func NewLocalStorage ¶
func NewLocalStorage(baseDir string, opts ...LocalOption) *LocalStorage
NewLocalStorage creates a LocalStorage rooted at baseDir. The directory is created if it does not exist.
func (*LocalStorage) Delete ¶
func (ls *LocalStorage) Delete(ctx context.Context, key string) error
Delete removes the file identified by key from the filesystem. It is not an error if the file does not exist.
func (*LocalStorage) Get ¶
func (ls *LocalStorage) Get(ctx context.Context, key string) (io.ReadCloser, error)
Get opens the file identified by key and returns a ReadCloser for its contents.
type MemoryStorage ¶
type MemoryStorage struct {
// contains filtered or unexported fields
}
MemoryStorage implements Storage backed by an in-memory map. It is safe for concurrent use via sync.RWMutex.
func NewMemoryStorage ¶
func NewMemoryStorage() *MemoryStorage
NewMemoryStorage creates a new empty MemoryStorage.
func (*MemoryStorage) Delete ¶
func (ms *MemoryStorage) Delete(_ context.Context, key string) error
Delete removes the file identified by key. It is not an error if the key does not exist.
func (*MemoryStorage) Get ¶
func (ms *MemoryStorage) Get(_ context.Context, key string) (io.ReadCloser, error)
Get returns a ReadCloser for the file identified by key.
type Presigner ¶
type Presigner interface {
PresignGet(ctx context.Context, bucket, key string, expires time.Duration) (*url.URL, error)
PresignPut(ctx context.Context, bucket, key string, expires time.Duration) (*url.URL, error)
}
Presigner generates presigned URLs for direct browser uploads/downloads.
type ReadCloser ¶
ReadCloser wraps an io.Reader to implement io.ReadCloser with a no-op Close.
type S3Client ¶
type S3Client interface {
PutObject(ctx context.Context, bucket, key string, r io.Reader, size int64, contentType string) error
GetObject(ctx context.Context, bucket, key string) (io.ReadCloser, error)
DeleteObject(ctx context.Context, bucket, key string) error
HeadObject(ctx context.Context, bucket, key string) (bool, error)
}
S3Client is a minimal interface for S3-compatible operations. This avoids importing the AWS SDK directly; callers provide their own implementation or use the PresignedURL field for direct browser uploads.
type S3Option ¶
type S3Option func(*S3Storage)
S3Option configures an S3Storage instance.
func WithPresigner ¶
WithPresigner sets the URL presigner for generating presigned URLs.
func WithS3Client ¶
WithS3Client sets the S3 client implementation.
func WithS3Endpoint ¶
WithS3Endpoint sets a custom S3-compatible endpoint.
type S3Storage ¶
type S3Storage struct {
Bucket string
Region string
Endpoint string
Client S3Client
// contains filtered or unexported fields
}
S3Storage implements Storage backed by an S3-compatible object store. It uses a minimal S3Client interface so no AWS SDK is imported directly.
func NewS3Storage ¶
NewS3Storage creates a new S3Storage for the given bucket and region. Use WithS3Client to inject an actual client before calling Save/Get/etc.
func (*S3Storage) PresignedGetURL ¶
func (s *S3Storage) PresignedGetURL(ctx context.Context, key string, expires time.Duration) (*url.URL, error)
PresignedGetURL returns a presigned URL for downloading the object.
type StorageType ¶
type StorageType int
StorageType enumerates available storage backend types.
const ( Local StorageType = iota S3 Memory )
func (StorageType) String ¶
func (t StorageType) String() string
String returns a human-readable name for the StorageType.