storage

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package storage 提供文件存储的抽象层和不同存储策略的实现

Index

Constants

View Source
const (
	// ShareDownloadPath 分享下载路径
	ShareDownloadPath = "/share/download"
)

Variables

This section is empty.

Functions

func CalculateFileHash

func CalculateFileHash(file *multipart.FileHeader) (string, error)

func GenerateFileInfo

func GenerateFileInfo(fileName string, uploadID string) (path, suffix, prefix, uuidFileName string)

GenerateFileInfo 生成文件信息 (不依赖PathManager)

func GenerateFilePath

func GenerateFilePath(fileName string, uploadID string, pathManager *PathManager) (path, suffix, prefix, uuidFileName, savePath string)

GenerateFilePath 生成文件路径 (兼容旧代码)

func ParseExpireInfo

func ParseExpireInfo(expireValue int, expireStyle string) (expiredAt *int64, expiredCount int, usedCount int)

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) ServeFile

func (ls *LocalStorageStrategy) ServeFile(c *gin.Context, filePath string, fileName string) error

ServeFile 提供文件下载服务

func (*LocalStorageStrategy) TestConnection

func (ls *LocalStorageStrategy) TestConnection() error

TestConnection 测试本地存储连接

func (*LocalStorageStrategy) WriteFile

func (ls *LocalStorageStrategy) WriteFile(path string, data []byte) error

WriteFile 写入文件

type PathManager

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

PathManager 路径管理器,用于统一处理各种存储后端的路径

func NewPathManager

func NewPathManager(basePath string) *PathManager

NewPathManager 创建路径管理器

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) ServeFile

func (ss *S3StorageStrategy) ServeFile(c *gin.Context, filePath string, fileName string) error

ServeFile 提供文件下载服务

func (*S3StorageStrategy) TestConnection

func (ss *S3StorageStrategy) TestConnection() error

TestConnection 测试 S3 连接

func (*S3StorageStrategy) WriteFile

func (ss *S3StorageStrategy) WriteFile(path string, data []byte) error

WriteFile 写入文件

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) 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

func (so *StorageOperator) GetFileResponse(c *gin.Context, fileCode *models.FileCode) error

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

func (sbs *StrategyBasedStorage) GetFileResponse(c *gin.Context, fileCode *models.FileCode) error

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) ServeFile

func (ws *WebDAVStorageStrategy) ServeFile(c *gin.Context, filePath string, fileName string) error

ServeFile 提供文件下载服务

func (*WebDAVStorageStrategy) TestConnection

func (ws *WebDAVStorageStrategy) TestConnection() error

TestConnection 测试 WebDAV 连接

func (*WebDAVStorageStrategy) WriteFile

func (ws *WebDAVStorageStrategy) WriteFile(path string, data []byte) error

WriteFile 写入文件

Jump to

Keyboard shortcuts

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