From 9966beb84391a6b16c7b7d8f44374419fe32ee4f Mon Sep 17 00:00:00 2001 From: Adil Hanney Date: Thu, 24 Aug 2023 00:27:07 +0100 Subject: [PATCH] Revert "fix: adapt to new nextcloud api" --- .../settings/nextcloud_profile.dart | 23 ++++--------- lib/data/nextcloud/file_syncer.dart | 22 ++++++------- .../nextcloud/nextcloud_client_extension.dart | 11 ++++--- lib/data/prefs.dart | 4 +-- lib/pages/editor/editor.dart | 1 + lib/pages/user/login.dart | 12 +++---- test/nc_deletion_test.dart | 8 ++--- test/nc_quota_test.dart | 32 +++++++++++++++++++ 8 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 test/nc_quota_test.dart diff --git a/lib/components/settings/nextcloud_profile.dart b/lib/components/settings/nextcloud_profile.dart index 643e13967..c9477ea90 100644 --- a/lib/components/settings/nextcloud_profile.dart +++ b/lib/components/settings/nextcloud_profile.dart @@ -1,12 +1,12 @@ import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; -import 'package:nextcloud/nextcloud.dart' show ProvisioningApiUserDetailsQuota; +import 'package:nextcloud/nextcloud.dart' show ProvisioningApiUserDetails_Quota; import 'package:saber/data/nextcloud/nextcloud_client_extension.dart'; import 'package:saber/data/prefs.dart'; import 'package:saber/data/routes.dart'; import 'package:saber/i18n/strings.g.dart'; -typedef Quota = ProvisioningApiUserDetailsQuota; +typedef Quota = ProvisioningApiUserDetails_Quota; class NextcloudProfile extends StatefulWidget { const NextcloudProfile({super.key}); @@ -19,7 +19,7 @@ class NextcloudProfile extends StatefulWidget { final client = NextcloudClientExtension.withSavedDetails(); if (client == null) return null; - final user = await client.provisioningApi.users.getCurrentUser(); + final user = await client.provisioningApi.getCurrentUser(); Prefs.lastStorageQuota.value = user.ocs.data.quota; return Prefs.lastStorageQuota.value; } @@ -76,25 +76,18 @@ class _NextcloudProfileState extends State { initialData: Prefs.lastStorageQuota.value, builder: (BuildContext context, AsyncSnapshot snapshot) { final Quota? quota = snapshot.data; - final double? relativePercent; - if (quota != null && quota.relative != null) { - relativePercent = quota.relative! / 100; - } else { - relativePercent = null; - } - return Stack( alignment: Alignment.center, children: [ CircularProgressIndicator( - value: relativePercent, + value: quota != null ? quota.relative / 100 : null, color: colorScheme.primary.withOpacity(0.5), backgroundColor: colorScheme.primary.withOpacity(0.1), strokeWidth: 8, semanticsLabel: 'Storage usage', semanticsValue: snapshot.data != null ? '${snapshot.data}%' : null, ), - Text('${readableBytes(quota?.used)} / ${readableBytes(quota?.total)}'), + Text(quota != null ? '${readableBytes(quota.used)} / ${readableBytes(quota.total)}' : ' ... B / ... B '), ], ); }, @@ -108,10 +101,8 @@ class _NextcloudProfileState extends State { super.dispose(); } - String readableBytes(num? bytes) { - if (bytes == null) { - return '... B'; - } else if (bytes < 1024) { + String readableBytes(int bytes) { + if (bytes < 1024) { return '$bytes B'; } else if (bytes < 1024 * 2) { // e.g. 1.5 KB return '${(bytes / 1024).toStringAsFixed(1)} KB'; diff --git a/lib/data/nextcloud/file_syncer.dart b/lib/data/nextcloud/file_syncer.dart index bf665cca6..5c1cb9aef 100644 --- a/lib/data/nextcloud/file_syncer.dart +++ b/lib/data/nextcloud/file_syncer.dart @@ -51,20 +51,20 @@ abstract class FileSyncer { uploadFileFromQueue(); - if (_client?.loginName != Prefs.username.value) _client = null; + if (_client?.username != Prefs.username.value) _client = null; _client ??= NextcloudClientExtension.withSavedDetails(); if (_client == null) return; // Get list of remote files from server List remoteFiles; try { - remoteFiles = await _client!.webdav.propfind( + remoteFiles = await _client!.webdav.ls( FileManager.appRootDirectoryPrefix, - prop: WebDavPropWithoutValues.fromBools( + prop: WebDavPropfindProp.fromBools( davgetcontentlength: true, davgetlastmodified: true, ), - ).then((multistatus) => multistatus.toWebDavFiles()); + ).then((multistatus) => multistatus.toWebDavFiles(_client!.webdav)); } on SocketException { // network error filesDone.value = filesDoneLimit; downloadCancellable.cancelled = true; @@ -121,7 +121,7 @@ abstract class FileSyncer { await _uploadQueue.waitUntilLoaded(); if (_uploadQueue.value.isEmpty) return; - if (_client?.loginName != Prefs.username.value) _client = null; + if (_client?.username != Prefs.username.value) _client = null; _client ??= NextcloudClientExtension.withSavedDetails(); if (_client == null) return; @@ -186,7 +186,7 @@ abstract class FileSyncer { } // upload file - await webdav.put( + await webdav.upload( localDataEncrypted, filePathRemote, lastModified: lastModified, @@ -293,7 +293,7 @@ abstract class FileSyncer { final Uint8List encryptedDataEncoded; try { - encryptedDataEncoded = await _client!.webdav.get(file.remotePath); + encryptedDataEncoded = await _client!.webdav.download(file.remotePath); } on DynamiteApiException { return false; } @@ -345,14 +345,14 @@ abstract class FileSyncer { // get remote file try { - file.webDavFile ??= await _client!.webdav.propfind( + file.webDavFile ??= await _client!.webdav.ls( file.remotePath, - depth: WebDavDepth.zero, - prop: WebDavPropWithoutValues.fromBools( + depth: '0', + prop: WebDavPropfindProp.fromBools( davgetlastmodified: true, davgetcontentlength: true, ), - ).then((multistatus) => multistatus.toWebDavFiles().single); + ).then((multistatus) => multistatus.toWebDavFiles(_client!.webdav).single); } catch (e) { // remote file doesn't exist; keep local return true; diff --git a/lib/data/nextcloud/nextcloud_client_extension.dart b/lib/data/nextcloud/nextcloud_client_extension.dart index f8b3b071d..b331fc473 100644 --- a/lib/data/nextcloud/nextcloud_client_extension.dart +++ b/lib/data/nextcloud/nextcloud_client_extension.dart @@ -29,15 +29,16 @@ extension NextcloudClientExtension on NextcloudClient { return NextcloudClient( url.isNotEmpty ? url : defaultNextCloudUri, loginName: username, + username: username, password: ncPassword, ); } Future> getConfig() async { final Uint8List file; - await webdav.mkcol(appRootDirectoryPrefix); + await webdav.mkdir(appRootDirectoryPrefix); try { - file = await webdav.get(configFilePath); + file = await webdav.download(configFilePath); } on DynamiteApiException { return {}; } @@ -48,8 +49,8 @@ extension NextcloudClientExtension on NextcloudClient { Future setConfig(Map config) async { String json = jsonEncode(config); Uint8List file = Uint8List.fromList(json.codeUnits); - await webdav.mkcol(appRootDirectoryPrefix); - await webdav.put(file, configFilePath); + await webdav.mkdir(appRootDirectoryPrefix); + await webdav.upload(file, configFilePath); } Future loadEncryptionKey() async { @@ -85,7 +86,7 @@ extension NextcloudClientExtension on NextcloudClient { Future getUsername() async { try { - return (await provisioningApi.users.getCurrentUser()).ocs.data.id; + return (await provisioningApi.getCurrentUser()).ocs.data.id; } catch (e) { throw NcLoginFailure(); } diff --git a/lib/data/prefs.dart b/lib/data/prefs.dart index 5c369f2e0..82f73032e 100644 --- a/lib/data/prefs.dart +++ b/lib/data/prefs.dart @@ -6,7 +6,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:logging/logging.dart'; -import 'package:nextcloud/nextcloud.dart' show ProvisioningApiUserDetailsQuota; +import 'package:nextcloud/nextcloud.dart' show ProvisioningApiUserDetails_Quota; import 'package:saber/components/canvas/_canvas_background_painter.dart'; import 'package:saber/components/navbar/responsive_navbar.dart'; import 'package:saber/data/flavor_config.dart'; @@ -15,7 +15,7 @@ import 'package:saber/data/tools/_tool.dart'; import 'package:saber/data/tools/stroke_properties.dart'; import 'package:shared_preferences/shared_preferences.dart'; -typedef Quota = ProvisioningApiUserDetailsQuota; +typedef Quota = ProvisioningApiUserDetails_Quota; abstract class Prefs { static final log = Logger('Prefs'); diff --git a/lib/pages/editor/editor.dart b/lib/pages/editor/editor.dart index 23ef92ee0..3ba3112a0 100644 --- a/lib/pages/editor/editor.dart +++ b/lib/pages/editor/editor.dart @@ -45,6 +45,7 @@ import 'package:saber/data/tools/select.dart'; import 'package:saber/i18n/strings.g.dart'; import 'package:saber/pages/home/whiteboard.dart'; import 'package:super_clipboard/super_clipboard.dart'; +import 'package:super_native_extensions/raw_clipboard.dart' show ReadProgress; import 'package:vector_math/vector_math_64.dart' show Vector3; typedef _PhotoInfo = ({Uint8List bytes, String extension}); diff --git a/lib/pages/user/login.dart b/lib/pages/user/login.dart index 4a812624c..ec0f8b5db 100644 --- a/lib/pages/user/login.dart +++ b/lib/pages/user/login.dart @@ -32,11 +32,11 @@ class _NcLoginPageState extends State { password: loginDetails.ncPassword, ); - final CoreOcsGetCapabilitiesResponse200ApplicationJson_Ocs_Data capabilities; + final CoreServerCapabilities_Ocs_Data capabilities; final bool ncServerIsSupported; final int ncSupportedVersion; try { - capabilities = await client.core.ocs.getCapabilities() + capabilities = await client.core.getCapabilities() .then((capabilities) => capabilities.ocs.data); (ncServerIsSupported, ncSupportedVersion) = client.core.isSupported(capabilities); log.info('ncServerIsSupported: $ncServerIsSupported, ncSupportedVersion: $ncSupportedVersion'); @@ -64,6 +64,7 @@ class _NcLoginPageState extends State { client = NextcloudClient( loginDetails.url, loginName: username, + username: username, password: loginDetails.ncPassword, ); @@ -88,11 +89,10 @@ class _NcLoginPageState extends State { Prefs.ncPassword.value = loginDetails.ncPassword; Prefs.pfp.value = null; - client.core.avatar.getAvatar(userId: username, size: 512) - .then((response) => response.data) + client.core.getAvatar(userId: username, size: 512) .then((Uint8List pfp) { - Prefs.pfp.value = pfp; - }); + Prefs.pfp.value = pfp; + }); Prefs.lastStorageQuota.value = null; diff --git a/test/nc_deletion_test.dart b/test/nc_deletion_test.dart index 978a45723..31bfe311e 100644 --- a/test/nc_deletion_test.dart +++ b/test/nc_deletion_test.dart @@ -53,8 +53,8 @@ void main() { // Check that the file exists on Nextcloud printOnFailure('Checking if $filePathRemote exists on Nextcloud'); - final webDavFiles = await webdav.propfind(filePathRemote, depth: WebDavDepth.zero) - .then((multistatus) => multistatus.toWebDavFiles()); + final webDavFiles = await webdav.ls(filePathRemote, depth: '0') + .then((multistatus) => multistatus.toWebDavFiles(webdav)); expect(webDavFiles.length, 1, reason: 'File should exist on Nextcloud'); // Delete the file @@ -65,9 +65,9 @@ void main() { await FileSyncer.uploadFileFromQueue(); // Check that the file is empty on Nextcloud - final webDavFile = await webdav.propfind(filePathRemote, depth: WebDavDepth.zero, prop: WebDavPropWithoutValues.fromBools( + final webDavFile = await webdav.ls(filePathRemote, depth: '0', prop: WebDavPropfindProp.fromBools( davgetcontentlength: true, - )).then((multistatus) => multistatus.toWebDavFiles().single); + )).then((multistatus) => multistatus.toWebDavFiles(webdav).single); expect(webDavFile.size, 0, reason: 'File should be empty on Nextcloud'); // Sync the file from Nextcloud diff --git a/test/nc_quota_test.dart b/test/nc_quota_test.dart new file mode 100644 index 000000000..3eb3f9609 --- /dev/null +++ b/test/nc_quota_test.dart @@ -0,0 +1,32 @@ +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:saber/components/settings/nextcloud_profile.dart'; +import 'package:saber/data/flavor_config.dart'; +import 'package:saber/data/prefs.dart'; + +void main() { + test('Test getting the nc storage quota', () async { + TestWidgetsFlutterBinding.ensureInitialized(); + HttpOverrides.global = null; // enable http requests in test + + FlavorConfig.setup(); + Prefs.testingMode = true; + Prefs.init(); + + Prefs.username.value = 'test.deletion'; + Prefs.ncPassword.value = 'PRmjb-NWLzz-Gisq5-TAbtj-RbpWP'; + Prefs.encPassword.value = 'test.deletion'; + + expect(Prefs.lastStorageQuota.value, isNull); + + final quota = await NextcloudProfile.getStorageQuota(); + expect(quota, isNotNull); + expect(quota!, Prefs.lastStorageQuota.value); + expect(quota.used, greaterThan(0)); + expect(quota.total, greaterThan(0)); + expect(quota.used, lessThan(quota.total)); + expect(quota.free, quota.total - quota.used); + expect(quota.relative, greaterThan(0)); + }); +}