TF-644 Add accessToken in download interactor attachments for web

This commit is contained in:
dab246
2022-06-13 18:01:06 +07:00
committed by Dat H. Pham
parent 4b91ce2d7c
commit 4a2addd77a
5 changed files with 90 additions and 37 deletions
@@ -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<bool> downloadFileForWeb(String downloadUrl, String filename, String basicAuth) async {
final headerParam = Map<String, String>();
headerParam[HttpHeaders.authorizationHeader] = basicAuth;
headerParam[HttpHeaders.acceptHeader] = DioClient.jmapHeader;
Future<bool> downloadFileForWeb(
String downloadUrl,
String filename,
String authentication) async {
try {
final headerParam = Map<String, String>();
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) {
@@ -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,
);
}
@@ -9,7 +9,7 @@ class DownloadAttachmentForWebSuccess extends UIState {
}
class DownloadAttachmentForWebFailure extends FeatureFailure {
final exception;
final dynamic exception;
DownloadAttachmentForWebFailure(this.exception);
@@ -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<Either<Failure, Success>> execute(Attachment attachment, AccountId accountId, String baseDownloadUrl,) async* {
Stream<Either<Failure, Success>> 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<Failure, Success>(DownloadAttachmentForWebSuccess());
} else {
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(null));
}
} catch (exception) {
log('DownloadAttachmentForWebInteractor::execute(): exception: $exception');
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(exception));
}
}
@@ -98,7 +98,8 @@ class EmailBindings extends BaseBindings {
Get.lazyPut(() => DownloadAttachmentForWebInteractor(
Get.find<EmailRepository>(),
Get.find<CredentialRepository>(),
));
Get.find<AccountRepository>(),
Get.find<AuthenticationOIDCRepository>()));
Get.lazyPut(() => RefreshTokenOIDCInteractor(
Get.find<AuthenticationOIDCRepository>(),
Get.find<AccountRepository>(),