Browse Source

Merge pull request #262 from provokateurin/feature/webdav-serializable

Make WebDav serializable
pull/275/head
Kate 2 years ago committed by GitHub
parent
commit
a1b333e75f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      packages/app/pubspec.lock
  2. 1
      packages/neon/neon/lib/neon.dart
  3. 60
      packages/neon/neon/lib/src/utils/request_manager.dart
  4. 1
      packages/neon/neon/pubspec.yaml
  5. 35
      packages/neon/neon_files/lib/blocs/browser.dart
  6. 8
      packages/neon/neon_files/lib/blocs/files.dart
  7. 1
      packages/neon/neon_files/lib/neon_files.dart
  8. 1
      packages/neon/neon_files/lib/utils/upload_task.dart
  9. 73
      packages/nextcloud/bin/generate_props.dart
  10. 4
      packages/nextcloud/lib/nextcloud.dart
  11. 224
      packages/nextcloud/lib/src/webdav/client.dart
  12. 243
      packages/nextcloud/lib/src/webdav/file.dart
  13. 26
      packages/nextcloud/lib/src/webdav/props.csv
  14. 423
      packages/nextcloud/lib/src/webdav/props.dart
  15. 1915
      packages/nextcloud/lib/src/webdav/props.g.dart
  16. 137
      packages/nextcloud/lib/src/webdav/webdav.dart
  17. 477
      packages/nextcloud/lib/src/webdav/webdav.g.dart
  18. 2
      packages/nextcloud/pubspec.yaml
  19. 295
      packages/nextcloud/test/webdav.dart
  20. 1
      tool/generate-nextcloud.sh

8
packages/app/pubspec.lock

@ -1281,6 +1281,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.2.2"
xml_annotation:
dependency: transitive
description:
name: xml_annotation
sha256: "4c67438bd8cbe4c717b66f3991631ea3cdd372dc77255f9fd166a6b6e1cf6965"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
yaml:
dependency: transitive
description:

1
packages/neon/neon/lib/neon.dart

@ -41,6 +41,7 @@ import 'package:url_launcher/url_launcher_string.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:window_manager/window_manager.dart';
import 'package:xdg_directories/xdg_directories.dart' as xdg;
import 'package:xml/xml.dart' as xml;
export 'l10n/localizations.dart';
export 'src/models/account.dart';

60
packages/neon/neon/lib/src/utils/request_manager.dart

@ -14,8 +14,50 @@ class RequestManager {
final Future<R> Function() call,
final T Function(R) unwrap, {
final bool disableTimeout = false,
final int retries = 0,
}) async {
}) async =>
_wrap<T, R>(
clientID,
k,
subject,
call,
unwrap,
(final data) => json.encode(serializeNextcloud<R>(data)),
(final data) => deserializeNextcloud<R>(json.decode(data)),
disableTimeout,
0,
);
Future wrapWebDav<T>(
final String clientID,
final String k,
final BehaviorSubject<Result<T>> subject,
final Future<WebDavMultistatus> Function() call,
final T Function(WebDavMultistatus) unwrap, {
final bool disableTimeout = false,
}) async =>
_wrap<T, WebDavMultistatus>(
clientID,
k,
subject,
call,
unwrap,
(final data) => data.toXmlElement(namespaces: namespaces).toXmlString(),
(final data) => WebDavMultistatus.fromXmlElement(xml.XmlDocument.parse(data).rootElement),
disableTimeout,
0,
);
Future _wrap<T, R>(
final String clientID,
final String k,
final BehaviorSubject<Result<T>> subject,
final Future<R> Function() call,
final T Function(R) unwrap,
final String Function(R) serialize,
final R Function(String) deserialize,
final bool disableTimeout,
final int retries,
) async {
if (subject.valueOrNull?.data != null) {
subject.add(
Result(
@ -35,7 +77,7 @@ class RequestManager {
try {
subject.add(
Result(
unwrap(deserializeNextcloud<R>(json.decode((await cache!.get(key))!))),
unwrap(await compute(deserialize, (await cache!.get(key))!)),
null,
loading: true,
cached: true,
@ -49,21 +91,23 @@ class RequestManager {
try {
final response = await (disableTimeout ? call() : timeout(call));
await cache?.set(key, json.encode(serializeNextcloud<R>(response)));
await cache?.set(key, await compute(serialize, response));
subject.add(Result.success(unwrap(response)));
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
if (e is NextcloudApiException && e.statusCode >= 500 && retries < 3) {
debugPrint('Retrying...');
await wrapNextcloud(
await _wrap(
clientID,
k,
subject,
call,
unwrap,
disableTimeout: disableTimeout,
retries: retries + 1,
serialize,
deserialize,
disableTimeout,
retries + 1,
);
return;
}
@ -71,7 +115,7 @@ class RequestManager {
try {
subject.add(
Result(
unwrap(deserializeNextcloud<R>(json.decode((await cache!.get(key))!))),
unwrap(await compute(deserialize, (await cache!.get(key))!)),
null,
loading: false,
cached: true,

1
packages/neon/neon/pubspec.yaml

@ -46,6 +46,7 @@ dependencies:
webview_flutter: ^3.0.0
window_manager: ^0.2.5
xdg_directories: ^0.2.0+1
xml: ^6.2.2
dev_dependencies:
build_runner: ^2.1.7

35
packages/neon/neon_files/lib/blocs/browser.dart

@ -14,12 +14,14 @@ abstract class FilesBrowserBlocStates {
class FilesBrowserBloc extends InteractiveBloc implements FilesBrowserBlocEvents, FilesBrowserBlocStates {
FilesBrowserBloc(
this._requestManager,
this.options,
this.client,
) {
unawaited(refresh());
}
final RequestManager _requestManager;
final FilesAppSpecificOptions options;
final NextcloudClient client;
@ -38,26 +40,23 @@ class FilesBrowserBloc extends InteractiveBloc implements FilesBrowserBlocEvents
@override
Future refresh() async {
// TODO: We have to do this manually, because we can't cache WebDAV stuff right now
try {
files.add(Result.loading());
final data = await client.webdav.ls(
await _requestManager.wrapWebDav<List<WebDavFile>>(
client.id,
'files-${path.value.join('/')}',
files,
() async => client.webdav.ls(
path.value.join('/'),
props: {
WebDavProps.davContentType.name,
WebDavProps.davETag.name,
WebDavProps.davLastModified.name,
WebDavProps.ncHasPreview.name,
WebDavProps.ocSize.name,
WebDavProps.ocFavorite.name,
},
prop: WebDavPropfindProp(
davgetcontenttype: true,
davgetetag: true,
davgetlastmodified: true,
nchaspreview: true,
ocsize: true,
ocfavorite: true,
),
),
(final response) => response.toWebDavFiles(client.webdav),
);
files.add(Result.success(data));
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
files.add(Result.error(e));
}
}
@override

8
packages/neon/neon_files/lib/blocs/files.dart

@ -30,6 +30,7 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
FilesBloc(
this.options,
this.client,
this._requestManager,
this._platform,
) {
options.uploadQueueParallelism.stream.listen((final value) {
@ -42,6 +43,7 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
final FilesAppSpecificOptions options;
final NextcloudClient client;
final RequestManager _requestManager;
final NeonPlatform _platform;
late final browser = getNewFilesBrowserBloc();
@ -64,7 +66,7 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
@override
void addFavorite(final List<String> path) {
wrapAction(() async => client.webdav.updateProps(path.join('/'), {WebDavProps.ocFavorite.name: '1'}));
wrapAction(() async => client.webdav.updateProps(path.join('/'), WebDavProp(ocfavorite: 1)));
}
@override
@ -120,7 +122,7 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
wrapAction(
() async => client.webdav.updateProps(
path.join('/'),
{WebDavProps.ocFavorite.name: '0'},
WebDavProp(ocfavorite: 0),
),
);
}
@ -193,5 +195,5 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
}
}
FilesBrowserBloc getNewFilesBrowserBloc() => FilesBrowserBloc(options, client);
FilesBrowserBloc getNewFilesBrowserBloc() => FilesBrowserBloc(_requestManager, options, client);
}

1
packages/neon/neon_files/lib/neon_files.dart

@ -51,6 +51,7 @@ class FilesApp extends AppImplementation<FilesBloc, FilesAppSpecificOptions> {
FilesBloc buildBloc(final NextcloudClient client) => FilesBloc(
options,
client,
requestManager,
platform,
);

1
packages/neon/neon_files/lib/utils/upload_task.dart

@ -24,6 +24,7 @@ class UploadTask {
return Uint8List.fromList(chunk);
}),
path.join('/'),
lastModified: lastModified,
);
}
}

73
packages/nextcloud/bin/generate_props.dart

@ -0,0 +1,73 @@
import 'dart:io';
void main() {
final props = File('lib/src/webdav/props.csv').readAsLinesSync().map((final line) => line.split(','));
final valueProps = <String>[];
final findProps = <String>[];
final variables = <String>[];
for (final prop in props) {
final namespacePrefix = prop[0];
final namespaceVariable = convertNamespace(namespacePrefix);
final type = prop[2];
final name = prop[1];
final variable = namespacePrefix + name.toLowerCase().replaceAll(RegExp('[^a-z]'), '');
valueProps.addAll([
"@annotation.XmlElement(name: '$name', namespace: $namespaceVariable, includeIfNull: false)",
'$type? $variable;',
]);
findProps.addAll([
"@annotation.XmlElement(name: '$name', namespace: $namespaceVariable, includeIfNull: false)",
'bool? $variable;',
]);
variables.add(variable);
}
File('lib/src/webdav/props.dart').writeAsStringSync(
[
'// ignore_for_file: public_member_api_docs',
"import 'package:nextcloud/src/webdav/webdav.dart';",
"import 'package:xml/xml.dart';",
"import 'package:xml_annotation/xml_annotation.dart' as annotation;",
"part 'props.g.dart';",
'',
...generateClass('WebDavPropfindProp', 'prop', 'namespaceDav', findProps, variables),
...generateClass('WebDavProp', 'prop', 'namespaceDav', valueProps, variables),
...generateClass('WebDavOcFilterRules', 'filter-rules', 'namespaceOwncloud', valueProps, variables),
].join('\n'),
);
}
List<String> generateClass(
final String name,
final String elementName,
final String namespace,
final List<String> props,
final List<String> variables,
) =>
[
'@annotation.XmlSerializable(createMixin: true)',
"@annotation.XmlRootElement(name: '$elementName', namespace: $namespace)",
'class $name with _\$${name}XmlSerializableMixin {',
' $name({',
...variables.map((final variable) => ' this.$variable,'),
' });',
' factory $name.fromXmlElement(final XmlElement element) => _\$${name}FromXmlElement(element);',
...props.map((final prop) => ' $prop'),
'}',
];
String convertNamespace(final String namespacePrefix) {
switch (namespacePrefix) {
case 'dav':
return 'namespaceDav';
case 'oc':
return 'namespaceOwncloud';
case 'nc':
return 'namespaceNextcloud';
case 'ocs':
return 'namespaceOpenCollaborationServices';
case 'ocm':
return 'namespaceOpenCloudMesh';
default:
throw Exception('Unknown namespace prefix "$namespacePrefix"');
}
}

4
packages/nextcloud/lib/nextcloud.dart

@ -5,7 +5,6 @@ import 'dart:io';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:intl/intl.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/src/nextcloud.openapi.dart' as openapi;
import 'package:version/version.dart';
@ -14,6 +13,8 @@ import 'package:xml/xml.dart' as xml;
export 'package:crypton/crypton.dart' show RSAKeypair, RSAPrivateKey, RSAPublicKey;
export 'src/nextcloud.openapi.dart' hide NextcloudClient;
export 'src/webdav/props.dart';
export 'src/webdav/webdav.dart';
part 'src/app_type.dart';
part 'src/client.dart';
@ -21,4 +22,3 @@ part 'src/helpers.dart';
part 'src/version_supported.dart';
part 'src/webdav/client.dart';
part 'src/webdav/file.dart';
part 'src/webdav/props.dart';

224
packages/nextcloud/lib/src/webdav/client.dart

@ -14,18 +14,6 @@ class WebDavClient {
/// Base path used on the server
final String basePath;
/// XML namespaces supported by this client.
///
/// For Nextcloud namespaces see [WebDav/Requesting properties](https://docs.nextcloud.com/server/latest/developer_manual/client_apis/WebDAV/basic.html#requesting-properties).
final Map<String, String> namespaces = {
'DAV:': 'd',
'http://owncloud.org/ns': 'oc',
'http://nextcloud.org/ns': 'nc',
'http://open-collaboration-services.org/ns': 'ocs',
'http://open-cloud-mesh.org/ns': 'ocm',
'http://sabredav.org/ns': 's', // mostly used in error responses
};
Future<HttpClientResponse> _send(
final String method,
final String url,
@ -65,12 +53,6 @@ class WebDavClient {
return response;
}
/// Registers a custom namespace for properties.
///
/// Requires a unique [namespaceUri] and [prefix].
void registerNamespace(final String namespaceUri, final String prefix) =>
namespaces.putIfAbsent(namespaceUri, () => prefix);
String _constructPath([final String? path]) => [
rootClient.baseURL,
basePath,
@ -90,25 +72,6 @@ class WebDavClient {
.where((final part) => part.isNotEmpty)
.join('/');
String _buildPropsRequest(final Set<String> props) {
final builder = xml.XmlBuilder();
builder
..processing('xml', 'version="1.0"')
..element(
'd:propfind',
nest: () {
namespaces.forEach(builder.namespace);
builder.element(
'd:prop',
nest: () {
props.forEach(builder.element);
},
);
},
);
return builder.buildDocument().toString();
}
/// returns the WebDAV capabilities of the server
Future<WebDavStatus> status() async {
final response = await _send(
@ -175,18 +138,37 @@ class WebDavClient {
[204],
);
Map<String, String>? _generateUploadHeaders({
required final DateTime? lastModified,
required final DateTime? created,
}) {
final headers = <String, String>{
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<HttpClientResponse> 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: lastModified != null ? {'X-OC-Mtime': (lastModified.millisecondsSinceEpoch ~/ 1000).toString()} : null,
headers: _generateUploadHeaders(
lastModified: lastModified,
created: created,
),
);
/// upload a new file with [localData] as content to [remotePath]
@ -194,13 +176,17 @@ class WebDavClient {
final Stream<Uint8List> localData,
final String remotePath, {
final DateTime? lastModified,
final DateTime? created,
}) async =>
_send(
'PUT',
_constructPath(remotePath),
[200, 201, 204],
data: localData,
headers: lastModified != null ? {'X-OC-Mtime': (lastModified.millisecondsSinceEpoch ~/ 1000).toString()} : null,
headers: _generateUploadHeaders(
lastModified: lastModified,
created: created,
),
);
/// download [remotePath] and store the response file contents to String
@ -221,100 +207,69 @@ class WebDavClient {
[200],
);
Future<WebDavMultistatus> _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 [props] on the returned files.
Future<List<WebDavFile>> ls(
/// Optionally populates the given [prop]s on the returned files.
Future<WebDavMultistatus> ls(
final String remotePath, {
final Set<String>? props,
final WebDavPropfindProp? prop,
final int? depth,
}) async {
final response = await _send(
'PROPFIND',
_constructPath(remotePath),
[207, 301],
data: Stream.value(Uint8List.fromList(utf8.encode(_buildPropsRequest(props ?? {})))),
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);
return ls(
response.headers['location']!.first,
prop: prop,
depth: depth,
);
}
return treeFromWebDavXml(
basePath,
namespaces,
await response.body,
)..removeAt(0);
return _parseResponse(response);
}
/// Runs the filter-files report with the given [propFilters] on the
/// Runs the filter-files report with the given [filterRules] on the
/// [remotePath].
///
/// Optionally populates the given [props] on the returned files.
Future<List<WebDavFile>> filter(
/// Optionally populates the given [prop]s on the returned files.
Future<WebDavMultistatus> filter(
final String remotePath,
final Map<String, String> propFilters, {
final Set<String> props = const {},
final WebDavOcFilterRules filterRules, {
final WebDavPropfindProp? prop,
}) async {
final builder = xml.XmlBuilder();
builder
..processing('xml', 'version="1.0"')
..element(
'oc:filter-files',
nest: () {
namespaces.forEach(builder.namespace);
builder
..element(
'oc:filter-rules',
nest: () {
propFilters.forEach((final key, final value) {
builder.element(
key,
nest: () {
builder.text(value);
},
);
});
},
)
..element(
'd:prop',
nest: () {
props.forEach(builder.element);
},
);
},
);
final response = await _send(
'REPORT',
_constructPath(remotePath),
[200, 207],
data: Stream.value(Uint8List.fromList(utf8.encode(builder.buildDocument().toString()))),
);
return treeFromWebDavXml(
basePath,
namespaces,
await response.body,
);
}
/// Retrieves properties for the given [remotePath].
///
/// Populates all available properties by default, but a reduced set can be
/// specified via [props].
Future<WebDavFile> getProps(
final String remotePath, {
final Set<String>? props,
}) async {
final response = await _send(
'PROPFIND',
_constructPath(remotePath),
[200, 207],
data: Stream.value(Uint8List.fromList(utf8.encode(_buildPropsRequest(props ?? {})))),
headers: {'Depth': '0'},
);
return fileFromWebDavXml(
basePath,
namespaces,
await response.body,
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].
@ -322,42 +277,29 @@ class WebDavClient {
/// Returns true if the update was successful.
Future<bool> updateProps(
final String remotePath,
final Map<String, String> props,
final WebDavProp prop,
) async {
final builder = xml.XmlBuilder();
builder
..processing('xml', 'version="1.0"')
..element(
'd:propertyupdate',
nest: () {
namespaces.forEach(builder.namespace);
builder.element(
'd:set',
nest: () {
builder.element(
'd:prop',
nest: () {
props.forEach((final key, final value) {
builder.element(
key,
nest: () {
builder.text(value);
},
);
});
},
);
},
);
},
);
final response = await _send(
'PROPPATCH',
_constructPath(remotePath),
[200, 207],
data: Stream.value(Uint8List.fromList(utf8.encode(builder.buildDocument().toString()))),
data: Stream.value(
Uint8List.fromList(
utf8.encode(
WebDavPropertyupdate(set: WebDavSet(prop: prop)).toXmlElement(namespaces: namespaces).toXmlString(),
),
),
),
);
return checkUpdateFromWebDavXml(await response.body);
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]

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

@ -1,142 +1,88 @@
part of '../../nextcloud.dart';
// ignore: public_member_api_docs
extension WebDavMultistatusFile on WebDavMultistatus {
/// Convert the [WebDavMultistatus] into a [WebDavFile] for easier handling
List<WebDavFile> toWebDavFiles(final WebDavClient client) => responses
.sublist(1)
.where((final response) => response.href != null)
.map(
(final response) => WebDavFile(
basePath: client.basePath,
response: response,
),
)
.toList();
}
/// WebDavFile class
class WebDavFile {
/// Creates a new WebDavFile object with the given path
WebDavFile({
required this.path,
required final List<xml.XmlElement> props,
required final Map<String, String> namespaces,
}) : _props = props,
_namespaces = namespaces;
required final String basePath,
required final WebDavResponse response,
}) : _basePath = basePath,
_response = response;
/// The path of file
final String path;
final String _basePath;
final WebDavResponse _response;
final List<xml.XmlElement> _props;
final Map<String, String> _namespaces;
/// Get the props of the file
late final WebDavProp props =
_response.propstats.singleWhere((final propstat) => propstat.status.contains('200')).prop;
/// Gets a prop by it's qualified name. It already does namespace matching
xml.XmlElement? getProp(final String qualifiedName) {
final name = xml.XmlName.fromString(qualifiedName);
final namespaceUri = _namespaces.keys.singleWhere((final namespaceUri) => _namespaces[namespaceUri] == name.prefix);
final matches =
_props.where((final prop) => prop.name.local == name.local && prop.namespaceUri == namespaceUri).toList();
if (matches.isNotEmpty) {
return matches[0];
}
return null;
}
/// The path of file
late final String path =
Uri.decodeFull(_response.href!.substring(Uri.encodeFull(_basePath).length, _response.href!.length));
/// The fileid namespaced by the instance id, globally unique
String? get id => getProp(WebDavProps.ocId.name)?.text;
late final String? id = props.ocid;
/// The unique id for the file within the instance
String? get fileId => getProp(WebDavProps.ocFileId.name)?.text;
/// Whether this is a collection resource type
bool? get isCollection {
final prop = getProp(WebDavProps.davResourceType.name);
if (prop != null) {
return prop.getElement('d:collection') != null;
}
return null;
}
late final String? fileId = props.ocfileid;
/// Mime-type of the file
String? get mimeType => getProp(WebDavProps.davContentType.name)?.text;
late final String? mimeType = props.davgetcontenttype;
/// ETag of the file
String? get etag => getProp(WebDavProps.davETag.name)?.text;
late final String? etag = props.davgetetag;
/// File content length or folder size
int? get size {
for (final prop in [
getProp(WebDavProps.ocSize.name),
getProp(WebDavProps.davContentLength.name),
]) {
if (prop != null) {
return int.parse(prop.text);
}
}
return null;
}
late final int? size = props.ocsize ?? props.davgetcontentlength;
/// The user id of the owner of a shared file
String? get ownerId => getProp(WebDavProps.ocOwnerId.name)?.text;
late final String? ownerId = props.ocownerid;
/// The display name of the owner of a shared file
String? get ownerDisplay => getProp(WebDavProps.ocOwnerDisplayName.name)?.text;
late final String? ownerDisplay = props.ocownerdisplayname;
/// Share note
String? get note => getProp(WebDavProps.ncNote.name)?.text;
late final String? note = props.ncnote;
/// Last modified date of the file
DateTime? get lastModified {
final prop = getProp(WebDavProps.davLastModified.name);
if (prop != null) {
return DateFormat('E, d MMM yyyy HH:mm:ss', 'en_US').parseUtc(prop.text);
late final DateTime? lastModified = () {
if (props.davgetlastmodified != null) {
return webdavDateFormat.parseUtc(props.davgetlastmodified!);
}
return null;
}
}();
/// Upload date of the file
DateTime? get uploadedDate {
final prop = getProp(WebDavProps.ncUploadTime.name);
if (prop != null) {
return DateTime.fromMillisecondsSinceEpoch(int.parse(prop.text) * 1000);
}
return null;
}
late final DateTime? uploadedDate =
props.ncuploadtime != null ? DateTime.fromMillisecondsSinceEpoch(props.ncuploadtime! * 1000) : null;
/// Creation date of the file as provided by uploader
DateTime? get createdDate {
final prop = getProp(WebDavProps.ncCreationTime.name);
if (prop != null) {
return DateTime.fromMillisecondsSinceEpoch(int.parse(prop.text) * 1000);
}
return null;
}
/// List of types of shares of the file
List<int>? get shareTypes {
final prop = getProp(WebDavProps.ocShareTypes.name);
if (prop != null) {
return prop.findElements('oc:share-type').map((final element) => int.parse(element.text)).toList();
}
return null;
}
/// User IDs of sharees
List<String>? get sharees {
final prop = getProp(WebDavProps.ncShareees.name);
if (prop != null) {
return prop.findAllElements('nc:id').map((final e) => e.text).toList();
}
return null;
}
late final DateTime? createdDate =
props.nccreationtime != null ? DateTime.fromMillisecondsSinceEpoch(props.nccreationtime! * 1000) : null;
/// Whether this file is marked as favorite
bool? get favorite {
final prop = getProp(WebDavProps.ocFavorite.name);
if (prop != null) {
return prop.text == '1';
}
return null;
}
late final bool? favorite = props.ocfavorite == null ? null : props.ocfavorite == 1;
/// Whether this file has a preview image
bool? get hasPreview {
final prop = getProp(WebDavProps.ncHasPreview.name);
if (prop != null) {
return prop.text == 'true';
}
return null;
}
late final bool? hasPreview = props.nchaspreview;
/// Returns the decoded name of the file / folder without the whole path
String get name {
late final String name = () {
// normalised path (remove trailing slash)
final end = path.endsWith('/') ? path.length - 1 : path.length;
final segments = Uri.parse(path, 0, end).pathSegments;
@ -144,101 +90,8 @@ class WebDavFile {
return segments.last;
}
return '';
}
}();
/// Returns if the file is a directory
bool get isDirectory => path.endsWith('/') || (isCollection ?? false);
// coverage:ignore-start
@override
String toString() =>
// ignore: lines_longer_than_80_chars
'WebDavFile{name: $name, id: $id, isDirectory: $isDirectory, path: $path, mimeType: $mimeType, size: $size, modificationTime: $lastModified, shareTypes: $shareTypes}';
// coverage:ignore-end
}
/// Converts a single d:response to a [WebDavFile]
WebDavFile _fromWebDavXml(
final String basePath,
final Map<String, String> namespaces,
final xml.XmlElement response,
) {
final davItemName = response.findElements('d:href').single.text;
final filePath = Uri.decodeFull(davItemName.substring(Uri.encodeFull(basePath).length, davItemName.length));
final allProps = <xml.XmlElement>[];
final propStatElements = response.findElements('d:propstat');
for (final propStat in propStatElements) {
final status = propStat.getElement('d:status')!.text;
final props = propStat.getElement('d:prop');
if (!status.contains('200')) {
// Skip any props that are not returned correctly (e.g. not found)
continue;
}
for (final prop in props!.nodes.whereType<xml.XmlElement>()) {
if (prop.children.isNotEmpty && prop.text.isNotEmpty) {
allProps.add(prop);
}
}
}
return WebDavFile(
path: filePath,
props: allProps,
namespaces: namespaces,
);
}
/// Extract a file from the webdav xml
WebDavFile fileFromWebDavXml(
final String basePath,
final Map<String, String> namespaces,
final String xmlStr,
) {
final xmlDocument = xml.XmlDocument.parse(xmlStr);
return _fromWebDavXml(
basePath,
namespaces,
xmlDocument.findAllElements('d:response').single,
);
}
/// Extract the file tree from the webdav xml
List<WebDavFile> treeFromWebDavXml(
final String basePath,
final Map<String, String> namespaces,
final String xmlStr,
) {
// Initialize a list to store the FileInfo Objects
final tree = [];
// parse the xml using the xml.XmlDocument.parse method
final xmlDocument = xml.XmlDocument.parse(xmlStr);
// Iterate over the response to find all folders / files and parse the information
for (final response in xmlDocument.findAllElements('d:response')) {
tree.add(
_fromWebDavXml(
basePath,
namespaces,
response,
),
);
}
return tree.cast<WebDavFile>();
}
/// Returns false if some updates have failed.
bool checkUpdateFromWebDavXml(final String xmlStr) {
final xmlDocument = xml.XmlDocument.parse(xmlStr);
final response = xmlDocument.findAllElements('d:response').single;
final propStatElements = response.findElements('d:propstat');
for (final propStat in propStatElements) {
final status = propStat.getElement('d:status')!.text;
if (!status.contains('200')) {
return false;
}
}
return true;
late final bool isDirectory = path.endsWith('/');
}

26
packages/nextcloud/lib/src/webdav/props.csv

@ -0,0 +1,26 @@
dav,getlastmodified,String
dav,getetag,String
dav,getcontenttype,String
dav,getcontentlength,int
oc,id,String
oc,fileid,String
oc,favorite,int
oc,comments-href,String
oc,comments-count,int
oc,comments-unread,int
oc,downloadURL,String
oc,owner-id,String
oc,owner-display-name,String
oc,size,int
oc,permissions,String
nc,note,String
nc,data-fingerprint,String
nc,has-preview,bool
nc,mount-type,String
nc,is-encrypted,int
nc,metadata_etag,String
nc,upload_time,int
nc,creation_time,int
nc,rich-workspace,String
ocs,share-permissions,int
ocm,share-permissions,String
1 dav getlastmodified String
2 dav getetag String
3 dav getcontenttype String
4 dav getcontentlength int
5 oc id String
6 oc fileid String
7 oc favorite int
8 oc comments-href String
9 oc comments-count int
10 oc comments-unread int
11 oc downloadURL String
12 oc owner-id String
13 oc owner-display-name String
14 oc size int
15 oc permissions String
16 nc note String
17 nc data-fingerprint String
18 nc has-preview bool
19 nc mount-type String
20 nc is-encrypted int
21 nc metadata_etag String
22 nc upload_time int
23 nc creation_time int
24 nc rich-workspace String
25 ocs share-permissions int
26 ocm share-permissions String

423
packages/nextcloud/lib/src/webdav/props.dart

@ -1,166 +1,263 @@
part of '../../nextcloud.dart';
/// Mapping of all WebDAV properties.
enum WebDavProps {
/// Contains the Last-Modified header value .
davLastModified('d:getlastmodified'),
/// Contains the ETag header value.
davETag('d:getetag'),
/// Contains the Content-Type header value.
davContentType('d:getcontenttype'),
/// Specifies the nature of the resource.
davResourceType('d:resourcetype'),
/// Contains the Content-Length header.
davContentLength('d:getcontentlength'),
/// The fileid namespaced by the instance id, globally unique
ocId('oc:id'),
/// The unique id for the file within the instance
ocFileId('oc:fileid'),
/// List of user specified tags. Can be modified.
ocTags('oc:tags'),
/// Whether a resource is tagged as favorite.
/// Can be modified and reported on with list-files.
ocFavorite('oc:favorite'),
/// List of collaborative tags. Can be reported on with list-files.
///
/// Valid system tags are:
/// - oc:id
/// - oc:display-name
/// - oc:user-visible
/// - oc:user-assignable
/// - oc:groups
/// - oc:can-assign
ocSystemTag('oc:systemtag'),
/// Can be reported on with list-files.
ocCircle('oc:circle'),
/// Link to the comments for this resource.
ocCommentsHref('oc:comments-href'),
/// Number of comments.
ocCommentsCount('oc:comments-count'),
/// Number of unread comments.
ocCommentsUnread('oc:comments-unread'),
/// Download URL.
ocDownloadURL('oc:downloadURL'),
/// The user id of the owner of a shared file
ocOwnerId('oc:owner-id'),
/// The display name of the owner of a shared file
ocOwnerDisplayName('oc:owner-display-name'),
/// Share types of this file.
///
/// Returns a list of share-type objects where:
/// - 0: user share
/// - 1: group share
/// - 2: usergroup share
/// - 3: public link
/// - 4: email
/// - 5: contact
/// - 6: remote (federated cloud)
/// - 7: circle
/// - 8: guest
/// - 9: remote group
/// - 10: room (talk conversation)
/// - 11: userroom
/// See also [OCS Share API](https://docs.nextcloud.com/server/19/developer_manual/client_apis/OCS/ocs-share-api.html)
ocShareTypes('oc:share-types'),
/// List of users this file is shared with.
///
/// Returns a list of sharee objects with:
/// - id
/// - display-name
/// - type (share type)
ncShareees('nc:sharees'),
/// Share note.
ncNote('nc:note'),
/// Checksums as provided during upload.
///
/// Returns a list of checksum objects.
ocChecksums('oc:checksums'),
/// Unlike [[davContentLength]], this property also works for folders
/// reporting the size of everything in the folder.
ocSize('oc:size'),
/// WebDAV permissions:
///
/// - S: shared
/// - R: shareable
/// - M: mounted
/// - G: readable
/// - D: deletable
/// - NV: updateable, renameable, moveble
/// - W: updateable (file)
/// - CK: creatable
ocPermissions('oc:permissions'),
/// Nextcloud CRUDS permissions:
///
/// - 1: read
/// - 2: update
/// - 4: create
/// - 8: delete
/// - 16: share
/// - 31: all
ocsSharePermissions('ocs:share-permissions'),
/// OCM permissions:
///
/// - share
/// - read
/// - write
ocmSharePermissions('ocm:share-permissions'),
/// system data-fingerprint
ncDataFingerprint('nc:data-fingerprint'),
/// Whether a preview is available.
ncHasPreview('nc:has-preview'),
/// Mount type, e.g. global, group, user, personal, shared, shared-root, external
ncMountType('nc:mount-type'),
/// Is this file is encrypted, 0 for false or 1 for true.
ncIsEncrypted('nc:is-encrypted'),
// ignore: public_member_api_docs
ncMetadataETag('nc:metadata_etag'),
/// Date this file was uploaded.
ncUploadTime('nc:upload_time'),
/// Creation time of the file as provided during upload.
ncCreationTime('nc:creation_time'),
// ignore: public_member_api_docs
ncRichWorkspace('nc:rich-workspace');
// ignore: public_member_api_docs
const WebDavProps(this.name);
// ignore_for_file: public_member_api_docs
import 'package:nextcloud/src/webdav/webdav.dart';
import 'package:xml/xml.dart';
import 'package:xml_annotation/xml_annotation.dart' as annotation;
part 'props.g.dart';
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'prop', namespace: namespaceDav)
class WebDavPropfindProp with _$WebDavPropfindPropXmlSerializableMixin {
WebDavPropfindProp({
this.davgetlastmodified,
this.davgetetag,
this.davgetcontenttype,
this.davgetcontentlength,
this.ocid,
this.ocfileid,
this.ocfavorite,
this.occommentshref,
this.occommentscount,
this.occommentsunread,
this.ocdownloadurl,
this.ocownerid,
this.ocownerdisplayname,
this.ocsize,
this.ocpermissions,
this.ncnote,
this.ncdatafingerprint,
this.nchaspreview,
this.ncmounttype,
this.ncisencrypted,
this.ncmetadataetag,
this.ncuploadtime,
this.nccreationtime,
this.ncrichworkspace,
this.ocssharepermissions,
this.ocmsharepermissions,
});
factory WebDavPropfindProp.fromXmlElement(final XmlElement element) => _$WebDavPropfindPropFromXmlElement(element);
@annotation.XmlElement(name: 'getlastmodified', namespace: namespaceDav, includeIfNull: false)
bool? davgetlastmodified;
@annotation.XmlElement(name: 'getetag', namespace: namespaceDav, includeIfNull: false)
bool? davgetetag;
@annotation.XmlElement(name: 'getcontenttype', namespace: namespaceDav, includeIfNull: false)
bool? davgetcontenttype;
@annotation.XmlElement(name: 'getcontentlength', namespace: namespaceDav, includeIfNull: false)
bool? davgetcontentlength;
@annotation.XmlElement(name: 'id', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocid;
@annotation.XmlElement(name: 'fileid', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocfileid;
@annotation.XmlElement(name: 'favorite', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocfavorite;
@annotation.XmlElement(name: 'comments-href', namespace: namespaceOwncloud, includeIfNull: false)
bool? occommentshref;
@annotation.XmlElement(name: 'comments-count', namespace: namespaceOwncloud, includeIfNull: false)
bool? occommentscount;
@annotation.XmlElement(name: 'comments-unread', namespace: namespaceOwncloud, includeIfNull: false)
bool? occommentsunread;
@annotation.XmlElement(name: 'downloadURL', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocdownloadurl;
@annotation.XmlElement(name: 'owner-id', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocownerid;
@annotation.XmlElement(name: 'owner-display-name', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocownerdisplayname;
@annotation.XmlElement(name: 'size', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocsize;
@annotation.XmlElement(name: 'permissions', namespace: namespaceOwncloud, includeIfNull: false)
bool? ocpermissions;
@annotation.XmlElement(name: 'note', namespace: namespaceNextcloud, includeIfNull: false)
bool? ncnote;
@annotation.XmlElement(name: 'data-fingerprint', namespace: namespaceNextcloud, includeIfNull: false)
bool? ncdatafingerprint;
@annotation.XmlElement(name: 'has-preview', namespace: namespaceNextcloud, includeIfNull: false)
bool? nchaspreview;
@annotation.XmlElement(name: 'mount-type', namespace: namespaceNextcloud, includeIfNull: false)
bool? ncmounttype;
@annotation.XmlElement(name: 'is-encrypted', namespace: namespaceNextcloud, includeIfNull: false)
bool? ncisencrypted;
@annotation.XmlElement(name: 'metadata_etag', namespace: namespaceNextcloud, includeIfNull: false)
bool? ncmetadataetag;
@annotation.XmlElement(name: 'upload_time', namespace: namespaceNextcloud, includeIfNull: false)
bool? ncuploadtime;
@annotation.XmlElement(name: 'creation_time', namespace: namespaceNextcloud, includeIfNull: false)
bool? nccreationtime;
@annotation.XmlElement(name: 'rich-workspace', namespace: namespaceNextcloud, includeIfNull: false)
bool? ncrichworkspace;
@annotation.XmlElement(name: 'share-permissions', namespace: namespaceOpenCollaborationServices, includeIfNull: false)
bool? ocssharepermissions;
@annotation.XmlElement(name: 'share-permissions', namespace: namespaceOpenCloudMesh, includeIfNull: false)
bool? ocmsharepermissions;
}
/// Name of the prop
final String name;
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'prop', namespace: namespaceDav)
class WebDavProp with _$WebDavPropXmlSerializableMixin {
WebDavProp({
this.davgetlastmodified,
this.davgetetag,
this.davgetcontenttype,
this.davgetcontentlength,
this.ocid,
this.ocfileid,
this.ocfavorite,
this.occommentshref,
this.occommentscount,
this.occommentsunread,
this.ocdownloadurl,
this.ocownerid,
this.ocownerdisplayname,
this.ocsize,
this.ocpermissions,
this.ncnote,
this.ncdatafingerprint,
this.nchaspreview,
this.ncmounttype,
this.ncisencrypted,
this.ncmetadataetag,
this.ncuploadtime,
this.nccreationtime,
this.ncrichworkspace,
this.ocssharepermissions,
this.ocmsharepermissions,
});
factory WebDavProp.fromXmlElement(final XmlElement element) => _$WebDavPropFromXmlElement(element);
@annotation.XmlElement(name: 'getlastmodified', namespace: namespaceDav, includeIfNull: false)
String? davgetlastmodified;
@annotation.XmlElement(name: 'getetag', namespace: namespaceDav, includeIfNull: false)
String? davgetetag;
@annotation.XmlElement(name: 'getcontenttype', namespace: namespaceDav, includeIfNull: false)
String? davgetcontenttype;
@annotation.XmlElement(name: 'getcontentlength', namespace: namespaceDav, includeIfNull: false)
int? davgetcontentlength;
@annotation.XmlElement(name: 'id', namespace: namespaceOwncloud, includeIfNull: false)
String? ocid;
@annotation.XmlElement(name: 'fileid', namespace: namespaceOwncloud, includeIfNull: false)
String? ocfileid;
@annotation.XmlElement(name: 'favorite', namespace: namespaceOwncloud, includeIfNull: false)
int? ocfavorite;
@annotation.XmlElement(name: 'comments-href', namespace: namespaceOwncloud, includeIfNull: false)
String? occommentshref;
@annotation.XmlElement(name: 'comments-count', namespace: namespaceOwncloud, includeIfNull: false)
int? occommentscount;
@annotation.XmlElement(name: 'comments-unread', namespace: namespaceOwncloud, includeIfNull: false)
int? occommentsunread;
@annotation.XmlElement(name: 'downloadURL', namespace: namespaceOwncloud, includeIfNull: false)
String? ocdownloadurl;
@annotation.XmlElement(name: 'owner-id', namespace: namespaceOwncloud, includeIfNull: false)
String? ocownerid;
@annotation.XmlElement(name: 'owner-display-name', namespace: namespaceOwncloud, includeIfNull: false)
String? ocownerdisplayname;
@annotation.XmlElement(name: 'size', namespace: namespaceOwncloud, includeIfNull: false)
int? ocsize;
@annotation.XmlElement(name: 'permissions', namespace: namespaceOwncloud, includeIfNull: false)
String? ocpermissions;
@annotation.XmlElement(name: 'note', namespace: namespaceNextcloud, includeIfNull: false)
String? ncnote;
@annotation.XmlElement(name: 'data-fingerprint', namespace: namespaceNextcloud, includeIfNull: false)
String? ncdatafingerprint;
@annotation.XmlElement(name: 'has-preview', namespace: namespaceNextcloud, includeIfNull: false)
bool? nchaspreview;
@annotation.XmlElement(name: 'mount-type', namespace: namespaceNextcloud, includeIfNull: false)
String? ncmounttype;
@annotation.XmlElement(name: 'is-encrypted', namespace: namespaceNextcloud, includeIfNull: false)
int? ncisencrypted;
@annotation.XmlElement(name: 'metadata_etag', namespace: namespaceNextcloud, includeIfNull: false)
String? ncmetadataetag;
@annotation.XmlElement(name: 'upload_time', namespace: namespaceNextcloud, includeIfNull: false)
int? ncuploadtime;
@annotation.XmlElement(name: 'creation_time', namespace: namespaceNextcloud, includeIfNull: false)
int? nccreationtime;
@annotation.XmlElement(name: 'rich-workspace', namespace: namespaceNextcloud, includeIfNull: false)
String? ncrichworkspace;
@annotation.XmlElement(name: 'share-permissions', namespace: namespaceOpenCollaborationServices, includeIfNull: false)
int? ocssharepermissions;
@annotation.XmlElement(name: 'share-permissions', namespace: namespaceOpenCloudMesh, includeIfNull: false)
String? ocmsharepermissions;
}
// coverage:ignore-start
@override
String toString() => name;
// coverage:ignore-end
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'filter-rules', namespace: namespaceOwncloud)
class WebDavOcFilterRules with _$WebDavOcFilterRulesXmlSerializableMixin {
WebDavOcFilterRules({
this.davgetlastmodified,
this.davgetetag,
this.davgetcontenttype,
this.davgetcontentlength,
this.ocid,
this.ocfileid,
this.ocfavorite,
this.occommentshref,
this.occommentscount,
this.occommentsunread,
this.ocdownloadurl,
this.ocownerid,
this.ocownerdisplayname,
this.ocsize,
this.ocpermissions,
this.ncnote,
this.ncdatafingerprint,
this.nchaspreview,
this.ncmounttype,
this.ncisencrypted,
this.ncmetadataetag,
this.ncuploadtime,
this.nccreationtime,
this.ncrichworkspace,
this.ocssharepermissions,
this.ocmsharepermissions,
});
factory WebDavOcFilterRules.fromXmlElement(final XmlElement element) => _$WebDavOcFilterRulesFromXmlElement(element);
@annotation.XmlElement(name: 'getlastmodified', namespace: namespaceDav, includeIfNull: false)
String? davgetlastmodified;
@annotation.XmlElement(name: 'getetag', namespace: namespaceDav, includeIfNull: false)
String? davgetetag;
@annotation.XmlElement(name: 'getcontenttype', namespace: namespaceDav, includeIfNull: false)
String? davgetcontenttype;
@annotation.XmlElement(name: 'getcontentlength', namespace: namespaceDav, includeIfNull: false)
int? davgetcontentlength;
@annotation.XmlElement(name: 'id', namespace: namespaceOwncloud, includeIfNull: false)
String? ocid;
@annotation.XmlElement(name: 'fileid', namespace: namespaceOwncloud, includeIfNull: false)
String? ocfileid;
@annotation.XmlElement(name: 'favorite', namespace: namespaceOwncloud, includeIfNull: false)
int? ocfavorite;
@annotation.XmlElement(name: 'comments-href', namespace: namespaceOwncloud, includeIfNull: false)
String? occommentshref;
@annotation.XmlElement(name: 'comments-count', namespace: namespaceOwncloud, includeIfNull: false)
int? occommentscount;
@annotation.XmlElement(name: 'comments-unread', namespace: namespaceOwncloud, includeIfNull: false)
int? occommentsunread;
@annotation.XmlElement(name: 'downloadURL', namespace: namespaceOwncloud, includeIfNull: false)
String? ocdownloadurl;
@annotation.XmlElement(name: 'owner-id', namespace: namespaceOwncloud, includeIfNull: false)
String? ocownerid;
@annotation.XmlElement(name: 'owner-display-name', namespace: namespaceOwncloud, includeIfNull: false)
String? ocownerdisplayname;
@annotation.XmlElement(name: 'size', namespace: namespaceOwncloud, includeIfNull: false)
int? ocsize;
@annotation.XmlElement(name: 'permissions', namespace: namespaceOwncloud, includeIfNull: false)
String? ocpermissions;
@annotation.XmlElement(name: 'note', namespace: namespaceNextcloud, includeIfNull: false)
String? ncnote;
@annotation.XmlElement(name: 'data-fingerprint', namespace: namespaceNextcloud, includeIfNull: false)
String? ncdatafingerprint;
@annotation.XmlElement(name: 'has-preview', namespace: namespaceNextcloud, includeIfNull: false)
bool? nchaspreview;
@annotation.XmlElement(name: 'mount-type', namespace: namespaceNextcloud, includeIfNull: false)
String? ncmounttype;
@annotation.XmlElement(name: 'is-encrypted', namespace: namespaceNextcloud, includeIfNull: false)
int? ncisencrypted;
@annotation.XmlElement(name: 'metadata_etag', namespace: namespaceNextcloud, includeIfNull: false)
String? ncmetadataetag;
@annotation.XmlElement(name: 'upload_time', namespace: namespaceNextcloud, includeIfNull: false)
int? ncuploadtime;
@annotation.XmlElement(name: 'creation_time', namespace: namespaceNextcloud, includeIfNull: false)
int? nccreationtime;
@annotation.XmlElement(name: 'rich-workspace', namespace: namespaceNextcloud, includeIfNull: false)
String? ncrichworkspace;
@annotation.XmlElement(name: 'share-permissions', namespace: namespaceOpenCollaborationServices, includeIfNull: false)
int? ocssharepermissions;
@annotation.XmlElement(name: 'share-permissions', namespace: namespaceOpenCloudMesh, includeIfNull: false)
String? ocmsharepermissions;
}

1915
packages/nextcloud/lib/src/webdav/props.g.dart

File diff suppressed because it is too large Load Diff

137
packages/nextcloud/lib/src/webdav/webdav.dart

@ -0,0 +1,137 @@
// ignore_for_file: public_member_api_docs
import 'package:intl/intl.dart';
import 'package:nextcloud/src/webdav/props.dart';
import 'package:xml/xml.dart';
import 'package:xml_annotation/xml_annotation.dart' as annotation;
part 'webdav.g.dart';
/// Format used in WebDAV
final webdavDateFormat = DateFormat('E, d MMM yyyy HH:mm:ss', 'en_US');
const namespaceDav = 'DAV:';
const namespaceOwncloud = 'http://owncloud.org/ns';
const namespaceNextcloud = 'http://nextcloud.org/ns';
const namespaceOpenCollaborationServices = 'http://open-collaboration-services.org/ns';
const namespaceOpenCloudMesh = 'http://open-cloud-mesh.org/ns';
final Map<String, String> namespaces = {
namespaceDav: 'd',
namespaceOwncloud: 'oc',
namespaceNextcloud: 'nc',
namespaceOpenCollaborationServices: 'ocs',
namespaceOpenCloudMesh: 'ocm',
};
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'multistatus', namespace: namespaceDav)
class WebDavMultistatus with _$WebDavMultistatusXmlSerializableMixin {
WebDavMultistatus({
required this.responses,
});
factory WebDavMultistatus.fromXmlElement(final XmlElement element) => _$WebDavMultistatusFromXmlElement(element);
@annotation.XmlElement(name: 'response', namespace: namespaceDav)
final List<WebDavResponse> responses;
}
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'response', namespace: namespaceDav)
class WebDavResponse with _$WebDavResponseXmlSerializableMixin {
WebDavResponse({
required this.href,
required this.propstats,
});
factory WebDavResponse.fromXmlElement(final XmlElement element) => _$WebDavResponseFromXmlElement(element);
@annotation.XmlElement(name: 'href', namespace: namespaceDav)
final String? href;
@annotation.XmlElement(name: 'propstat', namespace: namespaceDav)
final List<WebDavPropstat> propstats;
}
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'propstat', namespace: namespaceDav)
class WebDavPropstat with _$WebDavPropstatXmlSerializableMixin {
WebDavPropstat({
required this.status,
required this.prop,
});
factory WebDavPropstat.fromXmlElement(final XmlElement element) => _$WebDavPropstatFromXmlElement(element);
@annotation.XmlElement(name: 'status', namespace: namespaceDav)
final String status;
@annotation.XmlElement(name: 'prop', namespace: namespaceDav)
final WebDavProp prop;
}
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'propertyupdate', namespace: namespaceDav)
class WebDavPropertyupdate with _$WebDavPropertyupdateXmlSerializableMixin {
WebDavPropertyupdate({
required this.set,
});
factory WebDavPropertyupdate.fromXmlElement(final XmlElement element) =>
_$WebDavPropertyupdateFromXmlElement(element);
@annotation.XmlElement(name: 'set', namespace: namespaceDav)
final WebDavSet set;
}
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'propertyupdate', namespace: namespaceDav)
class WebDavSet with _$WebDavSetXmlSerializableMixin {
WebDavSet({
required this.prop,
});
factory WebDavSet.fromXmlElement(final XmlElement element) => _$WebDavSetFromXmlElement(element);
@annotation.XmlElement(name: 'prop', namespace: namespaceDav)
final WebDavProp prop;
}
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'propfind', namespace: namespaceDav)
class WebDavPropfind with _$WebDavPropfindXmlSerializableMixin {
WebDavPropfind({
required this.prop,
});
factory WebDavPropfind.fromXmlElement(final XmlElement element) => _$WebDavPropfindFromXmlElement(element);
@annotation.XmlElement(name: 'prop', namespace: namespaceDav)
final WebDavPropfindProp prop;
}
@annotation.XmlSerializable(createMixin: true)
@annotation.XmlRootElement(name: 'filter-files', namespace: namespaceOwncloud)
class WebDavOcFilterFiles with _$WebDavOcFilterFilesXmlSerializableMixin {
WebDavOcFilterFiles({
required this.filterRules,
required this.prop,
});
factory WebDavOcFilterFiles.fromXmlElement(final XmlElement element) => _$WebDavOcFilterFilesFromXmlElement(element);
@annotation.XmlElement(name: 'filter-rules', namespace: namespaceOwncloud)
final WebDavOcFilterRules filterRules;
@annotation.XmlElement(name: 'prop', namespace: namespaceDav)
final WebDavPropfindProp prop;
}
// TODO: d:resourcetype
// TODO: oc:checksum
// TODO: oc:tags
// TODO: oc:systemtag
// TODO: oc:circle
// TODO: oc:share-types
// TODO: nc:sharees

477
packages/nextcloud/lib/src/webdav/webdav.g.dart

@ -0,0 +1,477 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'webdav.dart';
// **************************************************************************
// XmlSerializableGenerator
// **************************************************************************
void _$WebDavMultistatusBuildXmlChildren(WebDavMultistatus instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final responses = instance.responses;
final responsesSerialized = responses;
for (final value in responsesSerialized) {
builder.element('response', namespace: 'DAV:', nest: () {
value.buildXmlChildren(builder, namespaces: namespaces);
});
}
}
void _$WebDavMultistatusBuildXmlElement(WebDavMultistatus instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
builder.element('multistatus', namespace: 'DAV:', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
WebDavMultistatus _$WebDavMultistatusFromXmlElement(XmlElement element) {
final responses = element.getElements('response', namespace: 'DAV:')!;
return WebDavMultistatus(responses: responses.map((e) => WebDavResponse.fromXmlElement(e)).toList());
}
List<XmlAttribute> _$WebDavMultistatusToXmlAttributes(WebDavMultistatus instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
return attributes;
}
List<XmlNode> _$WebDavMultistatusToXmlChildren(WebDavMultistatus instance,
{Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final responses = instance.responses;
final responsesSerialized = responses;
final responsesConstructed = responsesSerialized.map((e) => XmlElement(XmlName('response', namespaces['DAV:']),
e.toXmlAttributes(namespaces: namespaces), e.toXmlChildren(namespaces: namespaces)));
children.addAll(responsesConstructed);
return children;
}
XmlElement _$WebDavMultistatusToXmlElement(WebDavMultistatus instance, {Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('multistatus', namespaces['DAV:']),
[...namespaces.toXmlAttributes(), ...instance.toXmlAttributes(namespaces: namespaces)],
instance.toXmlChildren(namespaces: namespaces));
}
mixin _$WebDavMultistatusXmlSerializableMixin {
void buildXmlChildren(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavMultistatusBuildXmlChildren(this as WebDavMultistatus, builder, namespaces: namespaces);
void buildXmlElement(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavMultistatusBuildXmlElement(this as WebDavMultistatus, builder, namespaces: namespaces);
List<XmlAttribute> toXmlAttributes({Map<String, String?> namespaces = const {}}) =>
_$WebDavMultistatusToXmlAttributes(this as WebDavMultistatus, namespaces: namespaces);
List<XmlNode> toXmlChildren({Map<String, String?> namespaces = const {}}) =>
_$WebDavMultistatusToXmlChildren(this as WebDavMultistatus, namespaces: namespaces);
XmlElement toXmlElement({Map<String, String?> namespaces = const {}}) =>
_$WebDavMultistatusToXmlElement(this as WebDavMultistatus, namespaces: namespaces);
}
void _$WebDavResponseBuildXmlChildren(WebDavResponse instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final href = instance.href;
final hrefSerialized = href;
builder.element('href', namespace: 'DAV:', nest: () {
if (hrefSerialized != null) {
builder.text(hrefSerialized);
}
});
final propstats = instance.propstats;
final propstatsSerialized = propstats;
for (final value in propstatsSerialized) {
builder.element('propstat', namespace: 'DAV:', nest: () {
value.buildXmlChildren(builder, namespaces: namespaces);
});
}
}
void _$WebDavResponseBuildXmlElement(WebDavResponse instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
builder.element('response', namespace: 'DAV:', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
WebDavResponse _$WebDavResponseFromXmlElement(XmlElement element) {
final href = element.getElement('href', namespace: 'DAV:')?.getText();
final propstats = element.getElements('propstat', namespace: 'DAV:')!;
return WebDavResponse(href: href, propstats: propstats.map((e) => WebDavPropstat.fromXmlElement(e)).toList());
}
List<XmlAttribute> _$WebDavResponseToXmlAttributes(WebDavResponse instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
return attributes;
}
List<XmlNode> _$WebDavResponseToXmlChildren(WebDavResponse instance, {Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final href = instance.href;
final hrefSerialized = href;
final hrefConstructed =
XmlElement(XmlName('href', namespaces['DAV:']), [], hrefSerialized != null ? [XmlText(hrefSerialized)] : []);
children.add(hrefConstructed);
final propstats = instance.propstats;
final propstatsSerialized = propstats;
final propstatsConstructed = propstatsSerialized.map((e) => XmlElement(XmlName('propstat', namespaces['DAV:']),
e.toXmlAttributes(namespaces: namespaces), e.toXmlChildren(namespaces: namespaces)));
children.addAll(propstatsConstructed);
return children;
}
XmlElement _$WebDavResponseToXmlElement(WebDavResponse instance, {Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('response', namespaces['DAV:']),
[...namespaces.toXmlAttributes(), ...instance.toXmlAttributes(namespaces: namespaces)],
instance.toXmlChildren(namespaces: namespaces));
}
mixin _$WebDavResponseXmlSerializableMixin {
void buildXmlChildren(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavResponseBuildXmlChildren(this as WebDavResponse, builder, namespaces: namespaces);
void buildXmlElement(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavResponseBuildXmlElement(this as WebDavResponse, builder, namespaces: namespaces);
List<XmlAttribute> toXmlAttributes({Map<String, String?> namespaces = const {}}) =>
_$WebDavResponseToXmlAttributes(this as WebDavResponse, namespaces: namespaces);
List<XmlNode> toXmlChildren({Map<String, String?> namespaces = const {}}) =>
_$WebDavResponseToXmlChildren(this as WebDavResponse, namespaces: namespaces);
XmlElement toXmlElement({Map<String, String?> namespaces = const {}}) =>
_$WebDavResponseToXmlElement(this as WebDavResponse, namespaces: namespaces);
}
void _$WebDavPropstatBuildXmlChildren(WebDavPropstat instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final status = instance.status;
final statusSerialized = status;
builder.element('status', namespace: 'DAV:', nest: () {
builder.text(statusSerialized);
});
final prop = instance.prop;
final propSerialized = prop;
builder.element('prop', namespace: 'DAV:', nest: () {
propSerialized.buildXmlChildren(builder, namespaces: namespaces);
});
}
void _$WebDavPropstatBuildXmlElement(WebDavPropstat instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
builder.element('propstat', namespace: 'DAV:', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
WebDavPropstat _$WebDavPropstatFromXmlElement(XmlElement element) {
final status = element.getElement('status', namespace: 'DAV:')!.getText()!;
final prop = element.getElement('prop', namespace: 'DAV:')!;
return WebDavPropstat(status: status, prop: WebDavProp.fromXmlElement(prop));
}
List<XmlAttribute> _$WebDavPropstatToXmlAttributes(WebDavPropstat instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
return attributes;
}
List<XmlNode> _$WebDavPropstatToXmlChildren(WebDavPropstat instance, {Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final status = instance.status;
final statusSerialized = status;
final statusConstructed = XmlElement(XmlName('status', namespaces['DAV:']), [], [XmlText(statusSerialized)]);
children.add(statusConstructed);
final prop = instance.prop;
final propSerialized = prop;
final propConstructed = XmlElement(XmlName('prop', namespaces['DAV:']),
propSerialized.toXmlAttributes(namespaces: namespaces), propSerialized.toXmlChildren(namespaces: namespaces));
children.add(propConstructed);
return children;
}
XmlElement _$WebDavPropstatToXmlElement(WebDavPropstat instance, {Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('propstat', namespaces['DAV:']),
[...namespaces.toXmlAttributes(), ...instance.toXmlAttributes(namespaces: namespaces)],
instance.toXmlChildren(namespaces: namespaces));
}
mixin _$WebDavPropstatXmlSerializableMixin {
void buildXmlChildren(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavPropstatBuildXmlChildren(this as WebDavPropstat, builder, namespaces: namespaces);
void buildXmlElement(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavPropstatBuildXmlElement(this as WebDavPropstat, builder, namespaces: namespaces);
List<XmlAttribute> toXmlAttributes({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropstatToXmlAttributes(this as WebDavPropstat, namespaces: namespaces);
List<XmlNode> toXmlChildren({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropstatToXmlChildren(this as WebDavPropstat, namespaces: namespaces);
XmlElement toXmlElement({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropstatToXmlElement(this as WebDavPropstat, namespaces: namespaces);
}
void _$WebDavPropertyupdateBuildXmlChildren(WebDavPropertyupdate instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final set = instance.set;
final setSerialized = set;
builder.element('set', namespace: 'DAV:', nest: () {
setSerialized.buildXmlChildren(builder, namespaces: namespaces);
});
}
void _$WebDavPropertyupdateBuildXmlElement(WebDavPropertyupdate instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
builder.element('propertyupdate', namespace: 'DAV:', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
WebDavPropertyupdate _$WebDavPropertyupdateFromXmlElement(XmlElement element) {
final set = element.getElement('set', namespace: 'DAV:')!;
return WebDavPropertyupdate(set: WebDavSet.fromXmlElement(set));
}
List<XmlAttribute> _$WebDavPropertyupdateToXmlAttributes(WebDavPropertyupdate instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
return attributes;
}
List<XmlNode> _$WebDavPropertyupdateToXmlChildren(WebDavPropertyupdate instance,
{Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final set = instance.set;
final setSerialized = set;
final setConstructed = XmlElement(XmlName('set', namespaces['DAV:']),
setSerialized.toXmlAttributes(namespaces: namespaces), setSerialized.toXmlChildren(namespaces: namespaces));
children.add(setConstructed);
return children;
}
XmlElement _$WebDavPropertyupdateToXmlElement(WebDavPropertyupdate instance,
{Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('propertyupdate', namespaces['DAV:']),
[...namespaces.toXmlAttributes(), ...instance.toXmlAttributes(namespaces: namespaces)],
instance.toXmlChildren(namespaces: namespaces));
}
mixin _$WebDavPropertyupdateXmlSerializableMixin {
void buildXmlChildren(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavPropertyupdateBuildXmlChildren(this as WebDavPropertyupdate, builder, namespaces: namespaces);
void buildXmlElement(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavPropertyupdateBuildXmlElement(this as WebDavPropertyupdate, builder, namespaces: namespaces);
List<XmlAttribute> toXmlAttributes({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropertyupdateToXmlAttributes(this as WebDavPropertyupdate, namespaces: namespaces);
List<XmlNode> toXmlChildren({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropertyupdateToXmlChildren(this as WebDavPropertyupdate, namespaces: namespaces);
XmlElement toXmlElement({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropertyupdateToXmlElement(this as WebDavPropertyupdate, namespaces: namespaces);
}
void _$WebDavSetBuildXmlChildren(WebDavSet instance, XmlBuilder builder, {Map<String, String> namespaces = const {}}) {
final prop = instance.prop;
final propSerialized = prop;
builder.element('prop', namespace: 'DAV:', nest: () {
propSerialized.buildXmlChildren(builder, namespaces: namespaces);
});
}
void _$WebDavSetBuildXmlElement(WebDavSet instance, XmlBuilder builder, {Map<String, String> namespaces = const {}}) {
builder.element('propertyupdate', namespace: 'DAV:', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
WebDavSet _$WebDavSetFromXmlElement(XmlElement element) {
final prop = element.getElement('prop', namespace: 'DAV:')!;
return WebDavSet(prop: WebDavProp.fromXmlElement(prop));
}
List<XmlAttribute> _$WebDavSetToXmlAttributes(WebDavSet instance, {Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
return attributes;
}
List<XmlNode> _$WebDavSetToXmlChildren(WebDavSet instance, {Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final prop = instance.prop;
final propSerialized = prop;
final propConstructed = XmlElement(XmlName('prop', namespaces['DAV:']),
propSerialized.toXmlAttributes(namespaces: namespaces), propSerialized.toXmlChildren(namespaces: namespaces));
children.add(propConstructed);
return children;
}
XmlElement _$WebDavSetToXmlElement(WebDavSet instance, {Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('propertyupdate', namespaces['DAV:']),
[...namespaces.toXmlAttributes(), ...instance.toXmlAttributes(namespaces: namespaces)],
instance.toXmlChildren(namespaces: namespaces));
}
mixin _$WebDavSetXmlSerializableMixin {
void buildXmlChildren(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavSetBuildXmlChildren(this as WebDavSet, builder, namespaces: namespaces);
void buildXmlElement(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavSetBuildXmlElement(this as WebDavSet, builder, namespaces: namespaces);
List<XmlAttribute> toXmlAttributes({Map<String, String?> namespaces = const {}}) =>
_$WebDavSetToXmlAttributes(this as WebDavSet, namespaces: namespaces);
List<XmlNode> toXmlChildren({Map<String, String?> namespaces = const {}}) =>
_$WebDavSetToXmlChildren(this as WebDavSet, namespaces: namespaces);
XmlElement toXmlElement({Map<String, String?> namespaces = const {}}) =>
_$WebDavSetToXmlElement(this as WebDavSet, namespaces: namespaces);
}
void _$WebDavPropfindBuildXmlChildren(WebDavPropfind instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final prop = instance.prop;
final propSerialized = prop;
builder.element('prop', namespace: 'DAV:', nest: () {
propSerialized.buildXmlChildren(builder, namespaces: namespaces);
});
}
void _$WebDavPropfindBuildXmlElement(WebDavPropfind instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
builder.element('propfind', namespace: 'DAV:', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
WebDavPropfind _$WebDavPropfindFromXmlElement(XmlElement element) {
final prop = element.getElement('prop', namespace: 'DAV:')!;
return WebDavPropfind(prop: WebDavPropfindProp.fromXmlElement(prop));
}
List<XmlAttribute> _$WebDavPropfindToXmlAttributes(WebDavPropfind instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
return attributes;
}
List<XmlNode> _$WebDavPropfindToXmlChildren(WebDavPropfind instance, {Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final prop = instance.prop;
final propSerialized = prop;
final propConstructed = XmlElement(XmlName('prop', namespaces['DAV:']),
propSerialized.toXmlAttributes(namespaces: namespaces), propSerialized.toXmlChildren(namespaces: namespaces));
children.add(propConstructed);
return children;
}
XmlElement _$WebDavPropfindToXmlElement(WebDavPropfind instance, {Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('propfind', namespaces['DAV:']),
[...namespaces.toXmlAttributes(), ...instance.toXmlAttributes(namespaces: namespaces)],
instance.toXmlChildren(namespaces: namespaces));
}
mixin _$WebDavPropfindXmlSerializableMixin {
void buildXmlChildren(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavPropfindBuildXmlChildren(this as WebDavPropfind, builder, namespaces: namespaces);
void buildXmlElement(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavPropfindBuildXmlElement(this as WebDavPropfind, builder, namespaces: namespaces);
List<XmlAttribute> toXmlAttributes({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropfindToXmlAttributes(this as WebDavPropfind, namespaces: namespaces);
List<XmlNode> toXmlChildren({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropfindToXmlChildren(this as WebDavPropfind, namespaces: namespaces);
XmlElement toXmlElement({Map<String, String?> namespaces = const {}}) =>
_$WebDavPropfindToXmlElement(this as WebDavPropfind, namespaces: namespaces);
}
void _$WebDavOcFilterFilesBuildXmlChildren(WebDavOcFilterFiles instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final filterRules = instance.filterRules;
final filterRulesSerialized = filterRules;
builder.element('filter-rules', namespace: 'http://owncloud.org/ns', nest: () {
filterRulesSerialized.buildXmlChildren(builder, namespaces: namespaces);
});
final prop = instance.prop;
final propSerialized = prop;
builder.element('prop', namespace: 'DAV:', nest: () {
propSerialized.buildXmlChildren(builder, namespaces: namespaces);
});
}
void _$WebDavOcFilterFilesBuildXmlElement(WebDavOcFilterFiles instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
builder.element('filter-files', namespace: 'http://owncloud.org/ns', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
WebDavOcFilterFiles _$WebDavOcFilterFilesFromXmlElement(XmlElement element) {
final filterRules = element.getElement('filter-rules', namespace: 'http://owncloud.org/ns')!;
final prop = element.getElement('prop', namespace: 'DAV:')!;
return WebDavOcFilterFiles(
filterRules: WebDavOcFilterRules.fromXmlElement(filterRules), prop: WebDavPropfindProp.fromXmlElement(prop));
}
List<XmlAttribute> _$WebDavOcFilterFilesToXmlAttributes(WebDavOcFilterFiles instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
return attributes;
}
List<XmlNode> _$WebDavOcFilterFilesToXmlChildren(WebDavOcFilterFiles instance,
{Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final filterRules = instance.filterRules;
final filterRulesSerialized = filterRules;
final filterRulesConstructed = XmlElement(
XmlName('filter-rules', namespaces['http://owncloud.org/ns']),
filterRulesSerialized.toXmlAttributes(namespaces: namespaces),
filterRulesSerialized.toXmlChildren(namespaces: namespaces));
children.add(filterRulesConstructed);
final prop = instance.prop;
final propSerialized = prop;
final propConstructed = XmlElement(XmlName('prop', namespaces['DAV:']),
propSerialized.toXmlAttributes(namespaces: namespaces), propSerialized.toXmlChildren(namespaces: namespaces));
children.add(propConstructed);
return children;
}
XmlElement _$WebDavOcFilterFilesToXmlElement(WebDavOcFilterFiles instance,
{Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('filter-files', namespaces['http://owncloud.org/ns']),
[...namespaces.toXmlAttributes(), ...instance.toXmlAttributes(namespaces: namespaces)],
instance.toXmlChildren(namespaces: namespaces));
}
mixin _$WebDavOcFilterFilesXmlSerializableMixin {
void buildXmlChildren(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavOcFilterFilesBuildXmlChildren(this as WebDavOcFilterFiles, builder, namespaces: namespaces);
void buildXmlElement(XmlBuilder builder, {Map<String, String> namespaces = const {}}) =>
_$WebDavOcFilterFilesBuildXmlElement(this as WebDavOcFilterFiles, builder, namespaces: namespaces);
List<XmlAttribute> toXmlAttributes({Map<String, String?> namespaces = const {}}) =>
_$WebDavOcFilterFilesToXmlAttributes(this as WebDavOcFilterFiles, namespaces: namespaces);
List<XmlNode> toXmlChildren({Map<String, String?> namespaces = const {}}) =>
_$WebDavOcFilterFilesToXmlChildren(this as WebDavOcFilterFiles, namespaces: namespaces);
XmlElement toXmlElement({Map<String, String?> namespaces = const {}}) =>
_$WebDavOcFilterFilesToXmlElement(this as WebDavOcFilterFiles, namespaces: namespaces);
}

2
packages/nextcloud/pubspec.yaml

@ -12,6 +12,7 @@ dependencies:
json_annotation: ^4.7.0
version: ^3.0.2
xml: ^6.1.0
xml_annotation: ^2.2.0
dev_dependencies:
build_runner: ^2.2.1
@ -25,3 +26,4 @@ dev_dependencies:
ref: 0b2ee0d
process_run: ^0.12.5+2
test: ^1.16.0
xml_serializable: ^2.2.2

295
packages/nextcloud/test/webdav.dart

@ -36,21 +36,23 @@ Future run(final DockerImage image) async {
});
test('List directory', () async {
final files = await client.webdav.ls(
final responses = (await client.webdav.ls(
'/',
props: {
WebDavProps.ncHasPreview.name,
WebDavProps.davContentType.name,
WebDavProps.davLastModified.name,
WebDavProps.ocSize.name,
},
);
expect(files, hasLength(8));
final file = files.singleWhere((final f) => f.name == 'Nextcloud.png');
expect(file.hasPreview, isTrue);
expect(file.mimeType, 'image/png');
expect(file.lastModified!.isBefore(DateTime.now()), isTrue);
expect(file.size, 50598);
prop: WebDavPropfindProp(
nchaspreview: true,
davgetcontenttype: true,
davgetlastmodified: true,
ocsize: true,
),
))
.responses;
expect(responses, hasLength(9));
final props =
responses.singleWhere((final response) => response.href!.endsWith('/Nextcloud.png')).propstats.first.prop;
expect(props.nchaspreview, isTrue);
expect(props.davgetcontenttype, 'image/png');
expect(webdavDateFormat.parseUtc(props.davgetlastmodified!).isBefore(DateTime.now()), isTrue);
expect(props.ocsize, 50598);
});
test('Create directory', () async {
@ -62,9 +64,9 @@ Future run(final DockerImage image) async {
final response = await client.webdav.mkdirs('test/bla');
expect(response!.statusCode, equals(201));
final files = await client.webdav.ls('/test');
expect(files, hasLength(1));
expect(files[0].path, '/test/bla/');
final responses = (await client.webdav.ls('/test')).responses;
expect(responses, hasLength(2));
expect(responses[1].href, endsWith('/test/bla/'));
});
test('Upload files', () async {
@ -77,38 +79,57 @@ Future run(final DockerImage image) async {
response = await client.webdav.upload(txtBytes, 'test.txt');
expect(response.statusCode, equals(201));
final files = await client.webdav.ls(
final responses = (await client.webdav.ls(
'/',
props: {
WebDavProps.ocSize.name,
},
prop: WebDavPropfindProp(
ocsize: true,
),
))
.responses;
expect(responses, hasLength(11));
expect(
responses.singleWhere((final response) => response.href!.endsWith('/test.png')).propstats.first.prop.ocsize,
pngBytes.lengthInBytes,
);
expect(
responses.singleWhere((final response) => response.href!.endsWith('/test.txt')).propstats.first.prop.ocsize,
txtBytes.lengthInBytes,
);
expect(files, hasLength(10));
final pngFile = files.singleWhere((final f) => f.name == 'test.png');
final txtFile = files.singleWhere((final f) => f.name == 'test.txt');
expect(pngFile.size, pngBytes.lengthInBytes);
expect(txtFile.size, txtBytes.lengthInBytes);
});
test('Upload file with modified time', () async {
test('Upload file', () async {
final lastModified = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch ~/ 1000 * 1000);
final created = lastModified.subtract(const Duration(hours: 1));
final txtBytes = File('test/files/test.txt').readAsBytesSync();
final response = await client.webdav.upload(
txtBytes,
'test.txt',
lastModified: lastModified,
created: created,
);
expect(response.statusCode, equals(201));
final files = await client.webdav.ls(
final props = (await client.webdav.ls(
'/',
props: {
WebDavProps.davLastModified.name,
},
prop: WebDavPropfindProp(
davgetlastmodified: true,
nccreationtime: true,
),
))
.responses
.singleWhere((final response) => response.href!.endsWith('/test.txt'))
.propstats
.first
.prop;
expect(
webdavDateFormat.parseUtc(props.davgetlastmodified!).millisecondsSinceEpoch,
lastModified.millisecondsSinceEpoch,
);
expect(
DateTime.fromMillisecondsSinceEpoch(props.nccreationtime! * 1000).millisecondsSinceEpoch,
created.millisecondsSinceEpoch,
);
final txtFile = files.singleWhere((final f) => f.name == 'test.txt');
expect(txtFile.lastModified!.millisecondsSinceEpoch, lastModified.millisecondsSinceEpoch);
});
test('Copy file', () async {
@ -117,9 +138,9 @@ Future run(final DockerImage image) async {
'test.png',
);
expect(response.statusCode, 201);
final files = await client.webdav.ls('/');
expect(files.where((final f) => f.name == 'Nextcloud.png'), hasLength(1));
expect(files.where((final f) => f.name == 'test.png'), hasLength(1));
final responses = (await client.webdav.ls('/')).responses;
expect(responses.where((final response) => response.href!.endsWith('/Nextcloud.png')), hasLength(1));
expect(responses.where((final response) => response.href!.endsWith('/test.png')), hasLength(1));
});
test('Copy file (overwrite fail)', () async {
@ -150,9 +171,9 @@ Future run(final DockerImage image) async {
'test.png',
);
expect(response.statusCode, 201);
final files = await client.webdav.ls('/');
expect(files.where((final f) => f.name == 'Nextcloud.png'), hasLength(0));
expect(files.where((final f) => f.name == 'test.png'), hasLength(1));
final responses = (await client.webdav.ls('/')).responses;
expect(responses.where((final response) => response.href!.endsWith('/Nextcloud.png')), hasLength(0));
expect(responses.where((final response) => response.href!.endsWith('/test.png')), hasLength(1));
});
test('Move file (overwrite fail)', () async {
@ -178,19 +199,69 @@ Future run(final DockerImage image) async {
});
test('Get file props', () async {
final file = await client.webdav.getProps(
final props = (await client.webdav.ls(
'Nextcloud.png',
props: {
WebDavProps.ncHasPreview.name,
WebDavProps.davContentType.name,
WebDavProps.davLastModified.name,
WebDavProps.ocSize.name,
},
);
expect(file.hasPreview, isTrue);
expect(file.mimeType, 'image/png');
expect(file.lastModified!.isBefore(DateTime.now()), isTrue);
expect(file.size, 50598);
prop: WebDavPropfindProp(
davgetlastmodified: true,
davgetetag: true,
davgetcontenttype: true,
davgetcontentlength: true,
ocid: true,
ocfileid: true,
ocfavorite: true,
occommentshref: true,
occommentscount: true,
occommentsunread: true,
ocdownloadurl: true,
ocownerid: true,
ocownerdisplayname: true,
ocsize: true,
ocpermissions: true,
ncnote: true,
ncdatafingerprint: true,
nchaspreview: true,
ncmounttype: true,
ncisencrypted: true,
ncmetadataetag: true,
ncuploadtime: true,
nccreationtime: true,
ncrichworkspace: true,
ocssharepermissions: true,
ocmsharepermissions: true,
),
depth: 0,
))
.responses
.single
.propstats
.first
.prop;
expect(webdavDateFormat.parseUtc(props.davgetlastmodified!).isBefore(DateTime.now()), isTrue);
expect(props.davgetetag, isNotEmpty);
expect(props.davgetcontenttype, 'image/png');
expect(props.davgetcontentlength, 50598);
expect(props.ocid, isNotEmpty);
expect(props.ocfileid, isNotEmpty);
expect(props.ocfavorite, 0);
expect(props.occommentshref, isNotEmpty);
expect(props.occommentscount, 0);
expect(props.occommentsunread, 0);
expect(props.ocdownloadurl, isNull);
expect(props.ocownerid, 'user1');
expect(props.ocownerdisplayname, 'User One');
expect(props.ocsize, 50598);
expect(props.ocpermissions, 'RGDNVW');
expect(props.ncnote, isNull);
expect(props.ncdatafingerprint, isNull);
expect(props.nchaspreview, isTrue);
expect(props.ncmounttype, isNull);
expect(props.ncisencrypted, isNull);
expect(props.ncmetadataetag, isNull);
expect(props.ncuploadtime, 0);
expect(props.nccreationtime, 0);
expect(props.ncrichworkspace, isNull);
expect(props.ocssharepermissions, 19);
expect(json.decode(props.ocmsharepermissions!), ['share', 'read', 'write']);
});
test('Get directory props', () async {
@ -198,90 +269,86 @@ Future run(final DockerImage image) async {
await client.webdav.mkdir('test');
await client.webdav.upload(data, 'test/test.txt');
final file = await client.webdav.getProps(
final props = (await client.webdav.ls(
'test',
props: {
WebDavProps.davResourceType.name,
WebDavProps.davContentType.name,
WebDavProps.davLastModified.name,
WebDavProps.ocSize.name,
},
);
expect(file.isDirectory, isTrue);
expect(file.name, 'test');
expect(file.mimeType, null);
expectDateInReasonableTimeRange(file.lastModified!, DateTime.now());
expect(file.size, data.lengthInBytes);
prop: WebDavPropfindProp(
davgetcontenttype: true,
davgetlastmodified: true,
ocsize: true,
),
depth: 0,
))
.responses
.single
.propstats
.first
.prop;
expect(props.davgetcontenttype, isNull);
expectDateInReasonableTimeRange(webdavDateFormat.parseUtc(props.davgetlastmodified!), DateTime.now());
expect(props.ocsize, data.lengthInBytes);
});
test('Filter files', () async {
final response = await client.webdav.upload(Uint8List.fromList(utf8.encode('test')), 'test.txt');
final id = response.headers['oc-fileid']!.first;
await client.webdav.updateProps('test.txt', {WebDavProps.ocFavorite.name: '1'});
await client.webdav.updateProps(
'test.txt',
WebDavProp(
ocfavorite: 1,
),
);
final files = await client.webdav.filter(
final responses = (await client.webdav.filter(
'/',
{
WebDavProps.ocFavorite.name: '1',
},
props: {
WebDavProps.ocId.name,
WebDavProps.ocFavorite.name,
},
);
expect(files, hasLength(1));
final file = files.singleWhere((final e) => e.name == 'test.txt');
expect(file.id, id);
expect(file.favorite, isTrue);
WebDavOcFilterRules(
ocfavorite: 1,
),
prop: WebDavPropfindProp(
ocid: true,
ocfavorite: true,
),
))
.responses;
expect(responses, hasLength(1));
final props =
responses.singleWhere((final response) => response.href!.endsWith('/test.txt')).propstats.first.prop;
expect(props.ocid, id);
expect(props.ocfavorite, 1);
});
test('Set properties', () async {
final createdDate = DateTime.utc(1971, 2);
final createdEpoch = createdDate.millisecondsSinceEpoch / 1000;
final createdEpoch = createdDate.millisecondsSinceEpoch ~/ 1000;
final uploadTime = DateTime.now();
await client.webdav.upload(Uint8List.fromList(utf8.encode('test')), 'test.txt');
final updated = await client.webdav.updateProps('test.txt', {
WebDavProps.ocFavorite.name: '1',
WebDavProps.ncCreationTime.name: '$createdEpoch',
});
expect(updated, isTrue);
final file = await client.webdav.getProps(
final updated = await client.webdav.updateProps(
'test.txt',
props: {
WebDavProps.ocFavorite.name,
WebDavProps.ncCreationTime.name,
WebDavProps.ncUploadTime.name,
},
WebDavProp(
ocfavorite: 1,
nccreationtime: createdEpoch,
),
);
expect(file.favorite, isTrue);
expect(file.createdDate!.isAtSameMomentAs(createdDate), isTrue);
expectDateInReasonableTimeRange(file.uploadedDate!, uploadTime);
});
test('Set custom properties', () async {
client.webdav.registerNamespace('http://example.com/ns', 'test');
await client.webdav.upload(Uint8List.fromList(utf8.encode('test')), 'test.txt');
final updated = await client.webdav.updateProps('test.txt', {
'test:custom': 'test-custom-prop-value',
});
expect(updated, isTrue);
final file = await client.webdav.getProps(
final props = (await client.webdav.ls(
'test.txt',
props: {
'test:custom',
},
);
expect(
file.getProp('test:custom')!.text,
'test-custom-prop-value',
);
prop: WebDavPropfindProp(
ocfavorite: true,
nccreationtime: true,
ncuploadtime: true,
),
depth: 0,
))
.responses
.single
.propstats
.first
.prop;
expect(props.ocfavorite, 1);
expect(DateTime.fromMillisecondsSinceEpoch(props.nccreationtime! * 1000).isAtSameMomentAs(createdDate), isTrue);
expectDateInReasonableTimeRange(DateTime.fromMillisecondsSinceEpoch(props.ncuploadtime! * 1000), uploadTime);
});
});
}

1
tool/generate-nextcloud.sh

@ -62,6 +62,7 @@ jq \
(
cd packages/nextcloud
rm -rf .dart_tool/build
fvm dart run nextcloud:generate_props
fvm dart pub run build_runner build --delete-conflicting-outputs
# For some reason we need to fix and format twice, otherwise not everything gets fixed
fvm dart fix --apply lib/src/nextcloud.openapi.dart

Loading…
Cancel
Save