Skip to content

Commit

Permalink
Revert "fix: adapt to new nextcloud api"
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Aug 23, 2023
1 parent 1355d28 commit 9966beb
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 44 deletions.
23 changes: 7 additions & 16 deletions lib/components/settings/nextcloud_profile.dart
Original file line number Diff line number Diff line change
@@ -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});
Expand All @@ -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;
}
Expand Down Expand Up @@ -76,25 +76,18 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
initialData: Prefs.lastStorageQuota.value,
builder: (BuildContext context, AsyncSnapshot<Quota?> 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 '),
],
);
},
Expand All @@ -108,10 +101,8 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
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';
Expand Down
22 changes: 11 additions & 11 deletions lib/data/nextcloud/file_syncer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebDavFile> 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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -186,7 +186,7 @@ abstract class FileSyncer {
}

// upload file
await webdav.put(
await webdav.upload(
localDataEncrypted,
filePathRemote,
lastModified: lastModified,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions lib/data/nextcloud/nextcloud_client_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ extension NextcloudClientExtension on NextcloudClient {
return NextcloudClient(
url.isNotEmpty ? url : defaultNextCloudUri,
loginName: username,
username: username,
password: ncPassword,
);
}

Future<Map<String, String>> 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 {};
}
Expand All @@ -48,8 +49,8 @@ extension NextcloudClientExtension on NextcloudClient {
Future<void> setConfig(Map<String, String> 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<String> loadEncryptionKey() async {
Expand Down Expand Up @@ -85,7 +86,7 @@ extension NextcloudClientExtension on NextcloudClient {

Future<String> getUsername() async {
try {
return (await provisioningApi.users.getCurrentUser()).ocs.data.id;
return (await provisioningApi.getCurrentUser()).ocs.data.id;
} catch (e) {
throw NcLoginFailure();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/data/prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand Down
1 change: 1 addition & 0 deletions lib/pages/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
12 changes: 6 additions & 6 deletions lib/pages/user/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class _NcLoginPageState extends State<NcLoginPage> {
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');
Expand Down Expand Up @@ -64,6 +64,7 @@ class _NcLoginPageState extends State<NcLoginPage> {
client = NextcloudClient(
loginDetails.url,
loginName: username,
username: username,
password: loginDetails.ncPassword,
);

Expand All @@ -88,11 +89,10 @@ class _NcLoginPageState extends State<NcLoginPage> {
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;

Expand Down
8 changes: 4 additions & 4 deletions test/nc_deletion_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/nc_quota_test.dart
Original file line number Diff line number Diff line change
@@ -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));
});
}

0 comments on commit 9966beb

Please sign in to comment.