webdav存在自引用问题,现版本修复完成
This commit is contained in:
@@ -1,16 +1,9 @@
|
||||
// ============================================================
|
||||
// 文件名: webdav_service.dart
|
||||
// 功能: WebDAV 协议通信服务,支持文件夹浏览和音乐文件读取
|
||||
// ============================================================
|
||||
|
||||
// lib/services/webdav_service.dart
|
||||
import 'dart:convert';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:xml/xml.dart';
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// WebDAV 服务单例
|
||||
// -----------------------------------------------------------------
|
||||
class WebDAVService {
|
||||
static const String _keyBaseUrl = 'webdav_base_url';
|
||||
static const String _keyUsername = 'webdav_username';
|
||||
@@ -25,16 +18,11 @@ class WebDAVService {
|
||||
String? _baseUrl;
|
||||
String? _username;
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 公开 Getter
|
||||
// -------------------------------------------------------------
|
||||
bool get isConnected => _dio != null;
|
||||
String? get baseUrl => _baseUrl;
|
||||
String? get username => _username;
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 凭据管理
|
||||
// -------------------------------------------------------------
|
||||
// 保存凭据
|
||||
Future<void> saveCredentials(
|
||||
String baseUrl, String username, String password) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
@@ -53,6 +41,7 @@ class WebDAVService {
|
||||
));
|
||||
}
|
||||
|
||||
// 加载已保存的凭据
|
||||
Future<bool> loadCredentials() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final baseUrl = prefs.getString(_keyBaseUrl);
|
||||
@@ -74,6 +63,7 @@ class WebDAVService {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 清除凭据
|
||||
Future<void> clearCredentials() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_keyBaseUrl);
|
||||
@@ -84,15 +74,22 @@ class WebDAVService {
|
||||
_username = null;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 核心:列出目录内容(文件夹 + 音乐文件)
|
||||
// 这是浏览模式的核心方法,支持进入子目录
|
||||
// -------------------------------------------------------------
|
||||
// 获取音乐文件列表(PROPFIND)
|
||||
Future<List<WebDAVItem>> listDirectory({String path = '/'}) async {
|
||||
if (_dio == null) throw Exception('WebDAV 未连接');
|
||||
|
||||
final requestPath = path.startsWith('/') ? path : '/$path';
|
||||
|
||||
// 路径规范化:去掉首尾斜杠,用于比较
|
||||
String _normalize(String p) {
|
||||
var s = p;
|
||||
if (s.startsWith('/')) s = s.substring(1);
|
||||
if (s.endsWith('/')) s = s.substring(0, s.length - 1);
|
||||
return s;
|
||||
}
|
||||
|
||||
final normalizedRequest = _normalize(requestPath);
|
||||
|
||||
final body = '''
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
@@ -124,14 +121,13 @@ class WebDAVService {
|
||||
final items = <WebDAVItem>[];
|
||||
final musicExtensions = ['.mp3', '.flac', '.m4a', '.ape', '.wav', '.opus'];
|
||||
|
||||
final responseNodes = xml.findAllElements('D:response');
|
||||
for (final responseNode in responseNodes) {
|
||||
for (final responseNode in xml.findAllElements('D:response')) {
|
||||
final hrefNode = responseNode.findElements('D:href').firstOrNull;
|
||||
if (hrefNode == null) continue;
|
||||
|
||||
String fullPath = Uri.decodeComponent(hrefNode.text.trim());
|
||||
|
||||
// 去掉 baseUrl 前缀
|
||||
// 去掉 baseUrl 前缀,得到相对路径
|
||||
String relativePath = fullPath;
|
||||
if (_baseUrl != null) {
|
||||
try {
|
||||
@@ -149,10 +145,10 @@ class WebDAVService {
|
||||
}
|
||||
}
|
||||
|
||||
// 跳过根目录自身
|
||||
if (relativePath.isEmpty ||
|
||||
relativePath == '/' ||
|
||||
relativePath == requestPath) {
|
||||
// 使用规范化路径比较,跳过当前目录自身
|
||||
final normalizedRelative = _normalize(relativePath);
|
||||
if (normalizedRelative.isEmpty ||
|
||||
normalizedRelative == normalizedRequest) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -160,7 +156,6 @@ class WebDAVService {
|
||||
? relativePath.substring(0, relativePath.length - 1)
|
||||
: relativePath;
|
||||
|
||||
// ✅ 解码文件名
|
||||
String fileName = cleanPath.split('/').last;
|
||||
fileName = Uri.decodeComponent(fileName);
|
||||
if (fileName.isEmpty) continue;
|
||||
@@ -219,26 +214,28 @@ class WebDAVService {
|
||||
));
|
||||
}
|
||||
|
||||
// 排序
|
||||
// 排序:目录在前,文件在后
|
||||
items.sort((a, b) {
|
||||
if (a.isDirectory && !b.isDirectory) return -1;
|
||||
if (!a.isDirectory && b.isDirectory) return 1;
|
||||
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
|
||||
});
|
||||
|
||||
// 只保留一条日志,避免刷屏
|
||||
print('WebDAV listDirectory: 找到 ${items.length} 项,路径: $requestPath');
|
||||
return items;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 兼容旧接口:获取所有音乐文件(递归扫描)
|
||||
// 用于主页的"我的收藏"列表(后续可改为读取用户收藏)
|
||||
// -------------------------------------------------------------
|
||||
// 获取文件完整 URL
|
||||
String getFileUrl(String path) {
|
||||
if (_baseUrl == null) throw Exception('WebDAV 未配置');
|
||||
final base = _baseUrl!.endsWith('/') ? _baseUrl! : '$_baseUrl/';
|
||||
final cleanPath = path.startsWith('/') ? path.substring(1) : path;
|
||||
return '$base$cleanPath';
|
||||
}
|
||||
|
||||
// 递归获取所有音乐文件(用于收藏列表)
|
||||
Future<List<WebDAVFileItem>> getMusicFiles({String path = '/'}) async {
|
||||
if (_dio == null) throw Exception('WebDAV 未连接');
|
||||
|
||||
// 递归获取所有文件(先获取当前目录,再递归子目录)
|
||||
final allItems = await _listAllRecursive(path);
|
||||
final musicExtensions = ['.mp3', '.flac', '.m4a', '.ape', '.wav', '.opus'];
|
||||
|
||||
@@ -255,7 +252,6 @@ class WebDAVService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
// 递归获取所有目录和文件(内部使用)
|
||||
Future<List<WebDAVItem>> _listAllRecursive(String path) async {
|
||||
final items = await listDirectory(path: path);
|
||||
final result = <WebDAVItem>[];
|
||||
@@ -263,32 +259,16 @@ class WebDAVService {
|
||||
for (final item in items) {
|
||||
result.add(item);
|
||||
if (item.isDirectory) {
|
||||
// 递归获取子目录内容
|
||||
try {
|
||||
final subItems = await _listAllRecursive(item.path);
|
||||
result.addAll(subItems);
|
||||
} catch (_) {
|
||||
// 忽略无法读取的子目录
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 获取文件的完整下载 URL
|
||||
// -------------------------------------------------------------
|
||||
String getFileUrl(String path) {
|
||||
if (_baseUrl == null) throw Exception('WebDAV 未配置');
|
||||
final base = _baseUrl!.endsWith('/') ? _baseUrl! : '$_baseUrl/';
|
||||
final cleanPath = path.startsWith('/') ? path.substring(1) : path;
|
||||
return '$base$cleanPath';
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 数据模型:WebDAV 目录项(用于浏览模式)
|
||||
// -----------------------------------------------------------------
|
||||
class WebDAVItem {
|
||||
final String path;
|
||||
final String name;
|
||||
@@ -305,9 +285,6 @@ class WebDAVItem {
|
||||
});
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 数据模型:WebDAV 文件项(用于播放列表)
|
||||
// -----------------------------------------------------------------
|
||||
class WebDAVFileItem {
|
||||
final String path;
|
||||
final String name;
|
||||
|
||||
Reference in New Issue
Block a user