diff --git a/core/lib/data/network/download/download_manager.dart b/core/lib/data/network/download/download_manager.dart index 34ea38941..02c26bbe5 100644 --- a/core/lib/data/network/download/download_manager.dart +++ b/core/lib/data/network/download/download_manager.dart @@ -1,8 +1,11 @@ import 'dart:async'; import 'dart:io'; -import 'package:core/core.dart'; +import 'package:core/data/network/dio_client.dart'; +import 'package:core/data/network/download/download_client.dart'; import 'package:core/data/network/download/downloaded_response.dart'; +import 'package:core/domain/exceptions/download_file_exception.dart'; +import 'package:core/utils/app_logger.dart'; import 'package:dio/dio.dart'; import 'package:http/http.dart' as http; import 'package:http_parser/http_parser.dart'; @@ -79,31 +82,40 @@ class DownloadManager { return streamController.stream.first; } - Future downloadFileForWeb(String downloadUrl, String filename, String basicAuth) async { - final headerParam = Map(); - headerParam[HttpHeaders.authorizationHeader] = basicAuth; - headerParam[HttpHeaders.acceptHeader] = DioClient.jmapHeader; + Future downloadFileForWeb( + String downloadUrl, + String filename, + String authentication) async { + try { + final headerParam = Map(); + headerParam[HttpHeaders.authorizationHeader] = authentication; + headerParam[HttpHeaders.acceptHeader] = DioClient.jmapHeader; - http.Response res = await http.get(Uri.parse(downloadUrl), headers: headerParam); + http.Response res = await http.get( + Uri.parse(downloadUrl), + headers: headerParam); - if (res.statusCode == 200) { - final blob = html.Blob([res.bodyBytes]); - final url = html.Url.createObjectUrlFromBlob(blob); - final anchor = html.document.createElement('a') as html.AnchorElement - ..href = url - ..style.display = 'none' - ..download = filename; - html.document.body?.children.add(anchor); + if (res.statusCode == 200) { + final blob = html.Blob([res.bodyBytes]); + final url = html.Url.createObjectUrlFromBlob(blob); + final anchor = html.document.createElement('a') as html.AnchorElement + ..href = url + ..style.display = 'none' + ..download = filename; + html.document.body?.children.add(anchor); - anchor.click(); + anchor.click(); - html.document.body?.children.remove(anchor); - html.Url.revokeObjectUrl(url); + html.document.body?.children.remove(anchor); + html.Url.revokeObjectUrl(url); - return true; + return true; + } + + return false; + } catch (exception) { + throw exception; } - - return false; } MediaType? _extractMediaTypeFromResponse(ResponseBody responseBody) { diff --git a/lib/features/email/data/network/email_api.dart b/lib/features/email/data/network/email_api.dart index 919c33f13..3b342fda7 100644 --- a/lib/features/email/data/network/email_api.dart +++ b/lib/features/email/data/network/email_api.dart @@ -218,8 +218,6 @@ class EmailAPI { ? accountRequest.bearerToken : accountRequest.basicAuth; - log('EmailAPI::exportAttachment(): authentication: $authentication'); - return _downloadManager.downloadFile( attachment.getDownloadUrl(baseDownloadUrl, accountId), getTemporaryDirectory(), @@ -234,10 +232,14 @@ class EmailAPI { String baseDownloadUrl, AccountRequest accountRequest, ) async { + final authentication = accountRequest.authenticationType == AuthenticationType.oidc + ? accountRequest.bearerToken + : accountRequest.basicAuth; + return _downloadManager.downloadFileForWeb( attachment.getDownloadUrl(baseDownloadUrl, accountId), attachment.name ?? '', - accountRequest.basicAuth, + authentication, ); } diff --git a/lib/features/email/domain/state/download_attachment_for_web_state.dart b/lib/features/email/domain/state/download_attachment_for_web_state.dart index eb697f3ac..3c1cfbef9 100644 --- a/lib/features/email/domain/state/download_attachment_for_web_state.dart +++ b/lib/features/email/domain/state/download_attachment_for_web_state.dart @@ -9,7 +9,7 @@ class DownloadAttachmentForWebSuccess extends UIState { } class DownloadAttachmentForWebFailure extends FeatureFailure { - final exception; + final dynamic exception; DownloadAttachmentForWebFailure(this.exception); diff --git a/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart b/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart index 6c861db74..e0c66b2ef 100644 --- a/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart +++ b/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart @@ -1,41 +1,79 @@ import 'dart:async'; -import 'package:core/core.dart'; +import 'package:core/presentation/state/failure.dart'; +import 'package:core/presentation/state/success.dart'; +import 'package:core/utils/app_logger.dart'; import 'package:dartz/dartz.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; -import 'package:model/model.dart'; +import 'package:model/account/account_request.dart'; +import 'package:model/account/authentication_type.dart'; +import 'package:model/account/password.dart'; +import 'package:model/account/user_name.dart'; +import 'package:model/email/attachment.dart'; +import 'package:model/oidc/token_oidc.dart'; import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart'; import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart'; +import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart'; +import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart'; import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart'; class DownloadAttachmentForWebInteractor { final EmailRepository emailRepository; final CredentialRepository credentialRepository; + final AccountRepository _accountRepository; + final AuthenticationOIDCRepository _authenticationOIDCRepository; - DownloadAttachmentForWebInteractor(this.emailRepository, this.credentialRepository); + DownloadAttachmentForWebInteractor( + this.emailRepository, + this.credentialRepository, + this._accountRepository, + this._authenticationOIDCRepository); - Stream> execute(Attachment attachment, AccountId accountId, String baseDownloadUrl,) async* { + Stream> execute( + Attachment attachment, + AccountId accountId, + String baseDownloadUrl + ) async* { try { - final result = await Future.wait( - [credentialRepository.getUserName(), credentialRepository.getPassword()], - eagerError: true - ).then((List responses) async { - final accountRequest = AccountRequest( - userName: responses.first, - password: responses.last, - authenticationType: AuthenticationType.basic); + final currentAccount = await _accountRepository.getCurrentAccount(); + + final result = await Future.wait([ + if (currentAccount.authenticationType == AuthenticationType.oidc) + _authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id) + else + ...[ + credentialRepository.getUserName(), + credentialRepository.getPassword() + ] + ], eagerError: true).then((List responses) async { + AccountRequest accountRequest; + + if (currentAccount.authenticationType == AuthenticationType.oidc) { + final tokenOidc = responses.first as TokenOIDC; + accountRequest = AccountRequest( + token: tokenOidc.toToken(), + authenticationType: AuthenticationType.oidc); + } else { + accountRequest = AccountRequest( + userName: responses.first as UserName, + password: responses.last as Password, + authenticationType: AuthenticationType.basic); + } + return await emailRepository.downloadAttachmentForWeb( attachment, accountId, baseDownloadUrl, accountRequest); }); + if (result) { yield Right(DownloadAttachmentForWebSuccess()); } else { yield Left(DownloadAttachmentForWebFailure(null)); } } catch (exception) { + log('DownloadAttachmentForWebInteractor::execute(): exception: $exception'); yield Left(DownloadAttachmentForWebFailure(exception)); } } diff --git a/lib/features/email/presentation/email_bindings.dart b/lib/features/email/presentation/email_bindings.dart index 98b660306..f7192516a 100644 --- a/lib/features/email/presentation/email_bindings.dart +++ b/lib/features/email/presentation/email_bindings.dart @@ -98,7 +98,8 @@ class EmailBindings extends BaseBindings { Get.lazyPut(() => DownloadAttachmentForWebInteractor( Get.find(), Get.find(), - )); + Get.find(), + Get.find())); Get.lazyPut(() => RefreshTokenOIDCInteractor( Get.find(), Get.find(),