界面部分阶段性达成效果
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
// ============================================================
|
||||
// 文件名: webdav_service.dart
|
||||
// 功能: WebDAV 协议通信服务,支持文件夹浏览和音乐文件读取
|
||||
// ============================================================
|
||||
|
||||
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';
|
||||
@@ -17,11 +25,16 @@ 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();
|
||||
@@ -40,7 +53,6 @@ class WebDAVService {
|
||||
));
|
||||
}
|
||||
|
||||
// 加载已保存的凭据
|
||||
Future<bool> loadCredentials() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final baseUrl = prefs.getString(_keyBaseUrl);
|
||||
@@ -62,7 +74,6 @@ class WebDAVService {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 清除凭据
|
||||
Future<void> clearCredentials() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_keyBaseUrl);
|
||||
@@ -73,11 +84,15 @@ class WebDAVService {
|
||||
_username = null;
|
||||
}
|
||||
|
||||
// 获取音乐文件列表(通过 PROPFIND)
|
||||
Future<List<WebDAVFileItem>> getMusicFiles({String path = '/'}) async {
|
||||
// -------------------------------------------------------------
|
||||
// 核心:列出目录内容(文件夹 + 音乐文件)
|
||||
// 这是浏览模式的核心方法,支持进入子目录
|
||||
// -------------------------------------------------------------
|
||||
Future<List<WebDAVItem>> listDirectory({String path = '/'}) async {
|
||||
if (_dio == null) throw Exception('WebDAV 未连接');
|
||||
|
||||
// PROPFIND 请求体(Depth: 1 表示获取子项)
|
||||
final requestPath = path.startsWith('/') ? path : '/$path';
|
||||
|
||||
final body = '''
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
@@ -87,10 +102,10 @@ class WebDAVService {
|
||||
<getlastmodified/>
|
||||
</prop>
|
||||
</propfind>
|
||||
''';
|
||||
''';
|
||||
|
||||
final response = await _dio!.request(
|
||||
path,
|
||||
requestPath,
|
||||
options: Options(
|
||||
method: 'PROPFIND',
|
||||
headers: {
|
||||
@@ -106,67 +121,163 @@ class WebDAVService {
|
||||
}
|
||||
|
||||
final xml = XmlDocument.parse(response.data as String);
|
||||
final items = <WebDAVItem>[];
|
||||
final musicExtensions = ['.mp3', '.flac', '.m4a', '.ape', '.wav', '.opus'];
|
||||
|
||||
final items = <WebDAVFileItem>[];
|
||||
|
||||
// 查找所有响应项
|
||||
for (final responseNode in xml.findAllElements('response')) {
|
||||
final hrefNode = responseNode.findElements('href').firstOrNull;
|
||||
final responseNodes = xml.findAllElements('D:response');
|
||||
for (final responseNode in responseNodes) {
|
||||
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) {
|
||||
final baseUri = Uri.parse(_baseUrl!);
|
||||
final fullUri = Uri.parse(fullPath);
|
||||
relativePath = fullUri.path;
|
||||
if (baseUri.path.isNotEmpty && relativePath.startsWith(baseUri.path)) {
|
||||
relativePath = relativePath.substring(baseUri.path.length);
|
||||
try {
|
||||
final baseUri = Uri.parse(_baseUrl!);
|
||||
final fullUri = Uri.parse(fullPath);
|
||||
String fullPathOnly = fullUri.path;
|
||||
if (baseUri.path.isNotEmpty &&
|
||||
fullPathOnly.startsWith(baseUri.path)) {
|
||||
relativePath = fullPathOnly.substring(baseUri.path.length);
|
||||
} else {
|
||||
relativePath = fullPathOnly;
|
||||
}
|
||||
} catch (_) {
|
||||
relativePath = fullPath;
|
||||
}
|
||||
}
|
||||
if (relativePath.isEmpty || relativePath == '/') continue;
|
||||
|
||||
// 检查是否是文件(非集合)
|
||||
final resTypeNode = responseNode.findElements('resourcetype').firstOrNull;
|
||||
final isCollection =
|
||||
resTypeNode?.findElements('collection').isNotEmpty ?? false;
|
||||
if (isCollection) continue;
|
||||
|
||||
// 检查扩展名
|
||||
final fileName = relativePath.split('/').last;
|
||||
if (!musicExtensions.any((ext) => fileName.toLowerCase().endsWith(ext)))
|
||||
// 跳过根目录自身
|
||||
if (relativePath.isEmpty ||
|
||||
relativePath == '/' ||
|
||||
relativePath == requestPath) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final cleanPath = relativePath.endsWith('/')
|
||||
? relativePath.substring(0, relativePath.length - 1)
|
||||
: relativePath;
|
||||
|
||||
// ✅ 解码文件名
|
||||
String fileName = cleanPath.split('/').last;
|
||||
fileName = Uri.decodeComponent(fileName);
|
||||
if (fileName.isEmpty) continue;
|
||||
|
||||
// 判断是否为目录
|
||||
bool isDirectory = false;
|
||||
final resTypeNode =
|
||||
responseNode.findElements('D:resourcetype').firstOrNull;
|
||||
if (resTypeNode != null) {
|
||||
final collection = resTypeNode.findElements('D:collection').firstOrNull;
|
||||
if (collection != null) {
|
||||
isDirectory = true;
|
||||
}
|
||||
}
|
||||
if (!isDirectory && fullPath.endsWith('/')) {
|
||||
isDirectory = true;
|
||||
}
|
||||
|
||||
if (isDirectory) {
|
||||
items.add(WebDAVItem(
|
||||
path: cleanPath,
|
||||
name: fileName,
|
||||
isDirectory: true,
|
||||
size: null,
|
||||
modified: null,
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
// 只保留音乐文件
|
||||
if (!musicExtensions.any((ext) => fileName.toLowerCase().endsWith(ext))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取文件大小和修改时间
|
||||
int? size;
|
||||
DateTime? modified;
|
||||
final sizeNode =
|
||||
responseNode.findElements('getcontentlength').firstOrNull;
|
||||
if (sizeNode != null) {
|
||||
responseNode.findElements('D:getcontentlength').firstOrNull;
|
||||
if (sizeNode != null && sizeNode.text.isNotEmpty) {
|
||||
size = int.tryParse(sizeNode.text);
|
||||
}
|
||||
final modifiedNode =
|
||||
responseNode.findElements('getlastmodified').firstOrNull;
|
||||
responseNode.findElements('D:getlastmodified').firstOrNull;
|
||||
if (modifiedNode != null) {
|
||||
try {
|
||||
modified = DateTime.parse(modifiedNode.text);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
items.add(WebDAVFileItem(
|
||||
path: relativePath,
|
||||
items.add(WebDAVItem(
|
||||
path: cleanPath,
|
||||
name: fileName,
|
||||
isDirectory: false,
|
||||
size: size,
|
||||
modified: modified,
|
||||
));
|
||||
}
|
||||
|
||||
// 排序
|
||||
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
|
||||
// -------------------------------------------------------------
|
||||
// 兼容旧接口:获取所有音乐文件(递归扫描)
|
||||
// 用于主页的"我的收藏"列表(后续可改为读取用户收藏)
|
||||
// -------------------------------------------------------------
|
||||
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'];
|
||||
|
||||
return allItems
|
||||
.where((item) =>
|
||||
!item.isDirectory &&
|
||||
musicExtensions.any((ext) => item.name.toLowerCase().endsWith(ext)))
|
||||
.map((item) => WebDAVFileItem(
|
||||
path: item.path,
|
||||
name: item.name,
|
||||
size: item.size,
|
||||
modified: item.modified,
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// 递归获取所有目录和文件(内部使用)
|
||||
Future<List<WebDAVItem>> _listAllRecursive(String path) async {
|
||||
final items = await listDirectory(path: path);
|
||||
final result = <WebDAVItem>[];
|
||||
|
||||
for (final item in items) {
|
||||
result.add(item);
|
||||
if (item.isDirectory) {
|
||||
// 递归获取子目录内容
|
||||
try {
|
||||
final subItems = await _listAllRecursive(item.path);
|
||||
result.addAll(subItems);
|
||||
} catch (_) {
|
||||
// 忽略无法读取的子目录
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 获取文件的完整下载 URL
|
||||
// -------------------------------------------------------------
|
||||
String getFileUrl(String path) {
|
||||
if (_baseUrl == null) throw Exception('WebDAV 未配置');
|
||||
final base = _baseUrl!.endsWith('/') ? _baseUrl! : '$_baseUrl/';
|
||||
@@ -175,6 +286,28 @@ class WebDAVService {
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 数据模型:WebDAV 目录项(用于浏览模式)
|
||||
// -----------------------------------------------------------------
|
||||
class WebDAVItem {
|
||||
final String path;
|
||||
final String name;
|
||||
final bool isDirectory;
|
||||
final int? size;
|
||||
final DateTime? modified;
|
||||
|
||||
WebDAVItem({
|
||||
required this.path,
|
||||
required this.name,
|
||||
required this.isDirectory,
|
||||
this.size,
|
||||
this.modified,
|
||||
});
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 数据模型:WebDAV 文件项(用于播放列表)
|
||||
// -----------------------------------------------------------------
|
||||
class WebDAVFileItem {
|
||||
final String path;
|
||||
final String name;
|
||||
|
||||
Reference in New Issue
Block a user