Documentation
¶
Overview ¶
Package vfs 提供虚拟文件系统抽象层,支持本地文件系统和远程存储(WebDAV、SFTP等)
Index ¶
- func CloseAll()
- func IsRemoteURL(storeURL string) bool
- func List() []string
- func Register(key string, fs FileSystem)
- func Unregister(key string)
- type BackendType
- type DirEntry
- type FTPFS
- func (f *FTPFS) BaseURL() string
- func (f *FTPFS) Close() error
- func (f *FTPFS) Exists(p string) (bool, error)
- func (f *FTPFS) GetBasePath() string
- func (f *FTPFS) IsDir(p string) (bool, error)
- func (f *FTPFS) IsRemote() bool
- func (f *FTPFS) JoinPath(elem ...string) string
- func (f *FTPFS) Open(p string) (File, error)
- func (f *FTPFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
- func (f *FTPFS) ReadDir(p string) ([]DirEntry, error)
- func (f *FTPFS) ReadFile(p string) ([]byte, error)
- func (f *FTPFS) RelPath(basePath, targetPath string) (string, error)
- func (f *FTPFS) Stat(p string) (FileInfo, error)
- func (f *FTPFS) Type() BackendType
- type File
- type FileCache
- type FileInfo
- type FileSystem
- type LocalFS
- func (l *LocalFS) BaseURL() string
- func (l *LocalFS) Close() error
- func (l *LocalFS) Exists(path string) (bool, error)
- func (l *LocalFS) IsDir(path string) (bool, error)
- func (l *LocalFS) IsRemote() bool
- func (l *LocalFS) JoinPath(elem ...string) string
- func (l *LocalFS) Open(path string) (File, error)
- func (l *LocalFS) OpenReaderAtSeeker(path string) (ReaderAtSeeker, error)
- func (l *LocalFS) ReadDir(path string) ([]DirEntry, error)
- func (l *LocalFS) ReadFile(path string) ([]byte, error)
- func (l *LocalFS) RelPath(basePath, targetPath string) (string, error)
- func (l *LocalFS) Stat(path string) (FileInfo, error)
- func (l *LocalFS) Type() BackendType
- type Options
- type RangeReaderAtSeeker
- type ReaderAtSeeker
- type S3FS
- func (s *S3FS) BaseURL() string
- func (s *S3FS) Close() error
- func (s *S3FS) Exists(p string) (bool, error)
- func (s *S3FS) GetBasePath() string
- func (s *S3FS) IsDir(p string) (bool, error)
- func (s *S3FS) IsRemote() bool
- func (s *S3FS) JoinPath(elem ...string) string
- func (s *S3FS) Open(p string) (File, error)
- func (s *S3FS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
- func (s *S3FS) ReadDir(p string) ([]DirEntry, error)
- func (s *S3FS) ReadFile(p string) ([]byte, error)
- func (s *S3FS) RelPath(basePath, targetPath string) (string, error)
- func (s *S3FS) Stat(p string) (FileInfo, error)
- func (s *S3FS) Type() BackendType
- type SFTPFS
- func (s *SFTPFS) BaseURL() string
- func (s *SFTPFS) Close() error
- func (s *SFTPFS) Exists(p string) (bool, error)
- func (s *SFTPFS) GetBasePath() string
- func (s *SFTPFS) IsDir(p string) (bool, error)
- func (s *SFTPFS) IsRemote() bool
- func (s *SFTPFS) JoinPath(elem ...string) string
- func (s *SFTPFS) Open(p string) (File, error)
- func (s *SFTPFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
- func (s *SFTPFS) ReadDir(p string) ([]DirEntry, error)
- func (s *SFTPFS) ReadFile(p string) ([]byte, error)
- func (s *SFTPFS) RelPath(basePath, targetPath string) (string, error)
- func (s *SFTPFS) Stat(p string) (FileInfo, error)
- func (s *SFTPFS) Type() BackendType
- type SMBFS
- func (s *SMBFS) BaseURL() string
- func (s *SMBFS) Close() error
- func (s *SMBFS) Exists(p string) (bool, error)
- func (s *SMBFS) GetBasePath() string
- func (s *SMBFS) IsDir(p string) (bool, error)
- func (s *SMBFS) IsRemote() bool
- func (s *SMBFS) JoinPath(elem ...string) string
- func (s *SMBFS) Open(p string) (File, error)
- func (s *SMBFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
- func (s *SMBFS) ReadDir(p string) ([]DirEntry, error)
- func (s *SMBFS) ReadFile(p string) ([]byte, error)
- func (s *SMBFS) RelPath(basePath, targetPath string) (string, error)
- func (s *SMBFS) Stat(p string) (FileInfo, error)
- func (s *SMBFS) Type() BackendType
- type WebDAVFS
- func (w *WebDAVFS) BaseURL() string
- func (w *WebDAVFS) Close() error
- func (w *WebDAVFS) Exists(p string) (bool, error)
- func (w *WebDAVFS) GetBasePath() string
- func (w *WebDAVFS) IsDir(p string) (bool, error)
- func (w *WebDAVFS) IsRemote() bool
- func (w *WebDAVFS) JoinPath(elem ...string) string
- func (w *WebDAVFS) Open(p string) (File, error)
- func (w *WebDAVFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
- func (w *WebDAVFS) ReadDir(p string) ([]DirEntry, error)
- func (w *WebDAVFS) ReadFile(p string) ([]byte, error)
- func (w *WebDAVFS) RelPath(basePath, targetPath string) (string, error)
- func (w *WebDAVFS) Stat(p string) (FileInfo, error)
- func (w *WebDAVFS) Type() BackendType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BackendType ¶
type BackendType int
BackendType 文件存储后端类型
const ( LocalDisk BackendType = 1 + iota SMB SFTP WebDAV S3 FTP ComigoRemote )
func GetBackendType ¶
func GetBackendType(storeURL string) BackendType
GetBackendType 获取 URL 对应的后端类型
func (BackendType) String ¶
func (f BackendType) String() string
type FTPFS ¶
type FTPFS struct {
// contains filtered or unexported fields
}
FTPFS FTP 文件系统实现
func NewFTPFS ¶
NewFTPFS 创建 FTP 文件系统实例 urlStr 格式: ftp://user:pass@host:port/path 或 ftps://user:pass@host:port/path
func (*FTPFS) OpenReaderAtSeeker ¶
func (f *FTPFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
OpenReaderAtSeeker 打开文件并返回支持 Seek 的 Reader FTP 不支持随机访问(ReadAt),统一下载到内存并缓存以避免重复下载
type File ¶
type File interface {
io.Reader
io.Closer
io.Seeker
// Stat 获取文件信息
Stat() (FileInfo, error)
// ReadAt 在指定偏移量处读取
ReadAt(p []byte, off int64) (n int, err error)
}
File 文件接口,提供基本的文件读取操作
type FileCache ¶
type FileCache struct {
// contains filtered or unexported fields
}
FileCache 文件缓存管理器 用于缓存远程文件系统读取的文件,减少网络请求
func NewFileCache ¶
NewFileCache 创建文件缓存管理器
func (*FileCache) ClearOlderThan ¶
ClearOlderThan 清除超过指定时间的缓存
type FileSystem ¶
type FileSystem interface {
// Open 打开文件用于读取
Open(path string) (File, error)
// Stat 获取文件或目录的信息
Stat(path string) (FileInfo, error)
// ReadDir 读取目录内容
ReadDir(path string) ([]DirEntry, error)
// ReadFile 读取整个文件内容
ReadFile(path string) ([]byte, error)
// Type 返回文件系统后端类型
Type() BackendType
// BaseURL 返回文件系统的基础URL或路径
BaseURL() string
// Close 关闭文件系统连接(对于远程文件系统)
Close() error
// IsRemote 返回是否为远程文件系统
IsRemote() bool
// JoinPath 连接路径,根据文件系统类型使用正确的分隔符
JoinPath(elem ...string) string
// RelPath 计算相对路径
RelPath(basePath, targetPath string) (string, error)
// Exists 检查路径是否存在
Exists(path string) (bool, error)
// IsDir 检查路径是否为目录
IsDir(path string) (bool, error)
// OpenReaderAtSeeker 打开文件并返回支持 Seek 的 Reader
// 这对于需要随机访问的压缩包格式很重要
// 如果文件系统不支持 Seek,会将数据读取到内存中
// 返回的 Reader 必须实现 io.Reader, io.ReaderAt, io.Seeker
OpenReaderAtSeeker(path string) (ReaderAtSeeker, error)
}
FileSystem 虚拟文件系统接口 提供统一的文件系统操作,支持本地和远程存储
func GetOrCreate ¶
func GetOrCreate(storeURL string, opts ...Options) (FileSystem, error)
GetOrCreate 获取或创建文件系统实例 如果已存在则返回现有实例,否则创建新实例并注册
type LocalFS ¶
type LocalFS struct {
// contains filtered or unexported fields
}
LocalFS 本地文件系统实现
func NewLocalFS ¶
NewLocalFS 创建本地文件系统实例
func (*LocalFS) OpenReaderAtSeeker ¶
func (l *LocalFS) OpenReaderAtSeeker(path string) (ReaderAtSeeker, error)
OpenReaderAtSeeker 打开文件并返回支持 Seek 的 Reader
type Options ¶
type Options struct {
// CacheEnabled 是否启用文件缓存
CacheEnabled bool
// CacheDir 缓存目录路径
CacheDir string
// Timeout 连接超时(秒)
Timeout int
// Debug 是否启用调试日志
Debug bool
// UseRangeRequests 是否使用 HTTP Range 请求进行按需读取(默认 true)
// 启用后,大文件将按需下载片段而不是完整下载
UseRangeRequests bool
}
Options 文件系统配置选项
type RangeReaderAtSeeker ¶
type RangeReaderAtSeeker struct {
// contains filtered or unexported fields
}
RangeReaderAtSeeker 基于 HTTP Range 请求的 ReaderAtSeeker 实现 支持按需下载文件片段,避免下载整个文件
func NewRangeReaderAtSeeker ¶
func NewRangeReaderAtSeeker(client *gowebdav.Client, path string, size int64, debug bool) *RangeReaderAtSeeker
NewRangeReaderAtSeeker 创建新的 RangeReaderAtSeeker
func (*RangeReaderAtSeeker) Read ¶
func (r *RangeReaderAtSeeker) Read(p []byte) (n int, err error)
Read 实现 io.Reader 接口
type ReaderAtSeeker ¶
ReaderAtSeeker 接口,组合了 io.Reader, io.ReaderAt, io.Seeker 用于支持需要随机访问的压缩包格式
type S3FS ¶
type S3FS struct {
// contains filtered or unexported fields
}
S3FS S3 文件系统实现
func (*S3FS) OpenReaderAtSeeker ¶
func (s *S3FS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
OpenReaderAtSeeker 打开文件并返回支持 Seek 的 Reader 下载到内存并缓存,避免重复下载 两种策略: 小文件(<1MB): 直接下载到内存+缓存 大文件: 也下载到内存+缓存(S3 Range 请求虽可行,但为保持简单先用全量下载+缓存,与 SMB 一致)
type SFTPFS ¶
type SFTPFS struct {
// contains filtered or unexported fields
}
SFTPFS SFTP 文件系统实现
func (*SFTPFS) OpenReaderAtSeeker ¶
func (s *SFTPFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
OpenReaderAtSeeker 打开文件并返回支持 Seek 的 Reader SFTP 原生支持随机访问,可以直接使用 SFTP 文件句柄
type SMBFS ¶
type SMBFS struct {
// contains filtered or unexported fields
}
SMBFS SMB 文件系统实现
func NewSMBFS ¶
NewSMBFS 创建 SMB 文件系统实例 urlStr 格式: smb://workgroup;user:password@server/share/folder/books
func (*SMBFS) OpenReaderAtSeeker ¶
func (s *SMBFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
OpenReaderAtSeeker 打开文件并返回支持 Seek 的 Reader SMB 文件句柄支持 Seek,但不一定支持 ReadAt 所有文件都会下载到内存以确保兼容性,并缓存以避免重复下载
type WebDAVFS ¶
type WebDAVFS struct {
// contains filtered or unexported fields
}
WebDAVFS WebDAV 文件系统实现
func NewWebDAVFS ¶
NewWebDAVFS 创建 WebDAV 文件系统实例 urlStr 格式: webdav://user:pass@host:port/path 或 davs://user:pass@host:port/path
func (*WebDAVFS) OpenReaderAtSeeker ¶
func (w *WebDAVFS) OpenReaderAtSeeker(p string) (ReaderAtSeeker, error)
OpenReaderAtSeeker 打开文件并返回支持 Seek 的 Reader 如果启用了 UseRangeRequests,将使用 RangeReaderAtSeeker 进行按需读取 否则将整个文件下载到内存(向后兼容)