Browse Source

refactor(nextcloud): use avoid creating unnecessary streams

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/592/head
Nikolas Rimikis 1 year ago
parent
commit
52e6ef96f5
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 65
      packages/nextcloud/lib/src/webdav/client.dart

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

@ -22,9 +22,12 @@ class WebDavClient {
Future<HttpClientResponse> _send( Future<HttpClientResponse> _send(
final String method, final String method,
final String url, { final String url, {
final Stream<Uint8List>? data, final Stream<Uint8List>? dataStream,
final Uint8List? data,
final Map<String, String>? headers, final Map<String, String>? headers,
}) async { }) async {
assert(dataStream == null || data == null, 'Only one of dataStream or data can be specified.');
final request = await HttpClient().openUrl( final request = await HttpClient().openUrl(
method, method,
Uri.parse(url), Uri.parse(url),
@ -40,7 +43,10 @@ class WebDavClient {
} }
if (data != null) { if (data != null) {
await request.addStream(data); request.add(data);
}
if (dataStream != null) {
await request.addStream(dataStream);
} }
final response = await request.close(); final response = await request.close();
@ -130,12 +136,15 @@ class WebDavClient {
final DateTime? lastModified, final DateTime? lastModified,
final DateTime? created, final DateTime? created,
}) => }) =>
putStream( _send(
Stream.value(localData), 'PUT',
path, _constructPath(path),
lastModified: lastModified, data: localData,
created: created, headers: _getUploadHeaders(
contentLength: localData.lengthInBytes, lastModified: lastModified,
created: created,
contentLength: null,
),
); );
/// Puts a new file at [path] with [localData] as content. /// Puts a new file at [path] with [localData] as content.
@ -154,7 +163,7 @@ class WebDavClient {
_send( _send(
'PUT', 'PUT',
_constructPath(path), _constructPath(path),
data: localData, dataStream: localData,
headers: _getUploadHeaders( headers: _getUploadHeaders(
lastModified: lastModified, lastModified: lastModified,
created: created, created: created,
@ -238,13 +247,9 @@ class WebDavClient {
await _send( await _send(
'PROPFIND', 'PROPFIND',
_constructPath(path), _constructPath(path),
data: Stream.value( data: utf8.encode(
utf8.encode( WebDavPropfind(prop: prop ?? WebDavPropWithoutValues()).toXmlElement(namespaces: namespaces).toXmlString(),
WebDavPropfind(prop: prop ?? WebDavPropWithoutValues()) ) as Uint8List,
.toXmlElement(namespaces: namespaces)
.toXmlString(),
) as Uint8List,
),
headers: depth != null ? {'Depth': depth.value} : null, headers: depth != null ? {'Depth': depth.value} : null,
), ),
); );
@ -262,14 +267,12 @@ class WebDavClient {
await _send( await _send(
'REPORT', 'REPORT',
_constructPath(path), _constructPath(path),
data: Stream.value( data: utf8.encode(
utf8.encode( WebDavOcFilterFiles(
WebDavOcFilterFiles( filterRules: filterRules,
filterRules: filterRules, prop: prop ?? WebDavPropWithoutValues(), // coverage:ignore-line
prop: prop ?? WebDavPropWithoutValues(), // coverage:ignore-line ).toXmlElement(namespaces: namespaces).toXmlString(),
).toXmlElement(namespaces: namespaces).toXmlString(), ) as Uint8List,
) as Uint8List,
),
), ),
); );
@ -287,14 +290,12 @@ class WebDavClient {
final response = await _send( final response = await _send(
'PROPPATCH', 'PROPPATCH',
_constructPath(path), _constructPath(path),
data: Stream.value( data: utf8.encode(
utf8.encode( WebDavPropertyupdate(
WebDavPropertyupdate( set: set != null ? WebDavSet(prop: set) : null,
set: set != null ? WebDavSet(prop: set) : null, remove: remove != null ? WebDavRemove(prop: remove) : null,
remove: remove != null ? WebDavRemove(prop: remove) : null, ).toXmlElement(namespaces: namespaces).toXmlString(),
).toXmlElement(namespaces: namespaces).toXmlString(), ) as Uint8List,
) as Uint8List,
),
); );
final data = await _parseResponse(response); final data = await _parseResponse(response);
for (final a in data.responses) { for (final a in data.responses) {

Loading…
Cancel
Save