Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions packages/nextcloud/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
13 changes: 5 additions & 8 deletions packages/nextcloud/lib/src/webdav/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpClientResponse> _send(
final String method,
final String url,
Expand Down Expand Up @@ -62,7 +59,7 @@ class WebDavClient {

String _constructPath([final String? path]) => [
rootClient.baseURL,
basePath,
webdavBasePath,
if (path != null) ...[
path,
],
Expand Down
14 changes: 3 additions & 11 deletions packages/nextcloud/lib/src/webdav/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@ extension WebDavMultistatusFile on WebDavMultistatus {
/// Convert the [WebDavMultistatus] into a [WebDavFile] for easier handling
List<WebDavFile> 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();
}

/// WebDavFile class
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
Expand All @@ -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;
Expand Down
10 changes: 1 addition & 9 deletions packages/nextcloud/test/webdav_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Expand Down