Skip to content

Commit

Permalink
Add permission checks for desktop OSs
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanas committed Apr 26, 2022
1 parent 7c57c39 commit 30e8120
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
14 changes: 8 additions & 6 deletions lib/views/shared/receive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:dart_wormhole_gui/views/shared/progress.dart';
import 'package:dart_wormhole_william/client/client.dart';
import 'package:dart_wormhole_william/client/native_client.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dart_wormhole_gui/views/shared/util.dart';
import 'package:path_provider/path_provider.dart';
Expand Down Expand Up @@ -96,12 +95,12 @@ abstract class ReceiveShared<T extends ReceiveState> extends State<T> {
return "$prefix.$suffix";
}

void defaultErrorHandler(Object error, Object stacktrace) {
void defaultErrorHandler(Object error) {
this.setState(() {
this.currentState = ReceiveScreenStates.ReceiveError;
this.error = error.toString();
this.errorMessage = "Failed to receive file: $error";
print("Error receiving file\n$error\n$stacktrace");
print("Error receiving file\n$error");

if (error is ClientError) {
switch (error.errorCode) {
Expand All @@ -121,8 +120,8 @@ abstract class ReceiveShared<T extends ReceiveState> extends State<T> {
}

Future<ReceiveFileResult> receive() async {
return await canWriteToFile().then((permissionStatus) async {
if (permissionStatus == PermissionStatus.granted) {
return await canWriteToDirectory(path!).then((canWrite) async {
if (canWrite) {
late final File tempFile;
this.setState(() {
isRequestingConnection = true;
Expand Down Expand Up @@ -167,7 +166,10 @@ abstract class ReceiveShared<T extends ReceiveState> extends State<T> {
return result;
}, onError: defaultErrorHandler);
} else {
return Future.error(Exception("Permission denied"));
final error =
Exception("Permission denied. Could not write to ${path!}");
defaultErrorHandler(error);
return Future.error(error);
}
});
}
Expand Down
30 changes: 14 additions & 16 deletions lib/views/shared/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,20 @@ abstract class SettingsShared<T extends SettingsState> extends State<T> {
}

void handleSelectFile() async {
await canWriteToFile().then((permissionStatus) async {
if (permissionStatus == PermissionStatus.granted) {
String? directory = await FilePicker.platform.getDirectoryPath();
if (directory == null) {
return;
}
if (await canWriteToDirectory(directory)) {
setState(() {
prefs?.setString(PATH, directory);
});
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
THE_APP_DOES_NOT_HAVE_THE_PREMISSION_TO_STORE_FILES_IN_THE_DIR),
));
}
String? directory = await FilePicker.platform.getDirectoryPath();
if (directory == null) {
return;
}
await canWriteToDirectory(directory).then((canWrite) async {
if (canWrite) {
setState(() {
prefs?.setString(PATH, directory);
});
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
THE_APP_DOES_NOT_HAVE_THE_PREMISSION_TO_STORE_FILES_IN_THE_DIR),
));
}
});
}
Expand Down
32 changes: 14 additions & 18 deletions lib/views/shared/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ extension BytesToReadableSize on int {
}
}

Future<PermissionStatus> canWriteToFile() async {
if (Platform.isAndroid) {
return await Permission.storage.request();
} else if (Platform.isLinux || Platform.isMacOS || Platform.isWindows) {
return PermissionStatus.granted;
} else {
print("Implement write checks for ${Platform()}");
return PermissionStatus.permanentlyDenied;
}
}

String nonExistingPathFor(String path) {
if (File(path).existsSync()) {
int i = 1;
Expand All @@ -65,13 +54,20 @@ String nonExistingPathFor(String path) {
}
}

canWriteToDirectory(String directory) async {
try {
String path = nonExistingPathFor('$directory/test');
await File(path).writeAsBytes([]);
await File(path).delete();
return true;
} catch (e) {
Future<bool> canWriteToDirectory(String directory) async {
if (Platform.isAndroid) {
return await Permission.storage.request() == PermissionStatus.granted;
} else if (Platform.isLinux || Platform.isMacOS || Platform.isWindows) {
try {
String path = nonExistingPathFor('$directory/test');
File(path).writeAsBytesSync([]);
File(path).deleteSync();
return true;
} catch (e) {
return false;
}
} else {
print("Implement write checks for ${Platform()}");
return false;
}
}

0 comments on commit 30e8120

Please sign in to comment.