Documentation
¶
Overview ¶
Package storage 提供文件存储的抽象层和不同存储策略的实现
Index ¶
- Constants
- func CalculateFileHash(file *multipart.FileHeader) (string, error)
- func GenerateFileInfo(fileName string, uploadID string) (path, suffix, prefix, uuidFileName string)
- func GenerateFilePath(fileName string, uploadID string, pathManager *PathManager) (path, suffix, prefix, uuidFileName, savePath string)
- func ParseExpireInfo(expireValue int, expireStyle string) (expiredAt *int64, expiredCount int, usedCount int)
- func ValidateFileSize(file *multipart.FileHeader, maxSize int64) error
- type LocalStorageStrategy
- func (ls *LocalStorageStrategy) DeleteFile(path string) error
- func (ls *LocalStorageStrategy) FileExists(path string) bool
- func (ls *LocalStorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
- func (ls *LocalStorageStrategy) ReadFile(path string) ([]byte, error)
- func (ls *LocalStorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
- func (ls *LocalStorageStrategy) ServeFile(c *gin.Context, filePath string, fileName string) error
- func (ls *LocalStorageStrategy) TestConnection() error
- func (ls *LocalStorageStrategy) WriteFile(path string, data []byte) error
- type NFSStorageStrategy
- func (nfs *NFSStorageStrategy) Cleanup() error
- func (nfs *NFSStorageStrategy) DeleteFile(path string) error
- func (nfs *NFSStorageStrategy) FileExists(path string) bool
- func (nfs *NFSStorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
- func (nfs *NFSStorageStrategy) GetMountInfo() map[string]interface{}
- func (nfs *NFSStorageStrategy) ReadFile(path string) ([]byte, error)
- func (nfs *NFSStorageStrategy) Remount() error
- func (nfs *NFSStorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
- func (nfs *NFSStorageStrategy) ServeFile(c *gin.Context, filePath string, fileName string) error
- func (nfs *NFSStorageStrategy) TestConnection() error
- func (nfs *NFSStorageStrategy) WriteFile(path string, data []byte) error
- type PathManager
- func (pm *PathManager) CleanPath(path string) string
- func (pm *PathManager) GetChunkBasePath() string
- func (pm *PathManager) GetChunkDir(uploadID string) string
- func (pm *PathManager) GetChunkPath(uploadID string, chunkIndex int) string
- func (pm *PathManager) GetDateBasedPath(filename string) string
- func (pm *PathManager) GetFullPath(relativePath string) string
- type S3StorageStrategy
- func (ss *S3StorageStrategy) DeleteFile(path string) error
- func (ss *S3StorageStrategy) FileExists(path string) bool
- func (ss *S3StorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
- func (ss *S3StorageStrategy) ReadFile(path string) ([]byte, error)
- func (ss *S3StorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
- func (ss *S3StorageStrategy) ServeFile(c *gin.Context, filePath string, fileName string) error
- func (ss *S3StorageStrategy) TestConnection() error
- func (ss *S3StorageStrategy) WriteFile(path string, data []byte) error
- type StorageInterface
- type StorageManager
- func (sm *StorageManager) GetAvailableStorages() []string
- func (sm *StorageManager) GetCurrentStorage() string
- func (sm *StorageManager) GetStorage() StorageInterface
- func (sm *StorageManager) GetStorageInstance(storageType string) (StorageInterface, bool)
- func (sm *StorageManager) ReconfigureNFS(server, nfsPath, mountPoint, version, options string, timeout int, ...) error
- func (sm *StorageManager) ReconfigureWebDAV(hostname, username, password, rootPath string) error
- func (sm *StorageManager) SwitchStorage(storageType string) error
- func (sm *StorageManager) TestStorage(storageType string) error
- type StorageOperator
- func (so *StorageOperator) CleanChunks(uploadID string) error
- func (so *StorageOperator) DeleteFile(fileCode *models.FileCode) error
- func (so *StorageOperator) GetFileResponse(c *gin.Context, fileCode *models.FileCode) error
- func (so *StorageOperator) GetFileURL(fileCode *models.FileCode) (string, error)
- func (so *StorageOperator) MergeChunks(uploadID string, chunk *models.UploadChunk, savePath string) error
- func (so *StorageOperator) SaveChunk(uploadID string, chunkIndex int, data []byte, chunkHash string) error
- func (so *StorageOperator) SaveFile(file *multipart.FileHeader, savePath string) error
- type StorageStrategy
- type StrategyBasedStorage
- func (sbs *StrategyBasedStorage) CleanChunks(uploadID string) error
- func (sbs *StrategyBasedStorage) DeleteFile(fileCode *models.FileCode) error
- func (sbs *StrategyBasedStorage) GetFileResponse(c *gin.Context, fileCode *models.FileCode) error
- func (sbs *StrategyBasedStorage) GetFileURL(fileCode *models.FileCode) (string, error)
- func (sbs *StrategyBasedStorage) MergeChunks(uploadID string, chunk *models.UploadChunk, savePath string) error
- func (sbs *StrategyBasedStorage) SaveChunk(uploadID string, chunkIndex int, data []byte, chunkHash string) error
- func (sbs *StrategyBasedStorage) SaveFile(file *multipart.FileHeader, savePath string) error
- func (sbs *StrategyBasedStorage) TestConnection() error
- type WebDAVStorageStrategy
- func (ws *WebDAVStorageStrategy) DeleteFile(path string) error
- func (ws *WebDAVStorageStrategy) FileExists(path string) bool
- func (ws *WebDAVStorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
- func (ws *WebDAVStorageStrategy) ReadFile(path string) ([]byte, error)
- func (ws *WebDAVStorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
- func (ws *WebDAVStorageStrategy) ServeFile(c *gin.Context, filePath string, fileName string) error
- func (ws *WebDAVStorageStrategy) TestConnection() error
- func (ws *WebDAVStorageStrategy) WriteFile(path string, data []byte) error
Constants ¶
const (
ShareDownloadPath = "/share/download"
)
Variables ¶
This section is empty.
Functions ¶
func CalculateFileHash ¶
func CalculateFileHash(file *multipart.FileHeader) (string, error)
func GenerateFileInfo ¶
GenerateFileInfo 生成文件信息 (不依赖PathManager)
func GenerateFilePath ¶
func GenerateFilePath(fileName string, uploadID string, pathManager *PathManager) (path, suffix, prefix, uuidFileName, savePath string)
GenerateFilePath 生成文件路径 (兼容旧代码)
func ParseExpireInfo ¶
func ValidateFileSize ¶
func ValidateFileSize(file *multipart.FileHeader, maxSize int64) error
Types ¶
type LocalStorageStrategy ¶
type LocalStorageStrategy struct {
// contains filtered or unexported fields
}
LocalStorageStrategy 本地存储策略实现
func NewLocalStorageStrategy ¶
func NewLocalStorageStrategy(basePath string) *LocalStorageStrategy
NewLocalStorageStrategy 创建本地存储策略
func (*LocalStorageStrategy) DeleteFile ¶
func (ls *LocalStorageStrategy) DeleteFile(path string) error
DeleteFile 删除文件
func (*LocalStorageStrategy) FileExists ¶
func (ls *LocalStorageStrategy) FileExists(path string) bool
FileExists 检查文件是否存在
func (*LocalStorageStrategy) GenerateFileURL ¶
func (ls *LocalStorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
GenerateFileURL 生成文件URL
func (*LocalStorageStrategy) ReadFile ¶
func (ls *LocalStorageStrategy) ReadFile(path string) ([]byte, error)
ReadFile 读取文件
func (*LocalStorageStrategy) SaveUploadFile ¶
func (ls *LocalStorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
SaveUploadFile 保存上传的文件
func (*LocalStorageStrategy) TestConnection ¶
func (ls *LocalStorageStrategy) TestConnection() error
TestConnection 测试本地存储连接
type NFSStorageStrategy ¶
type NFSStorageStrategy struct {
// contains filtered or unexported fields
}
NFSStorageStrategy NFS存储策略实现
func NewNFSStorageStrategy ¶
func NewNFSStorageStrategy(server, nfsPath, mountPoint, version, options string, timeout int, autoMount bool, retryCount int, subPath string) (*NFSStorageStrategy, error)
NewNFSStorageStrategy 创建NFS存储策略
func (*NFSStorageStrategy) DeleteFile ¶
func (nfs *NFSStorageStrategy) DeleteFile(path string) error
DeleteFile 删除文件
func (*NFSStorageStrategy) FileExists ¶
func (nfs *NFSStorageStrategy) FileExists(path string) bool
FileExists 检查文件是否存在
func (*NFSStorageStrategy) GenerateFileURL ¶
func (nfs *NFSStorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
GenerateFileURL 生成文件URL
func (*NFSStorageStrategy) GetMountInfo ¶
func (nfs *NFSStorageStrategy) GetMountInfo() map[string]interface{}
GetMountInfo 获取挂载信息
func (*NFSStorageStrategy) ReadFile ¶
func (nfs *NFSStorageStrategy) ReadFile(path string) ([]byte, error)
ReadFile 读取文件
func (*NFSStorageStrategy) SaveUploadFile ¶
func (nfs *NFSStorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
SaveUploadFile 保存上传的文件
func (*NFSStorageStrategy) TestConnection ¶
func (nfs *NFSStorageStrategy) TestConnection() error
TestConnection 测试NFS连接
type PathManager ¶
type PathManager struct {
// contains filtered or unexported fields
}
PathManager 路径管理器,用于统一处理各种存储后端的路径
func (*PathManager) CleanPath ¶
func (pm *PathManager) CleanPath(path string) string
CleanPath 清理路径,确保路径格式正确
func (*PathManager) GetChunkBasePath ¶
func (pm *PathManager) GetChunkBasePath() string
GetChunkBasePath 获取分片存储的基础路径
func (*PathManager) GetChunkDir ¶
func (pm *PathManager) GetChunkDir(uploadID string) string
GetChunkDir 获取特定上传ID的分片目录
func (*PathManager) GetChunkPath ¶
func (pm *PathManager) GetChunkPath(uploadID string, chunkIndex int) string
GetChunkPath 获取特定分片的完整路径
func (*PathManager) GetDateBasedPath ¶
func (pm *PathManager) GetDateBasedPath(filename string) string
GetDateBasedPath 获取基于日期的文件路径
func (*PathManager) GetFullPath ¶
func (pm *PathManager) GetFullPath(relativePath string) string
GetFullPath 获取基于基础路径的完整路径
type S3StorageStrategy ¶
type S3StorageStrategy struct {
// contains filtered or unexported fields
}
S3StorageStrategy S3 存储策略实现
func NewS3StorageStrategy ¶
func NewS3StorageStrategy(accessKeyID, secretAccessKey, bucketName, endpointURL, regionName, sessionToken, hostname string, proxy bool, basePath string) (*S3StorageStrategy, error)
NewS3StorageStrategy 创建 S3 存储策略
func (*S3StorageStrategy) DeleteFile ¶
func (ss *S3StorageStrategy) DeleteFile(path string) error
DeleteFile 删除文件(对于目录,删除所有匹配前缀的对象)
func (*S3StorageStrategy) FileExists ¶
func (ss *S3StorageStrategy) FileExists(path string) bool
FileExists 检查文件是否存在
func (*S3StorageStrategy) GenerateFileURL ¶
func (ss *S3StorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
GenerateFileURL 生成文件URL
func (*S3StorageStrategy) ReadFile ¶
func (ss *S3StorageStrategy) ReadFile(path string) ([]byte, error)
ReadFile 读取文件
func (*S3StorageStrategy) SaveUploadFile ¶
func (ss *S3StorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
SaveUploadFile 保存上传的文件
func (*S3StorageStrategy) TestConnection ¶
func (ss *S3StorageStrategy) TestConnection() error
TestConnection 测试 S3 连接
type StorageInterface ¶
type StorageInterface interface {
SaveFile(file *multipart.FileHeader, savePath string) error
SaveChunk(uploadID string, chunkIndex int, data []byte, chunkHash string) error
MergeChunks(uploadID string, chunk *models.UploadChunk, savePath string) error
CleanChunks(uploadID string) error
GetFileResponse(c *gin.Context, fileCode *models.FileCode) error
GetFileURL(fileCode *models.FileCode) (string, error)
DeleteFile(fileCode *models.FileCode) error
}
StorageInterface 存储接口
type StorageManager ¶
type StorageManager struct {
// contains filtered or unexported fields
}
StorageManager 存储管理器
func NewStorageManager ¶
func NewStorageManager(cfg *config.Config) *StorageManager
func (*StorageManager) GetAvailableStorages ¶
func (sm *StorageManager) GetAvailableStorages() []string
GetAvailableStorages 获取可用的存储类型
func (*StorageManager) GetCurrentStorage ¶
func (sm *StorageManager) GetCurrentStorage() string
GetCurrentStorage 获取当前存储类型
func (*StorageManager) GetStorage ¶
func (sm *StorageManager) GetStorage() StorageInterface
func (*StorageManager) GetStorageInstance ¶
func (sm *StorageManager) GetStorageInstance(storageType string) (StorageInterface, bool)
GetStorageInstance 获取指定类型的存储实例
func (*StorageManager) ReconfigureNFS ¶
func (sm *StorageManager) ReconfigureNFS(server, nfsPath, mountPoint, version, options string, timeout int, autoMount bool, retryCount int, subPath string) error
ReconfigureNFS 重新配置 NFS 存储
func (*StorageManager) ReconfigureWebDAV ¶
func (sm *StorageManager) ReconfigureWebDAV(hostname, username, password, rootPath string) error
ReconfigureWebDAV 重新配置 WebDAV 存储
func (*StorageManager) SwitchStorage ¶
func (sm *StorageManager) SwitchStorage(storageType string) error
SwitchStorage 切换存储后端
func (*StorageManager) TestStorage ¶
func (sm *StorageManager) TestStorage(storageType string) error
TestStorage 测试存储连接
type StorageOperator ¶
type StorageOperator struct {
// contains filtered or unexported fields
}
StorageOperator 通用存储操作器 - 包含公共逻辑
func NewStorageOperator ¶
func NewStorageOperator(strategy StorageStrategy, pathManager *PathManager) *StorageOperator
NewStorageOperator 创建存储操作器
func (*StorageOperator) CleanChunks ¶
func (so *StorageOperator) CleanChunks(uploadID string) error
CleanChunks 清理分片 - 公共逻辑
func (*StorageOperator) DeleteFile ¶
func (so *StorageOperator) DeleteFile(fileCode *models.FileCode) error
DeleteFile 删除文件 - 公共逻辑
func (*StorageOperator) GetFileResponse ¶
GetFileResponse 获取文件响应 - 公共逻辑
func (*StorageOperator) GetFileURL ¶
func (so *StorageOperator) GetFileURL(fileCode *models.FileCode) (string, error)
GetFileURL 获取文件URL - 公共逻辑
func (*StorageOperator) MergeChunks ¶
func (so *StorageOperator) MergeChunks(uploadID string, chunk *models.UploadChunk, savePath string) error
MergeChunks 合并分片 - 公共逻辑
func (*StorageOperator) SaveChunk ¶
func (so *StorageOperator) SaveChunk(uploadID string, chunkIndex int, data []byte, chunkHash string) error
SaveChunk 保存分片 - 公共逻辑
func (*StorageOperator) SaveFile ¶
func (so *StorageOperator) SaveFile(file *multipart.FileHeader, savePath string) error
SaveFile 保存文件 - 公共逻辑
type StorageStrategy ¶
type StorageStrategy interface {
// 基础文件操作
WriteFile(path string, data []byte) error
ReadFile(path string) ([]byte, error)
DeleteFile(path string) error
FileExists(path string) bool
// 上传文件操作
SaveUploadFile(file *multipart.FileHeader, savePath string) error
// 下载操作
ServeFile(c *gin.Context, filePath string, fileName string) error
GenerateFileURL(filePath string, fileName string) (string, error)
// 连接测试
TestConnection() error
}
StorageStrategy 存储策略接口 - 定义每个存储后端的差异化操作
type StrategyBasedStorage ¶
type StrategyBasedStorage struct {
// contains filtered or unexported fields
}
StrategyBasedStorage 基于策略的存储适配器
func NewStrategyBasedStorage ¶
func NewStrategyBasedStorage(strategy StorageStrategy, pathManager *PathManager) *StrategyBasedStorage
NewStrategyBasedStorage 创建基于策略的存储
func (*StrategyBasedStorage) CleanChunks ¶
func (sbs *StrategyBasedStorage) CleanChunks(uploadID string) error
CleanChunks 实现 StorageInterface
func (*StrategyBasedStorage) DeleteFile ¶
func (sbs *StrategyBasedStorage) DeleteFile(fileCode *models.FileCode) error
DeleteFile 实现 StorageInterface
func (*StrategyBasedStorage) GetFileResponse ¶
GetFileResponse 实现 StorageInterface
func (*StrategyBasedStorage) GetFileURL ¶
func (sbs *StrategyBasedStorage) GetFileURL(fileCode *models.FileCode) (string, error)
GetFileURL 实现 StorageInterface
func (*StrategyBasedStorage) MergeChunks ¶
func (sbs *StrategyBasedStorage) MergeChunks(uploadID string, chunk *models.UploadChunk, savePath string) error
MergeChunks 实现 StorageInterface
func (*StrategyBasedStorage) SaveChunk ¶
func (sbs *StrategyBasedStorage) SaveChunk(uploadID string, chunkIndex int, data []byte, chunkHash string) error
SaveChunk 实现 StorageInterface
func (*StrategyBasedStorage) SaveFile ¶
func (sbs *StrategyBasedStorage) SaveFile(file *multipart.FileHeader, savePath string) error
SaveFile 实现 StorageInterface
func (*StrategyBasedStorage) TestConnection ¶
func (sbs *StrategyBasedStorage) TestConnection() error
TestConnection 测试连接
type WebDAVStorageStrategy ¶
type WebDAVStorageStrategy struct {
// contains filtered or unexported fields
}
WebDAVStorageStrategy WebDAV 存储策略实现
func NewWebDAVStorageStrategy ¶
func NewWebDAVStorageStrategy(hostname, username, password, rootPath string) (*WebDAVStorageStrategy, error)
NewWebDAVStorageStrategy 创建 WebDAV 存储策略
func (*WebDAVStorageStrategy) DeleteFile ¶
func (ws *WebDAVStorageStrategy) DeleteFile(path string) error
DeleteFile 删除文件或目录
func (*WebDAVStorageStrategy) FileExists ¶
func (ws *WebDAVStorageStrategy) FileExists(path string) bool
FileExists 检查文件是否存在
func (*WebDAVStorageStrategy) GenerateFileURL ¶
func (ws *WebDAVStorageStrategy) GenerateFileURL(filePath string, fileName string) (string, error)
GenerateFileURL 生成文件URL
func (*WebDAVStorageStrategy) ReadFile ¶
func (ws *WebDAVStorageStrategy) ReadFile(path string) ([]byte, error)
ReadFile 读取文件
func (*WebDAVStorageStrategy) SaveUploadFile ¶
func (ws *WebDAVStorageStrategy) SaveUploadFile(file *multipart.FileHeader, savePath string) error
SaveUploadFile 保存上传的文件
func (*WebDAVStorageStrategy) TestConnection ¶
func (ws *WebDAVStorageStrategy) TestConnection() error
TestConnection 测试 WebDAV 连接