Browse Source

Merge pull request #605 from nextcloud/feature/webdav-put-stream-progress

feat(nextcloud): Support progress on webdav put stream
pull/607/head
Kate 1 year ago committed by GitHub
parent
commit
b22d76a725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      packages/nextcloud/lib/src/webdav/client.dart

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

@ -152,6 +152,7 @@ class WebDavClient {
/// [lastModified] sets the date when the file was last modified on the server. /// [lastModified] sets the date when the file was last modified on the server.
/// [created] sets the date when the file was created on the server. /// [created] sets the date when the file was created on the server.
/// [contentLength] sets the length of the [localData] that is uploaded. /// [contentLength] sets the length of the [localData] that is uploaded.
/// [onProgress] can be used to watch the upload progress. Possible values range from 0.0 to 1.0. [contentLength] needs to be set for it to work.
/// See http://www.webdav.org/specs/rfc2518.html#METHOD_PUT for more information. /// See http://www.webdav.org/specs/rfc2518.html#METHOD_PUT for more information.
Future<HttpClientResponse> putStream( Future<HttpClientResponse> putStream(
final Stream<Uint8List> localData, final Stream<Uint8List> localData,
@ -159,22 +160,32 @@ class WebDavClient {
final DateTime? lastModified, final DateTime? lastModified,
final DateTime? created, final DateTime? created,
final int? contentLength, final int? contentLength,
}) async => final Function(double progres)? onProgress,
_send( }) async {
'PUT', var uploaded = 0;
_constructPath(path), return _send(
dataStream: localData, 'PUT',
headers: _getUploadHeaders( _constructPath(path),
lastModified: lastModified, dataStream: contentLength != null
created: created, ? localData.map((final chunk) {
contentLength: contentLength, uploaded += chunk.length;
), onProgress?.call(uploaded / contentLength);
); return chunk;
})
: localData,
headers: _getUploadHeaders(
lastModified: lastModified,
created: created,
contentLength: contentLength,
),
);
}
/// Puts a new file at [path] with [file] as content. /// Puts a new file at [path] with [file] as content.
/// ///
/// [lastModified] sets the date when the file was last modified on the server. /// [lastModified] sets the date when the file was last modified on the server.
/// [created] sets the date when the file was created on the server. /// [created] sets the date when the file was created on the server.
/// [onProgress] can be used to watch the upload progress. Possible values range from 0.0 to 1.0.
/// See http://www.webdav.org/specs/rfc2518.html#METHOD_PUT for more information. /// See http://www.webdav.org/specs/rfc2518.html#METHOD_PUT for more information.
Future<HttpClientResponse> putFile( Future<HttpClientResponse> putFile(
final File file, final File file,
@ -183,20 +194,15 @@ class WebDavClient {
final DateTime? lastModified, final DateTime? lastModified,
final DateTime? created, final DateTime? created,
final Function(double progres)? onProgress, final Function(double progres)? onProgress,
}) async { }) async =>
var uploaded = 0; putStream(
return putStream( file.openRead().cast<Uint8List>(),
file.openRead().map((final chunk) { path,
uploaded += chunk.length; lastModified: lastModified,
onProgress?.call(uploaded / fileStat.size); created: created,
return chunk as Uint8List; contentLength: fileStat.size,
}), onProgress: onProgress,
path, );
lastModified: lastModified,
created: created,
contentLength: fileStat.size,
);
}
/// Gets the content of the file at [path]. /// Gets the content of the file at [path].
Future<Uint8List> get(final String path) async => (await getStream(path)).bodyBytes; Future<Uint8List> get(final String path) async => (await getStream(path)).bodyBytes;

Loading…
Cancel
Save