storage

package
v0.0.0-...-5b201f8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package storage provides file storage abstractions for local and S3 backends.

Index

Constants

This section is empty.

Variables

View Source
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) Delete

func (s *LocalStorage) Delete(ctx context.Context, path string) error

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").

func (*LocalStorage) Upload

func (s *LocalStorage) Upload(ctx context.Context, category, filename string, content io.Reader, contentType string, size int64) (*UploadResult, error)

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

func NewS3Storage(cfg Config) (*S3Storage, error)

NewS3Storage creates a new S3 storage backend.

func (*S3Storage) Delete

func (s *S3Storage) Delete(ctx context.Context, path string) error

func (*S3Storage) Enabled

func (s *S3Storage) Enabled() bool

func (*S3Storage) GetPresignedURL

func (s *S3Storage) GetPresignedURL(ctx context.Context, path string, expiry time.Duration) (string, error)

GetPresignedURL returns a presigned URL for temporary access.

func (*S3Storage) GetURL

func (s *S3Storage) GetURL(path string) string

func (*S3Storage) Upload

func (s *S3Storage) Upload(ctx context.Context, category, filename string, content io.Reader, contentType string, size int64) (*UploadResult, error)

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.

func New

func New(cfg Config) (Storage, error)

New creates a new storage backend based on configuration.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址