191 lines
5.4 KiB
Dart
191 lines
5.4 KiB
Dart
import 'dart:convert';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:xml/xml.dart';
|
|
|
|
class WebDAVService {
|
|
static const String _keyBaseUrl = 'webdav_base_url';
|
|
static const String _keyUsername = 'webdav_username';
|
|
static const String _keyPassword = 'webdav_password';
|
|
|
|
static WebDAVService? _instance;
|
|
static WebDAVService get instance => _instance ??= WebDAVService._();
|
|
|
|
WebDAVService._();
|
|
|
|
Dio? _dio;
|
|
String? _baseUrl;
|
|
String? _username;
|
|
|
|
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();
|
|
await prefs.setString(_keyBaseUrl, baseUrl);
|
|
await prefs.setString(_keyUsername, username);
|
|
await prefs.setString(_keyPassword, password);
|
|
|
|
_baseUrl = baseUrl;
|
|
_username = username;
|
|
_dio = Dio(BaseOptions(
|
|
baseUrl: baseUrl,
|
|
headers: {
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode('$username:$password'))}',
|
|
},
|
|
));
|
|
}
|
|
|
|
// 加载已保存的凭据
|
|
Future<bool> loadCredentials() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final baseUrl = prefs.getString(_keyBaseUrl);
|
|
final username = prefs.getString(_keyUsername);
|
|
final password = prefs.getString(_keyPassword);
|
|
|
|
if (baseUrl != null && username != null && password != null) {
|
|
_baseUrl = baseUrl;
|
|
_username = username;
|
|
_dio = Dio(BaseOptions(
|
|
baseUrl: baseUrl,
|
|
headers: {
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode('$username:$password'))}',
|
|
},
|
|
));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 清除凭据
|
|
Future<void> clearCredentials() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(_keyBaseUrl);
|
|
await prefs.remove(_keyUsername);
|
|
await prefs.remove(_keyPassword);
|
|
_dio = null;
|
|
_baseUrl = null;
|
|
_username = null;
|
|
}
|
|
|
|
// 获取音乐文件列表(通过 PROPFIND)
|
|
Future<List<WebDAVFileItem>> getMusicFiles({String path = '/'}) async {
|
|
if (_dio == null) throw Exception('WebDAV 未连接');
|
|
|
|
// PROPFIND 请求体(Depth: 1 表示获取子项)
|
|
final body = '''
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<propfind xmlns="DAV:">
|
|
<prop>
|
|
<resourcetype/>
|
|
<getcontentlength/>
|
|
<getlastmodified/>
|
|
</prop>
|
|
</propfind>
|
|
''';
|
|
|
|
final response = await _dio!.request(
|
|
path,
|
|
options: Options(
|
|
method: 'PROPFIND',
|
|
headers: {
|
|
'Depth': '1',
|
|
'Content-Type': 'application/xml; charset=utf-8',
|
|
},
|
|
),
|
|
data: body,
|
|
);
|
|
|
|
if (response.statusCode != 207) {
|
|
throw Exception('WebDAV 响应异常: ${response.statusCode}');
|
|
}
|
|
|
|
final xml = XmlDocument.parse(response.data as String);
|
|
final musicExtensions = ['.mp3', '.flac', '.m4a', '.ape', '.wav', '.opus'];
|
|
|
|
final items = <WebDAVFileItem>[];
|
|
|
|
// 查找所有响应项
|
|
for (final responseNode in xml.findAllElements('response')) {
|
|
final hrefNode = responseNode.findElements('href').firstOrNull;
|
|
if (hrefNode == null) continue;
|
|
|
|
String fullPath = Uri.decodeComponent(hrefNode.text.trim());
|
|
// 去掉 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);
|
|
}
|
|
}
|
|
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)))
|
|
continue;
|
|
|
|
// 获取文件大小和修改时间
|
|
int? size;
|
|
DateTime? modified;
|
|
final sizeNode =
|
|
responseNode.findElements('getcontentlength').firstOrNull;
|
|
if (sizeNode != null) {
|
|
size = int.tryParse(sizeNode.text);
|
|
}
|
|
final modifiedNode =
|
|
responseNode.findElements('getlastmodified').firstOrNull;
|
|
if (modifiedNode != null) {
|
|
try {
|
|
modified = DateTime.parse(modifiedNode.text);
|
|
} catch (_) {}
|
|
}
|
|
|
|
items.add(WebDAVFileItem(
|
|
path: relativePath,
|
|
name: fileName,
|
|
size: size,
|
|
modified: modified,
|
|
));
|
|
}
|
|
|
|
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';
|
|
}
|
|
}
|
|
|
|
class WebDAVFileItem {
|
|
final String path;
|
|
final String name;
|
|
final int? size;
|
|
final DateTime? modified;
|
|
|
|
WebDAVFileItem({
|
|
required this.path,
|
|
required this.name,
|
|
this.size,
|
|
this.modified,
|
|
});
|
|
}
|