part of '../../nextcloud.dart'; /// WebDavClient class class WebDavClient { // ignore: public_member_api_docs WebDavClient( this.rootClient, this.basePath, ); // ignore: public_member_api_docs final NextcloudClient rootClient; /// Base path used on the server final String basePath; Future _send( final String method, final String url, final List expectedCodes, { final Stream? data, final Map? headers, }) async { final request = await HttpClient().openUrl( method, Uri.parse(url), ) ..followRedirects = false ..persistentConnection = true; for (final header in { HttpHeaders.contentTypeHeader: 'application/xml', ...rootClient.baseHeaders, if (headers != null) ...headers, if (rootClient.authentications.isNotEmpty) ...rootClient.authentications.first.headers, }.entries) { request.headers.add(header.key, header.value); } if (data != null) { await request.addStream(data); } final response = await request.close(); if (!expectedCodes.contains(response.statusCode)) { throw NextcloudApiException( response.statusCode, {}, // TODO await response.bodyBytes, ); } return response; } String _constructPath([final String? path]) => [ rootClient.baseURL, basePath, if (path != null) ...[ path, ], ] .map((part) { while (part.startsWith('/')) { part = part.substring(1); } while (part.endsWith('/')) { part = part.substring(0, part.length - 1); } return part; }) .where((final part) => part.isNotEmpty) .join('/'); /// returns the WebDAV capabilities of the server Future status() async { final response = await _send( 'OPTIONS', _constructPath(), [200], ); final davCapabilities = response.headers['dav']?.cast().first ?? ''; final davSearchCapabilities = response.headers['dasl']?.cast().first ?? ''; return WebDavStatus( davCapabilities.split(',').map((final e) => e.trim()).where((final e) => e.isNotEmpty).toSet(), davSearchCapabilities.split(',').map((final e) => e.trim()).where((final e) => e.isNotEmpty).toSet(), ); } /// make a dir with [remotePath] under current dir Future mkdir( final String remotePath, { final bool safe = true, }) async { final expectedCodes = [ 201, if (safe) ...[ 301, 405, ], ]; return _send( 'MKCOL', _constructPath(remotePath), expectedCodes, ); } /// just like mkdir -p Future mkdirs( final String remotePath, { final bool safe = true, }) async { final dirs = remotePath.trim().split('/')..removeWhere((final value) => value == ''); if (dirs.isEmpty) { return null; } if (remotePath.trim().startsWith('/')) { dirs[0] = '/${dirs[0]}'; } final prevPath = StringBuffer(); late HttpClientResponse response; for (final dir in dirs) { response = await mkdir( '$prevPath/$dir', safe: safe, ); prevPath.write('/$dir'); } return response; } /// remove dir with given [path] Future delete(final String path) => _send( 'DELETE', _constructPath(path), [204], ); Map? _generateUploadHeaders({ required final DateTime? lastModified, required final DateTime? created, }) { final headers = { if (lastModified != null) ...{ 'X-OC-Mtime': (lastModified.millisecondsSinceEpoch ~/ 1000).toString(), }, if (created != null) ...{ 'X-OC-CTime': (created.millisecondsSinceEpoch ~/ 1000).toString(), }, }; return headers.isNotEmpty ? headers : null; } /// upload a new file with [localData] as content to [remotePath] Future upload( final Uint8List localData, final String remotePath, { final DateTime? lastModified, final DateTime? created, }) => _send( 'PUT', _constructPath(remotePath), [200, 201, 204], data: Stream.value(localData), headers: _generateUploadHeaders( lastModified: lastModified, created: created, ), ); /// upload a new file with [localData] as content to [remotePath] Future uploadStream( final Stream localData, final String remotePath, { final DateTime? lastModified, final DateTime? created, }) async => _send( 'PUT', _constructPath(remotePath), [200, 201, 204], data: localData, headers: _generateUploadHeaders( lastModified: lastModified, created: created, ), ); /// download [remotePath] and store the response file contents to String Future download(final String remotePath) async => Uint8List.fromList( (await (await _send( 'GET', _constructPath(remotePath), [200], )) .join()) .codeUnits, ); /// download [remotePath] and store the response file contents to ByteStream Future downloadStream(final String remotePath) async => _send( 'GET', _constructPath(remotePath), [200], ); Future _parseResponse(final HttpClientResponse response) async => WebDavMultistatus.fromXmlElement(xml.XmlDocument.parse(await response.body).rootElement); /// list the directories and files under given [remotePath]. /// /// Optionally populates the given [prop]s on the returned files. Future ls( final String remotePath, { final WebDavPropfindProp? prop, final int? depth, }) async { final response = await _send( 'PROPFIND', _constructPath(remotePath), [207, 301], data: Stream.value( Uint8List.fromList( utf8.encode( WebDavPropfind(prop: prop ?? WebDavPropfindProp()).toXmlElement(namespaces: namespaces).toXmlString(), ), ), ), headers: { if (depth != null) ...{ 'Depth': depth.toString(), }, }, ); if (response.statusCode == 301) { return ls( response.headers['location']!.first, prop: prop, depth: depth, ); } return _parseResponse(response); } /// Runs the filter-files report with the given [filterRules] on the /// [remotePath]. /// /// Optionally populates the given [prop]s on the returned files. Future filter( final String remotePath, final WebDavOcFilterRules filterRules, { final WebDavPropfindProp? prop, }) async { final response = await _send( 'REPORT', _constructPath(remotePath), [200, 207], data: Stream.value( Uint8List.fromList( utf8.encode( WebDavOcFilterFiles( filterRules: filterRules, prop: prop ?? WebDavPropfindProp(), ).toXmlElement(namespaces: namespaces).toXmlString(), ), ), ), ); return _parseResponse(response); } /// Update (string) properties of the given [remotePath]. /// /// Returns true if the update was successful. Future updateProps( final String remotePath, final WebDavProp prop, ) async { final response = await _send( 'PROPPATCH', _constructPath(remotePath), [200, 207], data: Stream.value( Uint8List.fromList( utf8.encode( WebDavPropertyupdate(set: WebDavSet(prop: prop)).toXmlElement(namespaces: namespaces).toXmlString(), ), ), ), ); final data = await _parseResponse(response); for (final a in data.responses) { for (final b in a.propstats) { if (!b.status.contains('200')) { return false; } } } return true; } /// Move a file from [sourcePath] to [destinationPath] Future move( final String sourcePath, final String destinationPath, { final bool overwrite = false, }) => _send( 'MOVE', _constructPath(sourcePath), [200, 201, 204], headers: { 'Destination': _constructPath(destinationPath), 'Overwrite': overwrite ? 'T' : 'F', }, ); /// Copy a file from [sourcePath] to [destinationPath] Future copy( final String sourcePath, final String destinationPath, { final bool overwrite = false, }) => _send( 'COPY', _constructPath(sourcePath), [200, 201, 204], headers: { 'Destination': _constructPath(destinationPath), 'Overwrite': overwrite ? 'T' : 'F', }, ); } /// WebDAV server status. class WebDavStatus { /// Creates a new WebDavStatus. WebDavStatus( this.capabilities, this.searchCapabilities, ); /// DAV capabilities as advertised by the server in the 'dav' header. Set capabilities; /// DAV search and locating capabilities as advertised by the server in the /// 'dasl' header. Set searchCapabilities; }