- 添加管理员端 API,用于获取和更新完整配置。 - 添加公共端 API,用于获取非敏感配置信息。 - 增加本地存储(LocalStorage)、S3(S3Storage)、和 WebDAV(WebDAVStorage)存储类型的实现。 - 支持热更新存储配置和保存配置文件至磁盘。 - 更新 Swagger 文档以包含新接口定义。
48 lines
1013 B
Go
48 lines
1013 B
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type LocalStorage struct {
|
|
RootPath string
|
|
}
|
|
|
|
func NewLocalStorage(rootPath string) *LocalStorage {
|
|
// 确保根目录存在
|
|
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
|
|
os.MkdirAll(rootPath, 0755)
|
|
}
|
|
return &LocalStorage{RootPath: rootPath}
|
|
}
|
|
|
|
func (s *LocalStorage) Save(ctx context.Context, path string, reader io.Reader) error {
|
|
fullPath := filepath.Join(s.RootPath, path)
|
|
dir := filepath.Dir(fullPath)
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
os.MkdirAll(dir, 0755)
|
|
}
|
|
|
|
file, err := os.Create(fullPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(file, reader)
|
|
return err
|
|
}
|
|
|
|
func (s *LocalStorage) Open(ctx context.Context, path string) (io.ReadCloser, error) {
|
|
fullPath := filepath.Join(s.RootPath, path)
|
|
return os.Open(fullPath)
|
|
}
|
|
|
|
func (s *LocalStorage) Delete(ctx context.Context, path string) error {
|
|
fullPath := filepath.Join(s.RootPath, path)
|
|
return os.Remove(fullPath)
|
|
}
|