From a37e583598c5f931c95f349db107b3305b4313f3 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sun, 30 Jul 2023 15:44:23 +0200 Subject: [PATCH] refactor(nextcloud): Use WebDAV endpoint that does not require the username --- packages/nextcloud/lib/src/client.dart | 22 ++++--------------- packages/nextcloud/lib/src/webdav/client.dart | 13 +++++------ packages/nextcloud/lib/src/webdav/file.dart | 14 +++--------- packages/nextcloud/test/webdav_test.dart | 10 +-------- 4 files changed, 13 insertions(+), 46 deletions(-) diff --git a/packages/nextcloud/lib/src/client.dart b/packages/nextcloud/lib/src/client.dart index c392e613..009b9bb9 100644 --- a/packages/nextcloud/lib/src/client.dart +++ b/packages/nextcloud/lib/src/client.dart @@ -44,27 +44,10 @@ class NextcloudClient extends DynamiteClient { /// Identifier used for authentication. This can be the username or email or something else. final String? loginName; - /// Username of the user on the server, it needs to be set for using WebDAV. - /// It can be obtained via the + /// Username of the user on the server final String? username; WebDavClient? _webdav; - - /// Client for WebDAV. Username needs to be set in order to use it - WebDavClient get webdav { - if (_webdav != null) { - return _webdav!; - } - if (username == null) { - throw Exception('The WebDAV client is only available when a username is set'); - } - - return _webdav = WebDavClient( - this, - '/remote.php/dav/files/$username', - ); - } - CoreClient? _core; NewsClient? _news; NotesClient? _notes; @@ -73,6 +56,9 @@ class NextcloudClient extends DynamiteClient { UppushClient? _uppush; UserStatusClient? _userStatus; + /// Client for WebDAV + WebDavClient get webdav => _webdav ??= WebDavClient(this); + /// Client for the core APIs CoreClient get core => _core ??= CoreClient.fromClient(this); diff --git a/packages/nextcloud/lib/src/webdav/client.dart b/packages/nextcloud/lib/src/webdav/client.dart index e8efcbeb..8fe41e7f 100644 --- a/packages/nextcloud/lib/src/webdav/client.dart +++ b/packages/nextcloud/lib/src/webdav/client.dart @@ -7,20 +7,17 @@ import 'package:nextcloud/src/webdav/webdav.dart'; import 'package:universal_io/io.dart'; import 'package:xml/xml.dart' as xml; +/// Base path used on the server +const String webdavBasePath = '/remote.php/webdav'; + /// WebDavClient class class WebDavClient { // ignore: public_member_api_docs - WebDavClient( - this.rootClient, - this.basePath, - ); + WebDavClient(this.rootClient); // ignore: public_member_api_docs final DynamiteClient rootClient; - /// Base path used on the server - final String basePath; - Future _send( final String method, final String url, @@ -62,7 +59,7 @@ class WebDavClient { String _constructPath([final String? path]) => [ rootClient.baseURL, - basePath, + webdavBasePath, if (path != null) ...[ path, ], diff --git a/packages/nextcloud/lib/src/webdav/file.dart b/packages/nextcloud/lib/src/webdav/file.dart index edaff53a..9ed5618f 100644 --- a/packages/nextcloud/lib/src/webdav/file.dart +++ b/packages/nextcloud/lib/src/webdav/file.dart @@ -7,12 +7,7 @@ extension WebDavMultistatusFile on WebDavMultistatus { /// Convert the [WebDavMultistatus] into a [WebDavFile] for easier handling List toWebDavFiles(final WebDavClient client) => responses .where((final response) => response.href != null) - .map( - (final response) => WebDavFile( - basePath: client.basePath, - response: response, - ), - ) + .map((final response) => WebDavFile(response: response)) .toList(); } @@ -20,12 +15,9 @@ extension WebDavMultistatusFile on WebDavMultistatus { class WebDavFile { /// Creates a new WebDavFile object with the given path WebDavFile({ - required final String basePath, required final WebDavResponse response, - }) : _basePath = basePath, - _response = response; + }) : _response = response; - final String _basePath; final WebDavResponse _response; /// Get the props of the file @@ -34,7 +26,7 @@ class WebDavFile { /// The path of file late final String path = - Uri.decodeFull(_response.href!.substring(Uri.encodeFull(_basePath).length, _response.href!.length)); + Uri.decodeFull(_response.href!.substring(Uri.encodeFull(webdavBasePath).length, _response.href!.length)); /// The fileid namespaced by the instance id, globally unique late final String? id = props.ocid; diff --git a/packages/nextcloud/test/webdav_test.dart b/packages/nextcloud/test/webdav_test.dart index 25396a30..e737a55d 100644 --- a/packages/nextcloud/test/webdav_test.dart +++ b/packages/nextcloud/test/webdav_test.dart @@ -23,17 +23,9 @@ void main() { }); tearDown(() => container.destroy()); - test('Fail without username', () async { - client = await getTestClient( - container, - username: null, - ); - expect(() => client.webdav, throwsException); - }); - test('Get status', () async { final status = await client.webdav.status(); - expect(status.capabilities, containsAll(['1', '3', 'access-control'])); + expect(status.capabilities, containsAll(['1', '3'])); expect(status.searchCapabilities, hasLength(0)); });