TF-605 Use bearer token download attachment on Android

This commit is contained in:
dab246
2022-06-02 17:37:39 +07:00
committed by Dat H. Pham
parent 69b2dbf3cf
commit ba49d7699a
16 changed files with 243 additions and 14 deletions
+12 -1
View File
@@ -27,6 +27,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:tmail_ui_user/features/composer/domain/model/email_request.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/email/domain/model/move_to_mailbox_request.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
class EmailAPI {
@@ -160,6 +161,12 @@ class EmailAPI {
String baseDownloadUrl,
AccountRequest accountRequest
) async {
if (accountRequest.authenticationType == AuthenticationType.oidc &&
accountRequest.token?.isExpired == true &&
accountRequest.token?.refreshToken.isNotEmpty == true) {
throw DownloadAttachmentHasTokenExpiredException(accountRequest.token!.refreshToken);
}
String externalStorageDirPath;
if (Platform.isAndroid) {
externalStorageDirPath = await ExternalPath.getExternalStoragePublicDirectory(ExternalPath.DIRECTORY_DOWNLOADS);
@@ -169,12 +176,16 @@ class EmailAPI {
throw DeviceNotSupportedException();
}
final authentication = accountRequest.authenticationType == AuthenticationType.oidc
? accountRequest.bearerToken
: accountRequest.basicAuth;
final taskIds = await Future.wait(
attachments.map((attachment) async => await FlutterDownloader.enqueue(
url: attachment.getDownloadUrl(baseDownloadUrl, accountId),
savedDir: externalStorageDirPath,
headers: {
HttpHeaders.authorizationHeader: accountRequest.basicAuth,
HttpHeaders.authorizationHeader: authentication,
HttpHeaders.acceptHeader: DioClient.jmapHeader
},
fileName: attachment.name,