storage

package
v1.1.13 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileNotFound      = os.ErrNotExist
	ErrPermissionDenied  = os.ErrPermission
	ErrInvalidPath       = os.ErrInvalid
	ErrDirectoryNotEmpty = os.ErrExist
	ErrFileAlreadyExists = os.ErrExist
)

常见错误定义

View Source
var (
	ErrUnsupportedFileFormat = errors.New("processor: 不支持的文件格式")
	ErrProcessingFailed      = errors.New("processor: 处理文件失败")
	ErrInvalidDimensions     = errors.New("processor: 无效的图片尺寸")
	ErrInvalidOperation      = errors.New("processor: 无效的操作")
)

文件处理相关错误

Functions

func CalculateChecksum

func CalculateChecksum(data []byte, algorithm string) (string, error)

CalculateChecksum 计算数据的校验和

func CalculateChecksumFromReader

func CalculateChecksumFromReader(reader io.Reader, algorithm string) (string, error)

CalculateChecksumFromReader 从数据流计算校验和

func ConvertToCoreFSFileSystem

func ConvertToCoreFSFileSystem(storageFS FileSystem) core.FileSystem

ConvertToCoreFSFileSystem 将storage.FileSystem转换为core.FileSystem

func ConvertToCoreWriteOptions

func ConvertToCoreWriteOptions(options []WriteOption) []core.WriteOption

ConvertToCoreWriteOptions 将storage.WriteOption转换为core.WriteOption

func DetectMimeType

func DetectMimeType(filename string, data []byte) string

DetectMimeType 尝试检测文件的MIME类型

Types

type AfterUploadFunc

type AfterUploadFunc func(ctx context.Context, file *UploadedFile, metadata *Metadata) error

AfterUploadFunc 上传后回调函数类型

type BatchProcessOptions

type BatchProcessOptions struct {
	// 处理类型: resize, watermark, convert
	ProcessType string

	// 处理选项
	Options interface{}

	// 文件匹配模式
	Pattern string

	// 是否递归处理子目录
	Recursive bool

	// 最大并发处理数
	MaxConcurrent int

	// 结果目录
	ResultDirectory string

	// 目标磁盘
	TargetDisk string
}

BatchProcessOptions 批量处理选项

func DefaultBatchProcessOptions

func DefaultBatchProcessOptions() *BatchProcessOptions

DefaultBatchProcessOptions 返回默认的批量处理选项

type BatchProcessResult

type BatchProcessResult struct {
	// 成功处理的文件路径
	SuccessFiles []string

	// 处理失败的文件及错误
	FailedFiles map[string]error

	// 处理的总文件数
	TotalCount int

	// 成功处理的文件数
	SuccessCount int

	// 处理失败的文件数
	FailedCount int
}

BatchProcessResult 批量处理结果

type BeforeUploadFunc

type BeforeUploadFunc func(ctx context.Context, file *UploadedFile) error

BeforeUploadFunc 上传前回调函数类型

type Cache

type Cache interface {
	// Get 从缓存获取值
	Get(ctx context.Context, key string, value interface{}) (bool, error)

	// Set 设置缓存值
	Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

	// Forget 删除缓存项
	Forget(ctx context.Context, key string) error

	// Flush 清空所有缓存
	Flush(ctx context.Context) error

	// FlushByPattern 按模式清空缓存
	FlushByPattern(ctx context.Context, pattern string) error
}

Cache 缓存接口

func NewMemory

func NewMemory() Cache

NewMemory 创建新的内存缓存

type CacheManager

type CacheManager struct {
	// contains filtered or unexported fields
}

CacheManager 文件缓存管理器

func NewCacheManager

func NewCacheManager(manager *Manager, options *CacheOptions) *CacheManager

NewCacheManager 创建文件缓存管理器

func (*CacheManager) Exists

func (cm *CacheManager) Exists(ctx context.Context, disk, path string) (bool, error)

Exists 检查文件是否存在(带缓存)

func (*CacheManager) Flush

func (cm *CacheManager) Flush(ctx context.Context) error

Flush 清空所有缓存

func (*CacheManager) GetContent

func (cm *CacheManager) GetContent(ctx context.Context, disk, path string) ([]byte, error)

GetContent 获取文件内容(带缓存)

func (*CacheManager) GetFile

func (cm *CacheManager) GetFile(ctx context.Context, disk, path string) (File, error)

GetFile 获取文件(带缓存)

func (*CacheManager) GetStream

func (cm *CacheManager) GetStream(ctx context.Context, disk, path string) (io.ReadCloser, error)

GetStream 获取文件流(带缓存)

func (*CacheManager) InvalidateCache

func (cm *CacheManager) InvalidateCache(ctx context.Context, disk, path string) error

InvalidateCache 使缓存失效

func (*CacheManager) InvalidateCacheByPattern

func (cm *CacheManager) InvalidateCacheByPattern(ctx context.Context, disk, pattern string) error

InvalidateCacheByPattern 按模式使缓存失效

func (*CacheManager) IsEnabled

func (cm *CacheManager) IsEnabled() bool

IsEnabled 检查缓存是否启用

func (*CacheManager) SetEnabled

func (cm *CacheManager) SetEnabled(enabled bool)

SetEnabled 设置是否启用缓存

type CacheOptions

type CacheOptions struct {
	// 缓存实例
	Cache Cache

	// 缓存前缀
	Prefix string

	// 默认过期时间
	DefaultTTL time.Duration

	// 缓存目录(用于大文件本地缓存)
	CacheDir string

	// 是否开启缓存
	Enabled bool
}

CacheOptions 缓存选项

type CachedFile

type CachedFile struct {
	// contains filtered or unexported fields
}

CachedFile 表示一个缓存的文件

func (*CachedFile) Extension

func (f *CachedFile) Extension() string

Extension 返回文件的扩展名

func (*CachedFile) IsDirectory

func (f *CachedFile) IsDirectory() bool

IsDirectory 判断是否为目录

func (*CachedFile) LastModified

func (f *CachedFile) LastModified() time.Time

LastModified 返回文件的最后修改时间

func (*CachedFile) Metadata

func (f *CachedFile) Metadata() map[string]interface{}

Metadata 获取文件的元数据

func (*CachedFile) MimeType

func (f *CachedFile) MimeType() string

MimeType 返回文件的MIME类型

func (*CachedFile) Name

func (f *CachedFile) Name() string

Name 返回文件的名称

func (*CachedFile) Path

func (f *CachedFile) Path() string

Path 返回文件的路径

func (*CachedFile) Read

func (f *CachedFile) Read(ctx context.Context) ([]byte, error)

Read 读取文件内容

func (*CachedFile) ReadStream

func (f *CachedFile) ReadStream(ctx context.Context) (io.ReadCloser, error)

ReadStream 获取文件的读取流

func (*CachedFile) Size

func (f *CachedFile) Size() int64

Size 返回文件的大小

func (*CachedFile) TemporaryURL

func (f *CachedFile) TemporaryURL(ctx context.Context, expiration time.Duration) (string, error)

TemporaryURL 获取文件的临时URL

func (*CachedFile) URL

func (f *CachedFile) URL() string

URL 获取文件的URL

func (*CachedFile) Visibility

func (f *CachedFile) Visibility() string

Visibility 返回文件的可见性

type DiskConfig

type DiskConfig struct {
	// 驱动类型 (local, s3, etc.)
	Driver string `mapstructure:"driver"`

	// 驱动配置
	Config map[string]interface{} `mapstructure:"config"`
}

DiskConfig 磁盘配置

type File

type File interface {
	// Path 返回文件的路径
	Path() string

	// Name 返回文件的名称(不包含路径)
	Name() string

	// Extension 返回文件的扩展名
	Extension() string

	// Size 返回文件的大小(字节)
	Size() int64

	// LastModified 返回文件的最后修改时间
	LastModified() time.Time

	// IsDirectory 判断是否为目录
	IsDirectory() bool

	// Read 读取文件内容
	Read(ctx context.Context) ([]byte, error)

	// ReadStream 获取文件的读取流
	ReadStream(ctx context.Context) (io.ReadCloser, error)

	// MimeType 返回文件的MIME类型
	MimeType() string

	// Visibility 返回文件的可见性(public 或 private)
	Visibility() string

	// URL 获取文件的URL(如果适用)
	URL() string

	// TemporaryURL 获取文件的临时URL(带有过期时间)
	TemporaryURL(ctx context.Context, expiration time.Duration) (string, error)

	// Metadata 获取文件的元数据
	Metadata() map[string]interface{}
}

File 表示一个文件对象

func ConvertToStorageFile

func ConvertToStorageFile(coreFile core.File) File

ConvertToStorageFile 将core.File转换为storage.File

func ConvertToStorageFiles

func ConvertToStorageFiles(coreFiles []core.File) []File

ConvertToStorageFiles 将[]core.File转换为[]File

type FileAdapter

type FileAdapter struct {
	CoreFile core.File
}

FileAdapter 适配器,用于将core.File转换为storage.File

func (*FileAdapter) Extension

func (f *FileAdapter) Extension() string

func (*FileAdapter) IsDirectory

func (f *FileAdapter) IsDirectory() bool

func (*FileAdapter) LastModified

func (f *FileAdapter) LastModified() time.Time

func (*FileAdapter) Metadata

func (f *FileAdapter) Metadata() map[string]interface{}

func (*FileAdapter) MimeType

func (f *FileAdapter) MimeType() string

func (*FileAdapter) Name

func (f *FileAdapter) Name() string

func (*FileAdapter) Path

func (f *FileAdapter) Path() string

实现File接口

func (*FileAdapter) Read

func (f *FileAdapter) Read(ctx context.Context) ([]byte, error)

func (*FileAdapter) ReadStream

func (f *FileAdapter) ReadStream(ctx context.Context) (io.ReadCloser, error)

func (*FileAdapter) Size

func (f *FileAdapter) Size() int64

func (*FileAdapter) TemporaryURL

func (f *FileAdapter) TemporaryURL(ctx context.Context, expiration time.Duration) (string, error)

func (*FileAdapter) URL

func (f *FileAdapter) URL() string

func (*FileAdapter) Visibility

func (f *FileAdapter) Visibility() string

type FileConvertOptions

type FileConvertOptions struct {
	// 目标格式
	TargetFormat string

	// 转换参数
	Parameters map[string]interface{}

	// 是否保存原始文件
	KeepOriginal bool

	// 目标路径
	TargetPath string

	// 目标磁盘
	TargetDisk string
}

FileConvertOptions 文件转换选项

func DefaultFileConvertOptions

func DefaultFileConvertOptions() *FileConvertOptions

DefaultFileConvertOptions 返回默认的文件转换选项

type FileInfo

type FileInfo struct {
	// 文件路径
	FilePath string `json:"path"`
	// 文件名
	FileName string `json:"name"`
	// 文件大小(字节)
	FileSize int64 `json:"size"`
	// 是否为目录
	IsDir bool `json:"is_directory"`
	// 最后修改时间
	ModTime time.Time `json:"modified_at"`
	// MIME类型
	ContentType string `json:"content_type"`
	// 可见性
	FileVisibility string `json:"visibility"`
	// 元数据
	MetaData map[string]interface{} `json:"metadata,omitempty"`
}

FileInfo 包含文件的基本信息

type FileSystem

type FileSystem interface {
	// Get 获取指定路径的文件
	Get(ctx context.Context, path string) (File, error)

	// Exists 检查文件是否存在
	Exists(ctx context.Context, path string) (bool, error)

	// Write 写入文件内容
	Write(ctx context.Context, path string, content []byte, options ...WriteOption) error

	// WriteStream 通过流写入文件
	WriteStream(ctx context.Context, path string, content io.Reader, options ...WriteOption) error

	// Delete 删除文件
	Delete(ctx context.Context, path string) error

	// DeleteDirectory 删除目录及其内容
	DeleteDirectory(ctx context.Context, path string) error

	// CreateDirectory 创建目录
	CreateDirectory(ctx context.Context, path string, options ...WriteOption) error

	// Files 列出目录下的所有文件
	Files(ctx context.Context, directory string) ([]File, error)

	// AllFiles 递归列出目录下的所有文件
	AllFiles(ctx context.Context, directory string) ([]File, error)

	// Directories 列出目录下的所有子目录
	Directories(ctx context.Context, directory string) ([]string, error)

	// AllDirectories 递归列出目录下的所有子目录
	AllDirectories(ctx context.Context, directory string) ([]string, error)

	// Copy 复制文件
	Copy(ctx context.Context, source, destination string) error

	// Move 移动文件
	Move(ctx context.Context, source, destination string) error

	// Size 获取文件大小
	Size(ctx context.Context, path string) (int64, error)

	// LastModified 获取文件修改时间
	LastModified(ctx context.Context, path string) (time.Time, error)

	// MimeType 获取文件MIME类型
	MimeType(ctx context.Context, path string) (string, error)

	// SetVisibility 设置文件可见性
	SetVisibility(ctx context.Context, path, visibility string) error

	// Visibility 获取文件可见性
	Visibility(ctx context.Context, path string) (string, error)

	// URL 获取文件URL
	URL(ctx context.Context, path string) string

	// TemporaryURL 获取临时URL
	TemporaryURL(ctx context.Context, path string, expiration time.Duration) (string, error)

	// Checksum 计算文件校验和
	Checksum(ctx context.Context, path string, algorithm string) (string, error)
}

FileSystem 文件存储系统接口

func ConvertToStorageFileSystem

func ConvertToStorageFileSystem(coreFS core.FileSystem) FileSystem

ConvertToStorageFileSystem 将core.FileSystem转换为storage.FileSystem

type FileSystemAdapter

type FileSystemAdapter struct {
	CoreFS core.FileSystem
}

FileSystemAdapter 适配器,用于将core.FileSystem转换为storage.FileSystem

func (*FileSystemAdapter) AllDirectories

func (fs *FileSystemAdapter) AllDirectories(ctx context.Context, directory string) ([]string, error)

func (*FileSystemAdapter) AllFiles

func (fs *FileSystemAdapter) AllFiles(ctx context.Context, directory string) ([]File, error)

func (*FileSystemAdapter) Checksum

func (fs *FileSystemAdapter) Checksum(ctx context.Context, path string, algorithm string) (string, error)

func (*FileSystemAdapter) Copy

func (fs *FileSystemAdapter) Copy(ctx context.Context, source, destination string) error

func (*FileSystemAdapter) CreateDirectory

func (fs *FileSystemAdapter) CreateDirectory(ctx context.Context, path string, options ...WriteOption) error

func (*FileSystemAdapter) Delete

func (fs *FileSystemAdapter) Delete(ctx context.Context, path string) error

func (*FileSystemAdapter) DeleteDirectory

func (fs *FileSystemAdapter) DeleteDirectory(ctx context.Context, path string) error

func (*FileSystemAdapter) Directories

func (fs *FileSystemAdapter) Directories(ctx context.Context, directory string) ([]string, error)

func (*FileSystemAdapter) Exists

func (fs *FileSystemAdapter) Exists(ctx context.Context, path string) (bool, error)

func (*FileSystemAdapter) Files

func (fs *FileSystemAdapter) Files(ctx context.Context, directory string) ([]File, error)

func (*FileSystemAdapter) Get

func (fs *FileSystemAdapter) Get(ctx context.Context, path string) (File, error)

实现FileSystem接口

func (*FileSystemAdapter) LastModified

func (fs *FileSystemAdapter) LastModified(ctx context.Context, path string) (time.Time, error)

func (*FileSystemAdapter) MimeType

func (fs *FileSystemAdapter) MimeType(ctx context.Context, path string) (string, error)

func (*FileSystemAdapter) Move

func (fs *FileSystemAdapter) Move(ctx context.Context, source, destination string) error

func (*FileSystemAdapter) SetVisibility

func (fs *FileSystemAdapter) SetVisibility(ctx context.Context, path, visibility string) error

func (*FileSystemAdapter) Size

func (fs *FileSystemAdapter) Size(ctx context.Context, path string) (int64, error)

func (*FileSystemAdapter) TemporaryURL

func (fs *FileSystemAdapter) TemporaryURL(ctx context.Context, path string, expiration time.Duration) (string, error)

func (*FileSystemAdapter) URL

func (fs *FileSystemAdapter) URL(ctx context.Context, path string) string

func (*FileSystemAdapter) Visibility

func (fs *FileSystemAdapter) Visibility(ctx context.Context, path string) (string, error)

func (*FileSystemAdapter) Write

func (fs *FileSystemAdapter) Write(ctx context.Context, path string, content []byte, options ...WriteOption) error

func (*FileSystemAdapter) WriteStream

func (fs *FileSystemAdapter) WriteStream(ctx context.Context, path string, content io.Reader, options ...WriteOption) error

type FileValidator

type FileValidator func(ctx context.Context, file *UploadedFile) error

FileValidator 文件验证器接口

func DocumentValidator

func DocumentValidator() FileValidator

DocumentValidator 创建文档验证器

func ImageValidator

func ImageValidator() FileValidator

ImageValidator 创建图片验证器

func PDFValidator

func PDFValidator() FileValidator

PDFValidator 创建PDF验证器

type ImageFormat

type ImageFormat string

ImageFormat 图片格式

const (
	JPEG ImageFormat = "jpeg"
	PNG  ImageFormat = "png"
	GIF  ImageFormat = "gif"
)

支持的图片格式

type ImageResizeOptions

type ImageResizeOptions struct {
	// 目标宽度
	Width int

	// 目标高度
	Height int

	// 质量 (1-100,仅对JPEG有效)
	Quality int

	// 目标格式
	Format ImageFormat

	// 是否保持比例
	KeepAspectRatio bool

	// 调整模式:fit, fill, stretch
	Mode string

	// 填充颜色 (用于填充模式)
	FillColor string

	// 是否保存原始文件
	KeepOriginal bool

	// 目标路径
	TargetPath string

	// 目标磁盘
	TargetDisk string
}

ImageResizeOptions 图片缩放选项

func DefaultImageResizeOptions

func DefaultImageResizeOptions() *ImageResizeOptions

DefaultImageResizeOptions 返回默认的图片缩放选项

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager 存储管理器

func NewManager

func NewManager() *Manager

NewManager 创建新的存储管理器

func (*Manager) AllFiles

func (m *Manager) AllFiles(ctx context.Context, directory string) ([]File, error)

AllFiles 递归列出默认磁盘目录下的所有文件

func (*Manager) Copy

func (m *Manager) Copy(ctx context.Context, source, destination string) error

Copy 在默认磁盘上复制文件

func (*Manager) CreateDirectory

func (m *Manager) CreateDirectory(ctx context.Context, path string, options ...WriteOption) error

CreateDirectory 在默认磁盘上创建目录

func (*Manager) DefaultDisk

func (m *Manager) DefaultDisk() (FileSystem, error)

DefaultDisk 获取默认文件系统驱动

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, path string) error

Delete 从默认磁盘删除文件

func (*Manager) DeleteDirectory

func (m *Manager) DeleteDirectory(ctx context.Context, path string) error

DeleteDirectory 从默认磁盘删除目录

func (*Manager) Directories

func (m *Manager) Directories(ctx context.Context, directory string) ([]string, error)

Directories 列出默认磁盘目录下的所有子目录

func (*Manager) Disk

func (m *Manager) Disk(name string) (FileSystem, error)

Disk 获取指定名称的文件系统驱动

func (*Manager) Exists

func (m *Manager) Exists(ctx context.Context, path string) (bool, error)

Exists 检查文件在默认磁盘上是否存在

func (*Manager) Files

func (m *Manager) Files(ctx context.Context, directory string) ([]File, error)

Files 列出默认磁盘目录下的所有文件

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, path string) (File, error)

Get 从默认磁盘获取文件

func (*Manager) GetDefaultDiskName

func (m *Manager) GetDefaultDiskName() string

GetDefaultDiskName 获取默认文件系统驱动名称

func (*Manager) GetDiskNames

func (m *Manager) GetDiskNames() []string

GetDiskNames 获取所有已注册的文件系统驱动名称

func (*Manager) HasDisk

func (m *Manager) HasDisk(name string) bool

HasDisk 检查指定名称的文件系统驱动是否存在

func (*Manager) Move

func (m *Manager) Move(ctx context.Context, source, destination string) error

Move 在默认磁盘上移动文件

func (*Manager) RegisterDisk

func (m *Manager) RegisterDisk(name string, fs interface{})

RegisterDisk 注册存储驱动

func (*Manager) SetDefaultDisk

func (m *Manager) SetDefaultDisk(name string) error

SetDefaultDisk 设置默认文件系统驱动

func (*Manager) TemporaryURL

func (m *Manager) TemporaryURL(ctx context.Context, path string, expiration time.Duration) (string, error)

TemporaryURL 获取默认磁盘上文件的临时URL

func (*Manager) URL

func (m *Manager) URL(ctx context.Context, path string) (string, error)

URL 获取默认磁盘上文件的URL

func (*Manager) UnregisterDisk

func (m *Manager) UnregisterDisk(name string)

UnregisterDisk 注销文件系统驱动

func (*Manager) Write

func (m *Manager) Write(ctx context.Context, path string, content []byte, options ...WriteOption) error

Write 向默认磁盘写入文件

func (*Manager) WriteStream

func (m *Manager) WriteStream(ctx context.Context, path string, content interface{}, options ...WriteOption) error

WriteStream 向默认磁盘通过流写入文件

type MemoryCache

type MemoryCache struct {
	// contains filtered or unexported fields
}

MemoryCache 内存缓存实现

func (*MemoryCache) Flush

func (c *MemoryCache) Flush(ctx context.Context) error

Flush 清空所有缓存

func (*MemoryCache) FlushByPattern

func (c *MemoryCache) FlushByPattern(ctx context.Context, pattern string) error

FlushByPattern 按模式清空缓存

func (*MemoryCache) Forget

func (c *MemoryCache) Forget(ctx context.Context, key string) error

Forget 删除缓存项

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key string, value interface{}) (bool, error)

Get 从缓存获取值

func (*MemoryCache) Set

func (c *MemoryCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set 设置缓存值

type Metadata

type Metadata struct {
	// Path 文件路径
	Path string `json:"path"`

	// Name 文件名
	Name string `json:"name"`

	// Extension 文件扩展名
	Extension string `json:"extension"`

	// Size 文件大小(字节)
	Size int64 `json:"size"`

	// LastModified 最后修改时间
	LastModified time.Time `json:"last_modified"`

	// MimeType 文件MIME类型
	MimeType string `json:"mime_type"`

	// Visibility 文件可见性
	Visibility string `json:"visibility"`

	// Checksum 文件校验和
	Checksum string `json:"checksum,omitempty"`

	// IsDirectory 是否为目录
	IsDirectory bool `json:"is_directory"`

	// Custom 自定义元数据
	Custom map[string]interface{} `json:"custom,omitempty"`
}

Metadata 文件元数据管理

func NewMetadata

func NewMetadata(path string) *Metadata

NewMetadata 创建新的文件元数据对象

func (*Metadata) WithChecksum

func (m *Metadata) WithChecksum(checksum string) *Metadata

WithChecksum 设置校验和

func (*Metadata) WithCustom

func (m *Metadata) WithCustom(key string, value interface{}) *Metadata

WithCustom 设置自定义元数据

func (*Metadata) WithIsDirectory

func (m *Metadata) WithIsDirectory(isDirectory bool) *Metadata

WithIsDirectory 设置是否为目录

func (*Metadata) WithLastModified

func (m *Metadata) WithLastModified(lastModified time.Time) *Metadata

WithLastModified 设置最后修改时间

func (*Metadata) WithMimeType

func (m *Metadata) WithMimeType(mimeType string) *Metadata

WithMimeType 设置MIME类型

func (*Metadata) WithSize

func (m *Metadata) WithSize(size int64) *Metadata

WithSize 设置文件大小

func (*Metadata) WithVisibility

func (m *Metadata) WithVisibility(visibility string) *Metadata

WithVisibility 设置可见性

type MigrationOptions

type MigrationOptions struct {
	// 是否递归处理子目录
	Recursive bool

	// 文件匹配模式(例如 *.jpg)
	Pattern string

	// 是否保留目录结构
	PreserveStructure bool

	// 目标目录
	TargetDirectory string

	// 并发迁移的最大文件数
	MaxConcurrent int

	// 失败时是否继续
	ContinueOnError bool

	// 是否覆盖目标文件
	Overwrite bool

	// 处理进度回调
	ProgressCallback func(processed, total int, currentFile string)
}

MigrationOptions 迁移选项

func DefaultMigrationOptions

func DefaultMigrationOptions() *MigrationOptions

DefaultMigrationOptions 返回默认的迁移选项

type MigrationResult

type MigrationResult struct {
	// 成功迁移的文件数
	SuccessCount int

	// 失败的文件数
	FailedCount int

	// 总处理文件数
	TotalCount int

	// 失败的文件及错误
	Failures map[string]error

	// 开始时间
	StartTime time.Time

	// 结束时间
	EndTime time.Time

	// 耗时
	Duration time.Duration
}

MigrationResult 迁移结果

type Migrator

type Migrator struct {
	// contains filtered or unexported fields
}

Migrator 文件迁移器

func NewMigrator

func NewMigrator(manager *Manager) *Migrator

NewMigrator 创建文件迁移器

func (*Migrator) BatchCopy

func (m *Migrator) BatchCopy(ctx context.Context, disk string, sourceDirectory, targetDirectory string, options *MigrationOptions) (*MigrationResult, error)

BatchCopy 批量复制文件

func (*Migrator) BatchMove

func (m *Migrator) BatchMove(ctx context.Context, disk string, sourceDirectory, targetDirectory string, options *MigrationOptions) (*MigrationResult, error)

BatchMove 批量移动文件

func (*Migrator) Copy

func (m *Migrator) Copy(ctx context.Context, disk, sourcePath, targetPath string) error

Copy 复制文件到不同目录

func (*Migrator) Migrate

func (m *Migrator) Migrate(ctx context.Context, sourceDisk, targetDisk string, sourceDirectory string, options *MigrationOptions) (*MigrationResult, error)

Migrate 在两个存储之间迁移文件

func (*Migrator) Move

func (m *Migrator) Move(ctx context.Context, disk, sourcePath, targetPath string) error

Move 移动文件到不同目录

func (*Migrator) Sync

func (m *Migrator) Sync(ctx context.Context, sourceDisk, targetDisk string, directory string, options *MigrationOptions) (*MigrationResult, error)

Sync 同步两个存储之间的文件

type ProcessorManager

type ProcessorManager struct {
	// contains filtered or unexported fields
}

ProcessorManager 文件处理管理器

func NewProcessorManager

func NewProcessorManager(manager *Manager) *ProcessorManager

NewProcessorManager 创建新的文件处理管理器

func (*ProcessorManager) AddWatermark

func (pm *ProcessorManager) AddWatermark(ctx context.Context, sourcePath string, options *WatermarkOptions) (string, error)

AddWatermark 添加水印

func (*ProcessorManager) BatchProcess

func (pm *ProcessorManager) BatchProcess(ctx context.Context, directory string, options *BatchProcessOptions) (*BatchProcessResult, error)

BatchProcess 批量处理文件

func (*ProcessorManager) ConvertFile

func (pm *ProcessorManager) ConvertFile(ctx context.Context, sourcePath string, options *FileConvertOptions) (string, error)

ConvertFile 转换文件格式

func (*ProcessorManager) ResizeImage

func (pm *ProcessorManager) ResizeImage(ctx context.Context, sourcePath string, options *ImageResizeOptions) (string, error)

ResizeImage 调整图片大小

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider 文件存储系统服务提供者

func NewProvider

func NewProvider(config StorageConfig) *Provider

NewProvider 创建文件存储系统服务提供者

func (*Provider) Boot

func (p *Provider) Boot(container *di.Container) error

Boot 启动服务

func (*Provider) Register

func (p *Provider) Register(container *di.Container) error

Register 注册服务

type StorageConfig

type StorageConfig struct {
	// 默认驱动
	DefaultDisk string `mapstructure:"default_disk"`

	// 磁盘配置
	Disks map[string]DiskConfig `mapstructure:"disks"`
}

StorageConfig 文件存储系统配置

func DefaultConfig

func DefaultConfig() StorageConfig

DefaultConfig 返回默认配置

type StorageToCoreFSAdapter

type StorageToCoreFSAdapter struct {
	StorageFS FileSystem
}

StorageToCoreFSAdapter 适配器,将storage.FileSystem转换为core.FileSystem

func (*StorageToCoreFSAdapter) AllDirectories

func (fs *StorageToCoreFSAdapter) AllDirectories(ctx context.Context, directory string) ([]string, error)

func (*StorageToCoreFSAdapter) AllFiles

func (fs *StorageToCoreFSAdapter) AllFiles(ctx context.Context, directory string) ([]core.File, error)

func (*StorageToCoreFSAdapter) Checksum

func (fs *StorageToCoreFSAdapter) Checksum(ctx context.Context, path string, algorithm string) (string, error)

func (*StorageToCoreFSAdapter) Copy

func (fs *StorageToCoreFSAdapter) Copy(ctx context.Context, source, destination string) error

func (*StorageToCoreFSAdapter) CreateDirectory

func (fs *StorageToCoreFSAdapter) CreateDirectory(ctx context.Context, path string, options ...core.WriteOption) error

func (*StorageToCoreFSAdapter) Delete

func (fs *StorageToCoreFSAdapter) Delete(ctx context.Context, path string) error

func (*StorageToCoreFSAdapter) DeleteDirectory

func (fs *StorageToCoreFSAdapter) DeleteDirectory(ctx context.Context, path string) error

func (*StorageToCoreFSAdapter) Directories

func (fs *StorageToCoreFSAdapter) Directories(ctx context.Context, directory string) ([]string, error)

func (*StorageToCoreFSAdapter) Exists

func (fs *StorageToCoreFSAdapter) Exists(ctx context.Context, path string) (bool, error)

func (*StorageToCoreFSAdapter) Files

func (fs *StorageToCoreFSAdapter) Files(ctx context.Context, directory string) ([]core.File, error)

func (*StorageToCoreFSAdapter) Get

func (fs *StorageToCoreFSAdapter) Get(ctx context.Context, path string) (core.File, error)

实现core.FileSystem接口

func (*StorageToCoreFSAdapter) LastModified

func (fs *StorageToCoreFSAdapter) LastModified(ctx context.Context, path string) (time.Time, error)

func (*StorageToCoreFSAdapter) MimeType

func (fs *StorageToCoreFSAdapter) MimeType(ctx context.Context, path string) (string, error)

func (*StorageToCoreFSAdapter) Move

func (fs *StorageToCoreFSAdapter) Move(ctx context.Context, source, destination string) error

func (*StorageToCoreFSAdapter) SetVisibility

func (fs *StorageToCoreFSAdapter) SetVisibility(ctx context.Context, path, visibility string) error

func (*StorageToCoreFSAdapter) Size

func (fs *StorageToCoreFSAdapter) Size(ctx context.Context, path string) (int64, error)

func (*StorageToCoreFSAdapter) TemporaryURL

func (fs *StorageToCoreFSAdapter) TemporaryURL(ctx context.Context, path string, expiration time.Duration) (string, error)

func (*StorageToCoreFSAdapter) URL

func (fs *StorageToCoreFSAdapter) URL(ctx context.Context, path string) string

func (*StorageToCoreFSAdapter) Visibility

func (fs *StorageToCoreFSAdapter) Visibility(ctx context.Context, path string) (string, error)

func (*StorageToCoreFSAdapter) Write

func (fs *StorageToCoreFSAdapter) Write(ctx context.Context, path string, content []byte, options ...core.WriteOption) error

func (*StorageToCoreFSAdapter) WriteStream

func (fs *StorageToCoreFSAdapter) WriteStream(ctx context.Context, path string, content io.Reader, options ...core.WriteOption) error

type StorageToCoreFSFileAdapter

type StorageToCoreFSFileAdapter struct {
	StorageFile File
}

StorageToCoreFSFileAdapter 适配器,将storage.File转换为core.File

func (*StorageToCoreFSFileAdapter) Extension

func (f *StorageToCoreFSFileAdapter) Extension() string

func (*StorageToCoreFSFileAdapter) IsDirectory

func (f *StorageToCoreFSFileAdapter) IsDirectory() bool

func (*StorageToCoreFSFileAdapter) LastModified

func (f *StorageToCoreFSFileAdapter) LastModified() time.Time

func (*StorageToCoreFSFileAdapter) Metadata

func (f *StorageToCoreFSFileAdapter) Metadata() map[string]interface{}

func (*StorageToCoreFSFileAdapter) MimeType

func (f *StorageToCoreFSFileAdapter) MimeType() string

func (*StorageToCoreFSFileAdapter) Name

func (*StorageToCoreFSFileAdapter) Path

实现core.File接口

func (*StorageToCoreFSFileAdapter) Read

func (*StorageToCoreFSFileAdapter) ReadStream

func (*StorageToCoreFSFileAdapter) Size

func (*StorageToCoreFSFileAdapter) TemporaryURL

func (f *StorageToCoreFSFileAdapter) TemporaryURL(ctx context.Context, expiration time.Duration) (string, error)

func (*StorageToCoreFSFileAdapter) URL

func (*StorageToCoreFSFileAdapter) Visibility

func (f *StorageToCoreFSFileAdapter) Visibility() string

type UploadedFile

type UploadedFile struct {
	// Filename 原始文件名
	Filename string

	// SavedAs 保存后的文件名
	SavedAs string

	// Size 文件大小
	Size int64

	// MimeType MIME类型
	MimeType string

	// Extension 文件扩展名
	Extension string

	// Content 文件内容
	Content []byte

	// Stream 文件数据流
	Stream io.Reader

	// Path 保存路径
	Path string

	// URL 文件URL
	URL string

	// Metadata 文件元数据
	Metadata *Metadata
}

UploadedFile 上传的文件信息

type Uploader

type Uploader struct {
	// contains filtered or unexported fields
}

Uploader 文件上传助手

func NewUploader

func NewUploader(manager *Manager) *Uploader

NewUploader 创建新的文件上传助手

func (*Uploader) AfterUpload

func (u *Uploader) AfterUpload(callback AfterUploadFunc) *Uploader

AfterUpload 添加上传后回调函数

func (*Uploader) AllowMimeTypes

func (u *Uploader) AllowMimeTypes(mimeTypes ...string) *Uploader

AllowMimeTypes 设置允许的MIME类型

func (*Uploader) AllowOverwrite

func (u *Uploader) AllowOverwrite(allow bool) *Uploader

AllowOverwrite 设置是否允许覆盖同名文件

func (*Uploader) BeforeUpload

func (u *Uploader) BeforeUpload(callback BeforeUploadFunc) *Uploader

BeforeUpload 添加上传前回调函数

func (*Uploader) GenerateUniqueName

func (u *Uploader) GenerateUniqueName(generate bool) *Uploader

GenerateUniqueName 设置是否生成唯一文件名

func (*Uploader) InDirectory

func (u *Uploader) InDirectory(directory string) *Uploader

InDirectory 设置上传目录

func (*Uploader) MaxFileSize

func (u *Uploader) MaxFileSize(size int64) *Uploader

MaxFileSize 设置最大文件大小

func (*Uploader) UploadFile

func (u *Uploader) UploadFile(ctx context.Context, content []byte, filename string) (*UploadedFile, error)

UploadFile 上传文件内容

func (*Uploader) UploadFromMultipartFile

func (u *Uploader) UploadFromMultipartFile(ctx context.Context, file multipart.File, header *multipart.FileHeader) (*UploadedFile, error)

UploadFromMultipartFile 从multipart.File上传文件

func (*Uploader) UploadFromRequest

func (u *Uploader) UploadFromRequest(ctx context.Context, r *http.Request, fieldName string) (*UploadedFile, error)

UploadFromRequest 从HTTP请求中上传文件

func (*Uploader) UploadMultiple

func (u *Uploader) UploadMultiple(ctx context.Context, files map[string][]byte) ([]*UploadedFile, error)

UploadMultiple 上传多个文件

func (*Uploader) UseDisk

func (u *Uploader) UseDisk(disk string) *Uploader

UseDisk 设置使用的存储驱动

func (*Uploader) WithCustomNamer

func (u *Uploader) WithCustomNamer(namer func(filename string) string) *Uploader

WithCustomNamer 设置自定义文件命名函数

func (*Uploader) WithValidator

func (u *Uploader) WithValidator(validator FileValidator) *Uploader

WithValidator 添加文件验证器

func (*Uploader) WithVisibility

func (u *Uploader) WithVisibility(visibility string) *Uploader

WithVisibility 设置文件可见性

type WatermarkOptions

type WatermarkOptions struct {
	// 水印图片路径
	WatermarkPath string

	// 水印位置: top-left, top-right, bottom-left, bottom-right, center
	Position string

	// 水印透明度 (0-100)
	Opacity int

	// 水印边距
	Margin int

	// 是否保存原始文件
	KeepOriginal bool

	// 目标路径
	TargetPath string

	// 目标磁盘
	TargetDisk string
}

WatermarkOptions 水印选项

func DefaultWatermarkOptions

func DefaultWatermarkOptions() *WatermarkOptions

DefaultWatermarkOptions 返回默认的水印选项

type WriteOption

type WriteOption func(*WriteOptions)

WriteOption 写入文件的选项

func ConvertToStorageWriteOptions

func ConvertToStorageWriteOptions(options []core.WriteOption) []WriteOption

ConvertToStorageWriteOptions 将core.WriteOption转换为storage.WriteOption

func WithMetadata

func WithMetadata(metadata map[string]interface{}) WriteOption

WithMetadata 设置文件元数据选项

func WithMimeType

func WithMimeType(mimeType string) WriteOption

WithMimeType 设置文件MIME类型选项

func WithOverwrite

func WithOverwrite(overwrite bool) WriteOption

WithOverwrite 设置是否覆盖已存在文件选项

func WithPermissions

func WithPermissions(permissions os.FileMode) WriteOption

WithPermissions 设置文件权限选项

func WithVisibility

func WithVisibility(visibility string) WriteOption

WithVisibility 设置文件可见性选项

type WriteOptions

type WriteOptions struct {
	// 文件可见性:public 或 private
	Visibility string

	// 文件元数据
	Metadata map[string]interface{}

	// 文件权限
	Permissions os.FileMode

	// MIME类型
	MimeType string

	// 是否覆盖已存在的文件
	Overwrite bool
}

WriteOptions 写入文件的选项集合

func DefaultWriteOptions

func DefaultWriteOptions() *WriteOptions

DefaultWriteOptions 返回默认的写入选项

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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