Documentation
¶
Index ¶
- Constants
- Variables
- func GetFileExtensionForMIME(contentType string) string
- func GuessContentType(filePath string, data []byte) string
- func Init(configs ...Config) error
- func InitFromEnv() error
- func IsAudioFile(contentType string) bool
- func IsCompressedFile(contentType string) bool
- func IsExist(err error) bool
- func IsImageFile(contentType string) bool
- func IsNotExist(err error) bool
- func IsPDFFile(contentType string) bool
- func IsPermission(err error) bool
- func IsTextFile(contentType string) bool
- func IsVideoFile(contentType string) bool
- func RegisterDriver(name string, factory DriverFactory)
- func Reset()
- func Upload(ctx context.Context, fs FileSystem, path string, r io.Reader, size int64, ...) error
- type Builder
- type ChunkedUploader
- type Config
- type DriverFactory
- type EncryptedFS
- func (e *EncryptedFS) CreateDir(ctx context.Context, path string) error
- func (e *EncryptedFS) Delete(ctx context.Context, path string) error
- func (e *EncryptedFS) DeleteDir(ctx context.Context, path string) error
- func (e *EncryptedFS) Download(ctx context.Context, path string) (io.ReadCloser, error)
- func (e *EncryptedFS) Exists(ctx context.Context, path string) (bool, error)
- func (e *EncryptedFS) FileInfo(ctx context.Context, path string) (*File, error)
- func (e *EncryptedFS) List(ctx context.Context, prefix string) ([]File, error)
- func (e *EncryptedFS) Upload(ctx context.Context, path string, content io.Reader, options ...Option) error
- func (e *EncryptedFS) UploadFile(ctx context.Context, path, localPath string, options ...Option) error
- type EncryptionOptions
- type File
- type FileSystem
- type Option
- func WithACL(acl string) Option
- func WithCacheControl(cacheControl string) Option
- func WithContentDisposition(disposition string) Option
- func WithContentType(contentType string) Option
- func WithEncryption(algorithm string, key []byte) Option
- func WithEncryptionKeyID(algorithm string, key []byte, keyID string) Option
- func WithExpires(expires time.Time) Option
- func WithHeaders(headers map[string]string) Option
- func WithMetadata(metadata map[string]string) Option
- func WithOverwrite(overwrite bool) Option
- func WithPreserveFilename(preserve bool) Option
- func WithValidator(validator filevalidator.Validator) Option
- func WithVisibility(visibility Visibility) Option
- type Options
- type PathError
- type ProgressFunc
- type Streamer
- type UploadOptions
- type Uploader
- type ValidatedFileSystem
- func (v *ValidatedFileSystem) CreateDir(ctx context.Context, path string) error
- func (v *ValidatedFileSystem) Delete(ctx context.Context, path string) error
- func (v *ValidatedFileSystem) DeleteDir(ctx context.Context, path string) error
- func (v *ValidatedFileSystem) Download(ctx context.Context, path string) (io.ReadCloser, error)
- func (v *ValidatedFileSystem) Exists(ctx context.Context, path string) (bool, error)
- func (v *ValidatedFileSystem) FileInfo(ctx context.Context, path string) (*File, error)
- func (v *ValidatedFileSystem) List(ctx context.Context, prefix string) ([]File, error)
- func (v *ValidatedFileSystem) Upload(ctx context.Context, path string, content io.Reader, options ...Option) error
- type Visibility
Constants ¶
const ( MIMETypeTextPlain = "text/plain" MIMETypeTextHTML = "text/html" MIMETypeTextCSS = "text/css" MIMETypeTextJavaScript = "text/javascript" MIMETypeApplicationJSON = "application/json" MIMETypeApplicationXML = "application/xml" MIMETypeImageJPEG = "image/jpeg" MIMETypeImagePNG = "image/png" MIMETypeImageGIF = "image/gif" MIMETypeImageSVG = "image/svg+xml" MIMETypeImageWebP = "image/webp" MIMETypeAudioMP3 = "audio/mpeg" MIMETypeAudioOGG = "audio/ogg" MIMETypeVideoMP4 = "video/mp4" MIMETypeVideoWebM = "video/webm" MIMETypeApplicationPDF = "application/pdf" MIMETypeApplicationZip = "application/zip" )
Common MIME types
Variables ¶
var ( ErrNotExist = errors.New("file does not exist") ErrExist = errors.New("file already exists") ErrPermission = errors.New("permission denied") ErrClosed = errors.New("file already closed") ErrNotDir = errors.New("not a directory") ErrIsDir = errors.New("is a directory") ErrNotEmpty = errors.New("directory not empty") ErrInvalidName = errors.New("invalid name") ErrInvalidOffset = errors.New("invalid offset") ErrInvalidWhence = errors.New("invalid whence") ErrNotSupported = errors.New("operation not supported") ErrNotAllowed = errors.New("operation not allowed") )
Common filesystem errors
Functions ¶
func GetFileExtensionForMIME ¶ added in v0.0.4
GetFileExtensionForMIME returns a suitable file extension for a given MIME type
func GuessContentType ¶ added in v0.0.4
GuessContentType tries to determine the content type of a file from its path and data
func InitFromEnv ¶ added in v0.1.0
func InitFromEnv() error
InitFromEnv initializes the global instance from environment variables (convenience method)
func IsAudioFile ¶ added in v0.0.4
IsAudioFile returns true if the file is an audio file based on its MIME type
func IsCompressedFile ¶ added in v0.0.4
IsCompressedFile returns true if the file is a compressed file based on its MIME type
func IsExist ¶ added in v0.0.4
IsExist reports whether an error indicates that a file or directory already exists
func IsImageFile ¶ added in v0.0.4
IsImageFile returns true if the file is an image file based on its MIME type
func IsNotExist ¶ added in v0.0.4
IsNotExist reports whether an error indicates that a file or directory does not exist
func IsPDFFile ¶ added in v0.0.4
IsPDFFile returns true if the file is a PDF file based on its MIME type
func IsPermission ¶ added in v0.0.4
IsPermission reports whether an error indicates that permission is denied
func IsTextFile ¶ added in v0.0.4
IsTextFile returns true if the file is a text file based on its MIME type
func IsVideoFile ¶ added in v0.0.4
IsVideoFile returns true if the file is a video file based on its MIME type
func RegisterDriver ¶ added in v0.1.0
func RegisterDriver(name string, factory DriverFactory)
RegisterDriver registers a driver factory function
Types ¶
type Builder ¶ added in v0.1.0
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a way to create FileSystem instances with custom prefixes
func WithPrefix ¶ added in v0.1.0
WithPrefix creates a new Builder with the specified prefix
func (*Builder) Init ¶ added in v0.1.0
Init initializes the global FileSystem instance using the builder's prefix
func (*Builder) New ¶ added in v0.1.0
func (b *Builder) New() (FileSystem, error)
New creates a new FileSystem instance using the builder's prefix
type ChunkedUploader ¶ added in v0.0.4
type ChunkedUploader interface {
// InitiateUpload starts a chunked upload process and returns an upload ID
InitiateUpload(ctx context.Context, path string) (string, error)
// UploadPart uploads a part of a file in a chunked upload process
UploadPart(ctx context.Context, uploadID string, partNumber int, data []byte) error
// CompleteUpload finalizes a chunked upload
CompleteUpload(ctx context.Context, uploadID string) error
// AbortUpload cancels a chunked upload
AbortUpload(ctx context.Context, uploadID string) error
}
ChunkedUploader is the interface for filesystems that support chunked uploads
type Config ¶ added in v0.1.0
type Config struct {
// Default driver to use (local, s3)
Driver string `env:"FILEKIT_DRIVER,default:local"`
// Local driver configuration
LocalBasePath string `env:"FILEKIT_LOCAL_BASE_PATH,default:./storage"`
// S3 driver configuration
S3Region string `env:"FILEKIT_S3_REGION,default:us-east-1"`
S3Bucket string `env:"FILEKIT_S3_BUCKET"`
S3Prefix string `env:"FILEKIT_S3_PREFIX"`
S3Endpoint string `env:"FILEKIT_S3_ENDPOINT"`
S3AccessKeyID string `env:"FILEKIT_S3_ACCESS_KEY_ID"`
S3SecretAccessKey string `env:"FILEKIT_S3_SECRET_ACCESS_KEY"`
S3ForcePathStyle bool `env:"FILEKIT_S3_FORCE_PATH_STYLE,default:false"`
// Default upload options
DefaultVisibility string `env:"FILEKIT_DEFAULT_VISIBILITY,default:private"`
DefaultCacheControl string `env:"FILEKIT_DEFAULT_CACHE_CONTROL"`
DefaultOverwrite bool `env:"FILEKIT_DEFAULT_OVERWRITE,default:false"`
DefaultPreserveFilename bool `env:"FILEKIT_DEFAULT_PRESERVE_FILENAME,default:false"`
// File validation defaults
MaxFileSize int64 `env:"FILEKIT_MAX_FILE_SIZE,default:10485760"` // 10MB default
AllowedMimeTypes string `env:"FILEKIT_ALLOWED_MIME_TYPES"` // comma-separated
BlockedMimeTypes string `env:"FILEKIT_BLOCKED_MIME_TYPES"` // comma-separated
AllowedExtensions string `env:"FILEKIT_ALLOWED_EXTENSIONS"` // comma-separated
BlockedExtensions string `env:"FILEKIT_BLOCKED_EXTENSIONS"` // comma-separated
// Encryption settings
EncryptionEnabled bool `env:"FILEKIT_ENCRYPTION_ENABLED,default:false"`
EncryptionAlgorithm string `env:"FILEKIT_ENCRYPTION_ALGORITHM,default:AES-256-GCM"`
EncryptionKey string `env:"FILEKIT_ENCRYPTION_KEY"`
}
type DriverFactory ¶ added in v0.1.0
type DriverFactory func(cfg Config) (FileSystem, error)
DriverFactory is a function that creates a FileSystem from a config
type EncryptedFS ¶ added in v0.0.4
type EncryptedFS struct {
// contains filtered or unexported fields
}
EncryptedFS is a wrapper around a FileSystem that encrypts and decrypts data
func NewEncryptedFS ¶ added in v0.0.4
func NewEncryptedFS(fs FileSystem, key []byte) *EncryptedFS
NewEncryptedFS creates a new encrypted filesystem
func (*EncryptedFS) CreateDir ¶ added in v0.0.4
func (e *EncryptedFS) CreateDir(ctx context.Context, path string) error
CreateDir delegates to the underlying filesystem
func (*EncryptedFS) Delete ¶ added in v0.0.4
func (e *EncryptedFS) Delete(ctx context.Context, path string) error
Delete delegates to the underlying filesystem
func (*EncryptedFS) DeleteDir ¶ added in v0.0.4
func (e *EncryptedFS) DeleteDir(ctx context.Context, path string) error
DeleteDir delegates to the underlying filesystem
func (*EncryptedFS) Download ¶ added in v0.0.4
func (e *EncryptedFS) Download(ctx context.Context, path string) (io.ReadCloser, error)
Download decrypts the content after downloading
func (*EncryptedFS) Upload ¶ added in v0.0.4
func (e *EncryptedFS) Upload(ctx context.Context, path string, content io.Reader, options ...Option) error
Upload encrypts the content before uploading
func (*EncryptedFS) UploadFile ¶ added in v0.0.4
func (e *EncryptedFS) UploadFile(ctx context.Context, path, localPath string, options ...Option) error
UploadFile encrypts and uploads a local file
type EncryptionOptions ¶ added in v0.0.4
type EncryptionOptions struct {
// Algorithm specifies the encryption algorithm to use
Algorithm string
// Key is the encryption key
Key []byte
// KeyID is an identifier for the encryption key (for key rotation)
KeyID string
}
EncryptionOptions contains options for file encryption
type File ¶
type File struct {
Name string
Path string
Size int64
ModTime time.Time
IsDir bool
ContentType string
Metadata map[string]string
}
File represents a file in the filesystem
type FileSystem ¶
type FileSystem interface {
// Core operations
Upload(ctx context.Context, path string, content io.Reader, options ...Option) error
Download(ctx context.Context, path string) (io.ReadCloser, error)
Delete(ctx context.Context, path string) error
// File operations
Exists(ctx context.Context, path string) (bool, error)
FileInfo(ctx context.Context, path string) (*File, error)
// Directory operations
List(ctx context.Context, prefix string) ([]File, error)
CreateDir(ctx context.Context, path string) error
DeleteDir(ctx context.Context, path string) error
}
FileSystem defines the main interface for file operations
func CreateDriver ¶ added in v0.1.0
func CreateDriver(cfg Config) (FileSystem, error)
CreateDriver creates a driver instance from config
func Default ¶ added in v0.1.0
func Default() (FileSystem, error)
Default returns the global instance, initializing if needed with error handling
func New ¶ added in v0.1.0
func New(cfg Config) (FileSystem, error)
New creates a new file system instance with given config
func NewFromEnv ¶ added in v0.1.0
func NewFromEnv() (FileSystem, error)
NewFromEnv creates instance from environment variables (convenience constructor)
type Option ¶
type Option func(*Options)
Option represents a configuration option
func WithCacheControl ¶ added in v0.0.4
WithCacheControl sets the Cache-Control header
func WithContentDisposition ¶ added in v0.0.4
WithContentDisposition sets the Content-Disposition header
func WithContentType ¶
WithContentType sets the content type of the file
func WithEncryption ¶ added in v0.0.4
WithEncryption enables encryption for the file
func WithEncryptionKeyID ¶ added in v0.0.4
WithEncryptionKeyID enables encryption with a specific key ID
func WithExpires ¶ added in v0.0.4
WithExpires sets when the file should expire
func WithHeaders ¶ added in v0.0.4
WithHeaders sets additional HTTP headers
func WithMetadata ¶
WithMetadata sets additional metadata for the file
func WithOverwrite ¶ added in v0.0.4
WithOverwrite enables or disables overwriting existing files
func WithPreserveFilename ¶ added in v0.0.4
WithPreserveFilename enables or disables preserving the original filename
func WithValidator ¶ added in v0.0.4
func WithValidator(validator filevalidator.Validator) Option
WithValidator sets a file validator to use before upload
func WithVisibility ¶
func WithVisibility(visibility Visibility) Option
WithVisibility sets the file visibility
type Options ¶
type Options struct {
// ContentType specifies the MIME type of the file
ContentType string
// Metadata contains additional metadata for the file
Metadata map[string]string
// Visibility defines the file visibility (public or private)
Visibility Visibility
// CacheControl sets the Cache-Control header for the file
CacheControl string
// Overwrite determines whether to overwrite existing files
Overwrite bool
// Encryption specifies encryption settings for the file
Encryption *EncryptionOptions
// Expires sets when the file should expire
Expires *time.Time
// PreserveFilename determines whether to keep the original filename
PreserveFilename bool
// Headers contains additional HTTP headers to set
Headers map[string]string
// SkipExistingCheck skips checking if a file already exists before upload
SkipExistingCheck bool
// ContentDisposition sets the Content-Disposition header
ContentDisposition string
// ACL sets specific access control list settings
ACL string
// Validator is an optional file validator to use before upload
Validator filevalidator.Validator
}
Options contains all possible options for file operations
type PathError ¶ added in v0.0.4
PathError records an error and the operation and file path that caused it
type ProgressFunc ¶ added in v0.0.4
ProgressFunc is a callback function for upload progress
type Streamer ¶
type Streamer interface {
Stream(ctx context.Context, path string) (io.ReadCloser, error)
StreamWrite(ctx context.Context, path string, reader io.Reader) error
}
Streamer interface for streaming operations
func NewStreamer ¶
func NewStreamer(fs FileSystem) Streamer
type UploadOptions ¶ added in v0.0.4
type UploadOptions struct {
// ContentType specifies the MIME type of the file
ContentType string
// ChunkSize defines the size of chunks for chunked upload
// If zero, a default chunk size will be used
ChunkSize int64
// Progress is a callback function for upload progress
Progress ProgressFunc
// Metadata contains additional metadata for the file
Metadata map[string]string
// Visibility defines the file visibility (public or private)
Visibility Visibility
}
UploadOptions contains options for uploading files
type Uploader ¶
type Uploader interface {
Upload(ctx context.Context, path string, content io.Reader, options ...Option) error
UploadFile(ctx context.Context, path string, localPath string, options ...Option) error
}
Uploader interface specifically for upload operations
type ValidatedFileSystem ¶ added in v0.0.4
type ValidatedFileSystem struct {
// contains filtered or unexported fields
}
ValidatedFileSystem wraps a FileSystem with validation support
func NewValidatedFileSystem ¶ added in v0.0.4
func NewValidatedFileSystem(fs FileSystem, validator filevalidator.Validator) *ValidatedFileSystem
NewValidatedFileSystem creates a new FileSystem with validation
func (*ValidatedFileSystem) CreateDir ¶ added in v0.0.4
func (v *ValidatedFileSystem) CreateDir(ctx context.Context, path string) error
CreateDir implements FileSystem
func (*ValidatedFileSystem) Delete ¶ added in v0.0.4
func (v *ValidatedFileSystem) Delete(ctx context.Context, path string) error
Delete implements FileSystem
func (*ValidatedFileSystem) DeleteDir ¶ added in v0.0.4
func (v *ValidatedFileSystem) DeleteDir(ctx context.Context, path string) error
DeleteDir implements FileSystem
func (*ValidatedFileSystem) Download ¶ added in v0.0.4
func (v *ValidatedFileSystem) Download(ctx context.Context, path string) (io.ReadCloser, error)
Download implements FileSystem
type Visibility ¶
type Visibility string
Visibility represents file visibility
const ( // Private means the file is only accessible by authenticated users Private Visibility = "private" // Public means the file is publicly accessible Public Visibility = "public" // Protected means the file is accessible with specific permissions Protected Visibility = "protected" )