Skip to content

Commit

Permalink
Implement authorization through rclone in android
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Jul 3, 2024
1 parent 3fc406c commit 0c0dc18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
3 changes: 1 addition & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ android {
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'

main {
java.srcDirs += 'src/main/kotlin'
jniLibs.srcDirs = ['lib']
}
}
Expand Down
71 changes: 37 additions & 34 deletions lib/src/home/services/rclone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import 'package:fpdart/fpdart.dart';
import 'package:path_provider/path_provider.dart';
import 'package:syncvault/errors.dart';
import 'package:syncvault/src/home/models/drive_provider_model.dart';
import 'package:syncvault/src/home/models/remote_folder_model.dart';
import 'package:syncvault/src/introduction/services/intro_service.dart';
import 'package:url_launcher/url_launcher_string.dart';

enum DriveProvider {
oneDrive('onedrive'),
Expand All @@ -19,53 +18,57 @@ enum DriveProvider {
}

class RCloneAuthService {
Future<RemoteFolderModel> check() async {
final rCloneExec = await IntroService().getRCloneExec();

final res = await Process.run(rCloneExec.path, ['authorize', 'drive']);
print(res.stdout);

return RemoteFolderModel(provider: DriveProvider.googleDrive);
}

TaskEither<AppError, DriveProviderModel> authorize(
{required DriveProvider driveProvider}) {
TaskEither<AppError, DriveProviderModel> authorize({
required DriveProvider driveProvider,
}) {
return TaskEither.tryCatch(() async {
// final rCloneExec = await IntroService().getRCloneExec();

// final ress = await Process.run(
// 'ls', ['-a', '/data/user/0/com.example.syncvault/'],
// runInShell: true);
// print(ress.stdout);
// print('hi');

const channel = MethodChannel('com.example.syncvault/native_lib');
final path = await channel.invokeMethod('getNativeLibraryPath');
print(path);

// final res = await Process.run('ls', ['-a', path], runInShell: true);
// print(res.stdout);
// print(res.stderr);

final res = await Process.run(
final process = await Process.start(
'$path/librclone.so',
['authorize', '--auth-no-open-browser', driveProvider.providerName],
runInShell: true,
);

print(res.stdout);
print(res.stderr);
final output = StringBuffer();
final errorOutput = StringBuffer();
final urlPattern = RegExp(
r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)');

// Listen to stdout, stderr
process.stdout.transform(utf8.decoder).listen((data) {
output.write(data);
});
process.stderr.transform(utf8.decoder).listen((data) {
errorOutput.write(data);

// Match url with regex (url is in stderr for whatever reason)
final match = urlPattern.firstMatch(data);
if (match != null) {
final url = match.group(0);
if (url != null) {
launchUrlString(url);
}
}
});

// Wait for process to finish
await process.exitCode;

final authJson = jsonDecode(RegExp(r'\{.+\}').stringMatch(res.stdout)!);
final authJson =
jsonDecode(RegExp(r'\{.+\}').stringMatch(output.toString())!);
if (authJson
case {
'access_token': String accessToken,
'refresh_token': String refreshToken,
'expiry': String expiresIn
'expiry': String expiresIn,
}) {
return DriveProviderModel(
accessToken: accessToken,
refreshToken: refreshToken,
expiresIn: expiresIn);
accessToken: accessToken,
refreshToken: refreshToken,
expiresIn: expiresIn,
);
} else {
throw const GeneralError('Authorization response invalid');
}
Expand Down

0 comments on commit 0c0dc18

Please sign in to comment.