Documentation
¶
Index ¶
- Constants
- Variables
- func GenerateDownloadToken(key, secret string, expiresAt int64) string
- func GenerateToken(key, secret string, expiresAt int64, contentType string, maxSizeBytes int64) string
- func VerifyDownloadToken(key, token, secret string, expiresAt int64) bool
- func VerifyToken(key, token, secret string, expiresAt int64, contentType string, ...) bool
- type LocalFSDriver
- func (d *LocalFSDriver) Delete(ctx context.Context, key string) error
- func (d *LocalFSDriver) Get(ctx context.Context, key string) (io.ReadCloser, string, error)
- func (d *LocalFSDriver) GetDownloadURL(_ context.Context, key string) (string, error)
- func (d *LocalFSDriver) GetUploadURL(_ context.Context, key string, contentType string, maxSizeBytes int64) (string, error)
- func (d *LocalFSDriver) Save(ctx context.Context, key string, body io.Reader, contentType string) error
- func (d *LocalFSDriver) VerifyDownloadToken(key, token string, expiresAt int64) bool
- func (d *LocalFSDriver) VerifyToken(key, token string, expiresAt int64, contentType string, maxSizeBytes int64) bool
- type S3Driver
- func (d *S3Driver) Delete(ctx context.Context, key string) error
- func (d *S3Driver) Get(ctx context.Context, key string) (io.ReadCloser, string, error)
- func (d *S3Driver) GetDownloadURL(ctx context.Context, key string) (string, error)
- func (d *S3Driver) GetUploadURL(ctx context.Context, key string, contentType string, maxSizeBytes int64) (string, error)
- func (d *S3Driver) Save(ctx context.Context, key string, content io.Reader, contentType string) error
Constants ¶
const DefaultMime = "application/octet-stream"
DefaultMime is the fallback MIME type when none is provided.
const DefaultPresignTTL = 15 * time.Minute
DefaultPresignTTL is the default time-to-live for presigned upload and download URLs.
Variables ¶
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 ¶
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 ¶
VerifyDownloadToken checks if a provided download token matches the expected signature.
Types ¶
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) (*LocalFSDriver, error)
NewLocalFSDriver creates a new LocalFSDriver. baseDir is where files will be stored. publicURL is the base URL used to generate public links (e.g., /api/storage). 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) Delete ¶
func (d *LocalFSDriver) Delete(ctx context.Context, key string) error
func (*LocalFSDriver) Get ¶
func (d *LocalFSDriver) Get(ctx context.Context, key string) (io.ReadCloser, string, error)
func (*LocalFSDriver) GetDownloadURL ¶
func (*LocalFSDriver) GetUploadURL ¶
func (d *LocalFSDriver) GetUploadURL(_ context.Context, key string, contentType string, maxSizeBytes int64) (string, error)
GetUploadURL returns a presigned URL pointing to a local PUT handler. 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) 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 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 (request body is capped at 32MB by the HTTP handler).