From 658ad9f26b64102e2c00d4de36d4dc45428d2aa9 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 6 Feb 2023 11:52:10 +0100 Subject: [PATCH] nextcloud: Support setting last modified time when uploading --- packages/nextcloud/lib/src/webdav/client.dart | 16 ++++++++++++-- packages/nextcloud/test/webdav.dart | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/nextcloud/lib/src/webdav/client.dart b/packages/nextcloud/lib/src/webdav/client.dart index f3e334ec..05d6f01c 100644 --- a/packages/nextcloud/lib/src/webdav/client.dart +++ b/packages/nextcloud/lib/src/webdav/client.dart @@ -175,19 +175,31 @@ class WebDavClient { ); /// upload a new file with [localData] as content to [remotePath] - Future upload(final Uint8List localData, final String remotePath) => _send( + Future upload( + final Uint8List localData, + final String remotePath, { + final DateTime? lastModified, + }) => + _send( 'PUT', _constructPath(remotePath), [200, 201, 204], data: Stream.value(localData), + headers: lastModified != null ? {'X-OC-Mtime': (lastModified.millisecondsSinceEpoch ~/ 1000).toString()} : null, ); /// upload a new file with [localData] as content to [remotePath] - Future uploadStream(final Stream localData, final String remotePath) async => _send( + Future uploadStream( + final Stream localData, + final String remotePath, { + final DateTime? lastModified, + }) async => + _send( 'PUT', _constructPath(remotePath), [200, 201, 204], data: localData, + headers: lastModified != null ? {'X-OC-Mtime': (lastModified.millisecondsSinceEpoch ~/ 1000).toString()} : null, ); /// download [remotePath] and store the response file contents to String diff --git a/packages/nextcloud/test/webdav.dart b/packages/nextcloud/test/webdav.dart index feb4787a..b4dde42b 100644 --- a/packages/nextcloud/test/webdav.dart +++ b/packages/nextcloud/test/webdav.dart @@ -89,6 +89,27 @@ Future run(final DockerImage image) async { expect(txtFile.size, txtBytes.lengthInBytes); }); + test('Upload file with modified time', () async { + final lastModified = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch ~/ 1000 * 1000); + final txtBytes = File('test/files/test.txt').readAsBytesSync(); + + final response = await client.webdav.upload( + txtBytes, + 'test.txt', + lastModified: lastModified, + ); + expect(response.statusCode, equals(201)); + + final files = await client.webdav.ls( + '/', + props: { + WebDavProps.davLastModified.name, + }, + ); + final txtFile = files.singleWhere((final f) => f.name == 'test.txt'); + expect(txtFile.lastModified!.millisecondsSinceEpoch, lastModified.millisecondsSinceEpoch); + }); + test('Copy file', () async { final response = await client.webdav.copy( 'Nextcloud.png',