fix: improve file listing and path handling in Webdav storage

This commit is contained in:
krau
2026-01-19 20:35:48 +08:00
parent f92c43b9c8
commit 17e340fff1

View File

@@ -90,10 +90,10 @@ func (w *Webdav) Exists(ctx context.Context, storagePath string) bool {
// ListFiles implements storage.StorageListable // ListFiles implements storage.StorageListable
func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error) { func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error) {
w.logger.Infof("Listing files in %s", dirPath) w.logger.Infof("Listing files in %s", dirPath)
// Join with base path // Join with base path
fullPath := path.Join(w.config.BasePath, dirPath) fullPath := path.Join(w.config.BasePath, dirPath)
responses, err := w.client.ListDir(ctx, fullPath) responses, err := w.client.ListDir(ctx, fullPath)
if err != nil { if err != nil {
w.logger.Errorf("Failed to list directory %s: %v", fullPath, err) w.logger.Errorf("Failed to list directory %s: %v", fullPath, err)
@@ -108,7 +108,7 @@ func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.
w.logger.Warnf("Failed to unescape href %q: %v; using original value", resp.Href, err) w.logger.Warnf("Failed to unescape href %q: %v; using original value", resp.Href, err)
decodedHref = resp.Href decodedHref = resp.Href
} }
// Extract filename from href // Extract filename from href
name := path.Base(strings.TrimSuffix(decodedHref, "/")) name := path.Base(strings.TrimSuffix(decodedHref, "/"))
if name == "" || name == "." { if name == "" || name == "." {
@@ -128,15 +128,18 @@ func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.
} }
isDir := resp.Propstat.Prop.ResourceType.IsCollection() isDir := resp.Propstat.Prop.ResourceType.IsCollection()
filePath := strings.TrimPrefix(decodedHref, path.Join("/", strings.Trim(path.Dir(fullPath), "/")))
filePath = strings.TrimPrefix(filePath, "/")
fileInfo := storagetypes.FileInfo{ fileInfo := storagetypes.FileInfo{
Name: name, Name: name,
Path: strings.TrimPrefix(decodedHref, w.config.BasePath), Path: path.Join(dirPath, name),
Size: resp.Propstat.Prop.GetContentLength, Size: resp.Propstat.Prop.GetContentLength,
IsDir: isDir, IsDir: isDir,
ModTime: modTime, ModTime: modTime,
} }
files = append(files, fileInfo) files = append(files, fileInfo)
} }
@@ -147,10 +150,10 @@ func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.
// OpenFile implements storage.StorageReadable // OpenFile implements storage.StorageReadable
func (w *Webdav) OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error) { func (w *Webdav) OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error) {
w.logger.Infof("Opening file %s", filePath) w.logger.Infof("Opening file %s", filePath)
// Join with base path // Join with base path
fullPath := path.Join(w.config.BasePath, filePath) fullPath := path.Join(w.config.BasePath, filePath)
reader, size, err := w.client.ReadFile(ctx, fullPath) reader, size, err := w.client.ReadFile(ctx, fullPath)
if err != nil { if err != nil {
w.logger.Errorf("Failed to open file %s: %v", fullPath, err) w.logger.Errorf("Failed to open file %s: %v", fullPath, err)