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. 30
      packages/nextcloud/lib/src/webdav/client.dart

30
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 {
var uploaded = 0;
return _send(
'PUT', 'PUT',
_constructPath(path), _constructPath(path),
dataStream: localData, dataStream: contentLength != null
? localData.map((final chunk) {
uploaded += chunk.length;
onProgress?.call(uploaded / contentLength);
return chunk;
})
: localData,
headers: _getUploadHeaders( headers: _getUploadHeaders(
lastModified: lastModified, lastModified: lastModified,
created: created, created: created,
contentLength: contentLength, 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) {
uploaded += chunk.length;
onProgress?.call(uploaded / fileStat.size);
return chunk as Uint8List;
}),
path, path,
lastModified: lastModified, lastModified: lastModified,
created: created, created: created,
contentLength: fileStat.size, contentLength: fileStat.size,
onProgress: onProgress,
); );
}
/// 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