新增配置管理功能及多存储支持
- 添加管理员端 API,用于获取和更新完整配置。 - 添加公共端 API,用于获取非敏感配置信息。 - 增加本地存储(LocalStorage)、S3(S3Storage)、和 WebDAV(WebDAVStorage)存储类型的实现。 - 支持热更新存储配置和保存配置文件至磁盘。 - 更新 Swagger 文档以包含新接口定义。
This commit is contained in:
54
internal/storage/webdav.go
Normal file
54
internal/storage/webdav.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/studio-b12/gowebdav"
|
||||
)
|
||||
|
||||
type WebDAVStorage struct {
|
||||
client *gowebdav.Client
|
||||
root string
|
||||
}
|
||||
|
||||
func NewWebDAVStorage(url, username, password, root string) *WebDAVStorage {
|
||||
client := gowebdav.NewClient(url, username, password)
|
||||
return &WebDAVStorage{
|
||||
client: client,
|
||||
root: root,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WebDAVStorage) getFullPath(path string) string {
|
||||
return filepath.ToSlash(filepath.Join(s.root, path))
|
||||
}
|
||||
|
||||
func (s *WebDAVStorage) Save(ctx context.Context, path string, reader io.Reader) error {
|
||||
fullPath := s.getFullPath(path)
|
||||
|
||||
// Ensure directory exists
|
||||
dir := filepath.Dir(fullPath)
|
||||
if dir != "." && dir != "/" {
|
||||
parts := strings.Split(strings.Trim(dir, "/"), "/")
|
||||
current := ""
|
||||
for _, part := range parts {
|
||||
current += "/" + part
|
||||
_ = s.client.Mkdir(current, 0755)
|
||||
}
|
||||
}
|
||||
|
||||
return s.client.WriteStream(fullPath, reader, 0644)
|
||||
}
|
||||
|
||||
func (s *WebDAVStorage) Open(ctx context.Context, path string) (io.ReadCloser, error) {
|
||||
fullPath := s.getFullPath(path)
|
||||
return s.client.ReadStream(fullPath)
|
||||
}
|
||||
|
||||
func (s *WebDAVStorage) Delete(ctx context.Context, path string) error {
|
||||
fullPath := s.getFullPath(path)
|
||||
return s.client.Remove(fullPath)
|
||||
}
|
||||
Reference in New Issue
Block a user