Browse Source

refactor(nextcloud): Use WebDAV endpoint that does not require the username

pull/515/head
jld3103 1 year ago
parent
commit
a37e583598
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 22
      packages/nextcloud/lib/src/client.dart
  2. 13
      packages/nextcloud/lib/src/webdav/client.dart
  3. 14
      packages/nextcloud/lib/src/webdav/file.dart
  4. 10
      packages/nextcloud/test/webdav_test.dart

22
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. /// Identifier used for authentication. This can be the username or email or something else.
final String? loginName; final String? loginName;
/// Username of the user on the server, it needs to be set for using WebDAV. /// Username of the user on the server
/// It can be obtained via the
final String? username; final String? username;
WebDavClient? _webdav; 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; CoreClient? _core;
NewsClient? _news; NewsClient? _news;
NotesClient? _notes; NotesClient? _notes;
@ -73,6 +56,9 @@ class NextcloudClient extends DynamiteClient {
UppushClient? _uppush; UppushClient? _uppush;
UserStatusClient? _userStatus; UserStatusClient? _userStatus;
/// Client for WebDAV
WebDavClient get webdav => _webdav ??= WebDavClient(this);
/// Client for the core APIs /// Client for the core APIs
CoreClient get core => _core ??= CoreClient.fromClient(this); CoreClient get core => _core ??= CoreClient.fromClient(this);

13
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:universal_io/io.dart';
import 'package:xml/xml.dart' as xml; import 'package:xml/xml.dart' as xml;
/// Base path used on the server
const String webdavBasePath = '/remote.php/webdav';
/// WebDavClient class /// WebDavClient class
class WebDavClient { class WebDavClient {
// ignore: public_member_api_docs // ignore: public_member_api_docs
WebDavClient( WebDavClient(this.rootClient);
this.rootClient,
this.basePath,
);
// ignore: public_member_api_docs // ignore: public_member_api_docs
final DynamiteClient rootClient; final DynamiteClient rootClient;
/// Base path used on the server
final String basePath;
Future<HttpClientResponse> _send( Future<HttpClientResponse> _send(
final String method, final String method,
final String url, final String url,
@ -62,7 +59,7 @@ class WebDavClient {
String _constructPath([final String? path]) => [ String _constructPath([final String? path]) => [
rootClient.baseURL, rootClient.baseURL,
basePath, webdavBasePath,
if (path != null) ...[ if (path != null) ...[
path, path,
], ],

14
packages/nextcloud/lib/src/webdav/file.dart

@ -7,12 +7,7 @@ extension WebDavMultistatusFile on WebDavMultistatus {
/// Convert the [WebDavMultistatus] into a [WebDavFile] for easier handling /// Convert the [WebDavMultistatus] into a [WebDavFile] for easier handling
List<WebDavFile> toWebDavFiles(final WebDavClient client) => responses List<WebDavFile> toWebDavFiles(final WebDavClient client) => responses
.where((final response) => response.href != null) .where((final response) => response.href != null)
.map( .map((final response) => WebDavFile(response: response))
(final response) => WebDavFile(
basePath: client.basePath,
response: response,
),
)
.toList(); .toList();
} }
@ -20,12 +15,9 @@ extension WebDavMultistatusFile on WebDavMultistatus {
class WebDavFile { class WebDavFile {
/// Creates a new WebDavFile object with the given path /// Creates a new WebDavFile object with the given path
WebDavFile({ WebDavFile({
required final String basePath,
required final WebDavResponse response, required final WebDavResponse response,
}) : _basePath = basePath, }) : _response = response;
_response = response;
final String _basePath;
final WebDavResponse _response; final WebDavResponse _response;
/// Get the props of the file /// Get the props of the file
@ -34,7 +26,7 @@ class WebDavFile {
/// The path of file /// The path of file
late final String path = 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 /// The fileid namespaced by the instance id, globally unique
late final String? id = props.ocid; late final String? id = props.ocid;

10
packages/nextcloud/test/webdav_test.dart

@ -23,17 +23,9 @@ void main() {
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());
test('Fail without username', () async {
client = await getTestClient(
container,
username: null,
);
expect(() => client.webdav, throwsException);
});
test('Get status', () async { test('Get status', () async {
final status = await client.webdav.status(); 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)); expect(status.searchCapabilities, hasLength(0));
}); });

Loading…
Cancel
Save