Browse Source

nextcloud: Support setting last modified time when uploading

pull/192/head
jld3103 2 years ago
parent
commit
658ad9f26b
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 16
      packages/nextcloud/lib/src/webdav/client.dart
  2. 21
      packages/nextcloud/test/webdav.dart

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

@ -175,19 +175,31 @@ class WebDavClient {
); );
/// upload a new file with [localData] as content to [remotePath] /// upload a new file with [localData] as content to [remotePath]
Future<HttpClientResponse> upload(final Uint8List localData, final String remotePath) => _send( Future<HttpClientResponse> upload(
final Uint8List localData,
final String remotePath, {
final DateTime? lastModified,
}) =>
_send(
'PUT', 'PUT',
_constructPath(remotePath), _constructPath(remotePath),
[200, 201, 204], [200, 201, 204],
data: Stream.value(localData), 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] /// upload a new file with [localData] as content to [remotePath]
Future<HttpClientResponse> uploadStream(final Stream<Uint8List> localData, final String remotePath) async => _send( Future<HttpClientResponse> uploadStream(
final Stream<Uint8List> localData,
final String remotePath, {
final DateTime? lastModified,
}) async =>
_send(
'PUT', 'PUT',
_constructPath(remotePath), _constructPath(remotePath),
[200, 201, 204], [200, 201, 204],
data: localData, data: localData,
headers: lastModified != null ? {'X-OC-Mtime': (lastModified.millisecondsSinceEpoch ~/ 1000).toString()} : null,
); );
/// download [remotePath] and store the response file contents to String /// download [remotePath] and store the response file contents to String

21
packages/nextcloud/test/webdav.dart

@ -89,6 +89,27 @@ Future run(final DockerImage image) async {
expect(txtFile.size, txtBytes.lengthInBytes); 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 { test('Copy file', () async {
final response = await client.webdav.copy( final response = await client.webdav.copy(
'Nextcloud.png', 'Nextcloud.png',

Loading…
Cancel
Save