Documentation
¶
Overview ¶
Package storage provides file storage abstractions for local and S3 backends.
Index ¶
- Variables
- type Config
- type LocalConfig
- type LocalStorage
- func (s *LocalStorage) Delete(ctx context.Context, path string) error
- func (s *LocalStorage) Enabled() bool
- func (s *LocalStorage) GetStoragePath() string
- func (s *LocalStorage) GetURL(path string) string
- func (s *LocalStorage) ServeFiles(servePath string) http.Handler
- func (s *LocalStorage) Upload(ctx context.Context, category, filename string, content io.Reader, ...) (*UploadResult, error)
- type S3Config
- type S3Storage
- func (s *S3Storage) Delete(ctx context.Context, path string) error
- func (s *S3Storage) Enabled() bool
- func (s *S3Storage) GetPresignedURL(ctx context.Context, path string, expiry time.Duration) (string, error)
- func (s *S3Storage) GetURL(path string) string
- func (s *S3Storage) Upload(ctx context.Context, category, filename string, content io.Reader, ...) (*UploadResult, error)
- type Storage
- type UploadResult
Constants ¶
This section is empty.
Variables ¶
var ( ErrFileTooLarge = errors.New("file exceeds maximum size") ErrInvalidFileType = errors.New("file type not allowed") ErrStorageNotEnabled = errors.New("file storage not enabled") )
Common errors
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Backend string // "local" or "s3"
MaxFileSize int64 // Maximum file size in bytes
AllowedMIME []string // Allowed MIME types
ServeURL string // Base URL for serving files (e.g., https://api.example.com/files)
Local LocalConfig // Local storage config
S3 S3Config // S3 storage config
}
Config holds storage configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns default storage configuration.
type LocalConfig ¶
type LocalConfig struct {
Path string // Base path for file storage on disk
}
LocalConfig holds local storage configuration.
type LocalStorage ¶
type LocalStorage struct {
// contains filtered or unexported fields
}
LocalStorage implements Storage for local filesystem.
func NewLocalStorage ¶
func NewLocalStorage(cfg Config) (*LocalStorage, error)
NewLocalStorage creates a new local storage backend.
func (*LocalStorage) Enabled ¶
func (s *LocalStorage) Enabled() bool
func (*LocalStorage) GetStoragePath ¶
func (s *LocalStorage) GetStoragePath() string
GetStoragePath returns the local filesystem path for serving files.
func (*LocalStorage) GetURL ¶
func (s *LocalStorage) GetURL(path string) string
func (*LocalStorage) ServeFiles ¶
func (s *LocalStorage) ServeFiles(servePath string) http.Handler
ServeFiles returns an HTTP handler for serving uploaded files. The servePath parameter is the URL path prefix (e.g., "/files").
type S3Config ¶
type S3Config struct {
Bucket string
Region string
Endpoint string // Custom endpoint (for S3-compatible services)
AccessKeyID string
SecretAccessKey string
UsePathStyle bool // Use path-style URLs (for S3-compatible services)
CDN string // CDN URL for serving files (optional, uses S3 URL if empty)
ServeViaAPI bool // If true, serve S3 files through the API instead of direct S3 URLs
}
S3Config holds S3 storage configuration.
type S3Storage ¶
type S3Storage struct {
// contains filtered or unexported fields
}
S3Storage implements Storage for AWS S3 and S3-compatible services.
func NewS3Storage ¶
NewS3Storage creates a new S3 storage backend.
type Storage ¶
type Storage interface {
// Upload stores a file and returns the result.
Upload(ctx context.Context, category, filename string, content io.Reader, contentType string, size int64) (*UploadResult, error)
// Delete removes a file by its path.
Delete(ctx context.Context, path string) error
// GetURL returns the public URL for a file path.
GetURL(path string) string
// Enabled returns whether storage is configured and enabled.
Enabled() bool
}
Storage defines the file storage interface.
type UploadResult ¶
type UploadResult struct {
ID string `json:"id"` // Unique file identifier
Path string `json:"path"` // Storage path (category/id.ext)
ContentType string `json:"contentType"` // MIME type
Size int64 `json:"size"` // File size in bytes
}
UploadResult contains information about an uploaded file.