drivers

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultLocalRoutePrefix = "/api/v1/storage"

DefaultLocalRoutePrefix is the path the local driver's content routes sit under unless WithRoutePrefix sets another.

View Source
const DefaultMime = "application/octet-stream"

DefaultMime is the fallback MIME type when none is provided.

View Source
const DefaultPresignTTL = 15 * time.Minute

DefaultPresignTTL is the default time-to-live for presigned upload and download URLs.

Variables

View Source
var ErrInvalidPath = errors.New("invalid path: traversal or invalid key not allowed")

ErrInvalidPath is returned when a key or resolved path is invalid (e.g. path traversal). Callers can use errors.Is(err, drivers.ErrInvalidPath) to detect validation failures.

Functions

func GenerateDownloadToken

func GenerateDownloadToken(key, secret string, expiresAt int64) string

GenerateDownloadToken creates an HMAC-SHA256 token specifically for download links (only signs key and expiration).

func GenerateToken

func GenerateToken(key, secret string, expiresAt int64, contentType string, maxSizeBytes int64) string

GenerateToken creates an HMAC-SHA256 token signing multiple constraints.

func VerifyDownloadToken

func VerifyDownloadToken(key, token, secret string, expiresAt int64) bool

VerifyDownloadToken checks if a provided download token matches the expected signature.

func VerifyToken

func VerifyToken(key, token, secret string, expiresAt int64, contentType string, maxSizeBytes int64) bool

VerifyToken checks if a provided token matches the expected signature for given constraints.

Types

type LocalConfig added in v0.2.0

type LocalConfig struct {
	// BaseDir is the directory files are stored under (created if absent).
	BaseDir string `yaml:"baseDir"`
	// PublicURL is the origin of the server that serves the driver's
	// content routes (e.g. http://localhost:8080). See
	// storage.LocalContentHandler.
	PublicURL string `yaml:"publicURL"`
	// RoutePrefix is the path the content routes sit under on that server,
	// e.g. "/files" for routes at /files/{key}/content. Empty means
	// DefaultLocalRoutePrefix ("/api/v1/storage"). Otherwise it follows the
	// rules of WithRoutePrefix.
	RoutePrefix string `yaml:"routePrefix"`
	// PutSecret signs presigned upload URLs for the local driver.
	PutSecret string `yaml:"putSecret"`
}

LocalConfig holds configuration for the local filesystem storage backend.

LocalConfig carries yaml struct tags, so it can be embedded in a larger application config struct and populated generically (e.g. via yaml.Unmarshal).

func (LocalConfig) Validate added in v0.2.0

func (c LocalConfig) Validate() error

Validate ensures the local storage configuration is usable.

type LocalFSDriver

type LocalFSDriver struct {
	BaseDir   string
	PublicURL string
	// contains filtered or unexported fields
}

LocalFSDriver implements StorageDriver for local disk with directory hashing

func NewLocalFSDriver

func NewLocalFSDriver(baseDir, publicURL, secretKey string, presignTTL time.Duration, opts ...LocalOption) (*LocalFSDriver, error)

NewLocalFSDriver creates a new LocalFSDriver. baseDir is where files will be stored. publicURL is the origin of the server that serves ContentPattern (e.g. http://localhost:8080), using storage.LocalContentHandler. secretKey is the secret used for HMAC signing of local-put upload URLs. presignTTL is the default time-to-live for presigned URLs.

func (*LocalFSDriver) ContentPattern added in v0.3.0

func (d *LocalFSDriver) ContentPattern() string

ContentPattern is the ServeMux path pattern of the driver's content routes, the route prefix followed by "/{key}/content". Upload and download URLs point at it on PublicURL, and storage.LocalContentHandler serves it.

func (*LocalFSDriver) Delete

func (d *LocalFSDriver) Delete(ctx context.Context, key string) error

func (*LocalFSDriver) Get

func (*LocalFSDriver) GetDownloadURL

func (d *LocalFSDriver) GetDownloadURL(_ context.Context, key string) (string, error)

func (*LocalFSDriver) GetUploadURL

func (d *LocalFSDriver) GetUploadURL(_ context.Context, key string, contentType string, maxSizeBytes int64) (string, error)

GetUploadURL returns a presigned URL pointing to the PUT route on ContentPattern. Note: This method does NOT create the file on disk. It only signs the security constraints (key, expiration, size limit). The actual resource allocation (file creation) happens in Save() when the PUT request is eventually processed, matching S3's deferred behavior.

func (*LocalFSDriver) PresignTTL added in v0.3.0

func (d *LocalFSDriver) PresignTTL() time.Duration

PresignTTL reports how long the driver's presigned URLs stay valid.

func (*LocalFSDriver) Save

func (d *LocalFSDriver) Save(ctx context.Context, key string, body io.Reader, contentType string) error

func (*LocalFSDriver) VerifyDownloadToken

func (d *LocalFSDriver) VerifyDownloadToken(key, token string, expiresAt int64) bool

VerifyDownloadToken checks if a provided download token is valid and not expired.

func (*LocalFSDriver) VerifyToken

func (d *LocalFSDriver) VerifyToken(key, token string, expiresAt int64, contentType string, maxSizeBytes int64) bool

VerifyToken checks if a token is valid for a given key and constraints using the driver's secret.

type LocalOption added in v0.3.0

type LocalOption func(*LocalFSDriver)

LocalOption configures a LocalFSDriver.

func WithRoutePrefix added in v0.3.0

func WithRoutePrefix(prefix string) LocalOption

WithRoutePrefix sets the path the driver's content routes sit under, e.g. "/files" for routes at /files/{key}/content. It must start with "/", must not end with one, and must not contain {, }, ?, #, whitespace, or empty, "." or ".." segments; NewLocalFSDriver returns an error naming the rule a bad prefix breaks. Without it the prefix is DefaultLocalRoutePrefix.

type S3Config added in v0.2.0

type S3Config struct {
	// Endpoint is an optional custom endpoint URL for S3-compatible stores
	// (e.g. MinIO or LocalStack). Empty targets AWS S3.
	Endpoint string `yaml:"endpoint"`
	// Bucket is the S3 bucket that files are stored in. Required.
	Bucket string `yaml:"bucket"`
	// Region is the AWS region of the bucket. Required.
	Region string `yaml:"region"`
	// AccessKey and SecretKey are optional static credentials. They must be
	// set together; when both are empty the default AWS credential chain is
	// used.
	AccessKey string `yaml:"accessKey"`
	SecretKey string `yaml:"secretKey"`
	// PublicURL is an optional base URL files are served from, for a
	// CDN-fronted or otherwise rewritten bucket.
	PublicURL string `yaml:"publicURL"`
}

S3Config holds configuration for the S3 storage backend.

S3Config carries yaml struct tags, so it can be embedded in a larger application config struct and populated generically (e.g. via yaml.Unmarshal).

func (S3Config) Validate added in v0.2.0

func (c S3Config) Validate() error

Validate ensures the S3 storage configuration is usable.

type S3Driver

type S3Driver struct {
	Client        *s3.Client
	PresignClient *s3.PresignClient
	Bucket        string
	PublicURL     string // Optional: Base URL if files are public
	// contains filtered or unexported fields
}

S3Driver implements StorageDriver for S3-compatible storage. Save streams via PutObject with no size limit of its own; presigned uploads are limited by the ContentLength signed into the URL.

func NewS3Driver

func NewS3Driver(client *s3.Client, bucket string, publicURL string, presignTTL time.Duration) *S3Driver

func (*S3Driver) Delete

func (d *S3Driver) Delete(ctx context.Context, key string) error

func (*S3Driver) Get

func (d *S3Driver) Get(ctx context.Context, key string) (io.ReadCloser, string, error)

func (*S3Driver) GetDownloadURL

func (d *S3Driver) GetDownloadURL(ctx context.Context, key string) (string, error)

func (*S3Driver) GetUploadURL

func (d *S3Driver) GetUploadURL(ctx context.Context, key string, contentType string, maxSizeBytes int64) (string, error)

func (*S3Driver) PresignTTL added in v0.3.0

func (d *S3Driver) PresignTTL() time.Duration

PresignTTL reports how long the driver's presigned URLs stay valid.

func (*S3Driver) Save

func (d *S3Driver) Save(ctx context.Context, key string, content io.Reader, contentType string) error

Jump to

Keyboard shortcuts

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