TF-644 Add accessToken in download interactor attachments for web
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
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/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:dio/dio.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:http_parser/http_parser.dart';
|
import 'package:http_parser/http_parser.dart';
|
||||||
@@ -79,31 +82,40 @@ class DownloadManager {
|
|||||||
return streamController.stream.first;
|
return streamController.stream.first;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> downloadFileForWeb(String downloadUrl, String filename, String basicAuth) async {
|
Future<bool> downloadFileForWeb(
|
||||||
final headerParam = Map<String, String>();
|
String downloadUrl,
|
||||||
headerParam[HttpHeaders.authorizationHeader] = basicAuth;
|
String filename,
|
||||||
headerParam[HttpHeaders.acceptHeader] = DioClient.jmapHeader;
|
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) {
|
if (res.statusCode == 200) {
|
||||||
final blob = html.Blob([res.bodyBytes]);
|
final blob = html.Blob([res.bodyBytes]);
|
||||||
final url = html.Url.createObjectUrlFromBlob(blob);
|
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||||
final anchor = html.document.createElement('a') as html.AnchorElement
|
final anchor = html.document.createElement('a') as html.AnchorElement
|
||||||
..href = url
|
..href = url
|
||||||
..style.display = 'none'
|
..style.display = 'none'
|
||||||
..download = filename;
|
..download = filename;
|
||||||
html.document.body?.children.add(anchor);
|
html.document.body?.children.add(anchor);
|
||||||
|
|
||||||
anchor.click();
|
anchor.click();
|
||||||
|
|
||||||
html.document.body?.children.remove(anchor);
|
html.document.body?.children.remove(anchor);
|
||||||
html.Url.revokeObjectUrl(url);
|
html.Url.revokeObjectUrl(url);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch (exception) {
|
||||||
|
throw exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaType? _extractMediaTypeFromResponse(ResponseBody responseBody) {
|
MediaType? _extractMediaTypeFromResponse(ResponseBody responseBody) {
|
||||||
|
|||||||
@@ -218,8 +218,6 @@ class EmailAPI {
|
|||||||
? accountRequest.bearerToken
|
? accountRequest.bearerToken
|
||||||
: accountRequest.basicAuth;
|
: accountRequest.basicAuth;
|
||||||
|
|
||||||
log('EmailAPI::exportAttachment(): authentication: $authentication');
|
|
||||||
|
|
||||||
return _downloadManager.downloadFile(
|
return _downloadManager.downloadFile(
|
||||||
attachment.getDownloadUrl(baseDownloadUrl, accountId),
|
attachment.getDownloadUrl(baseDownloadUrl, accountId),
|
||||||
getTemporaryDirectory(),
|
getTemporaryDirectory(),
|
||||||
@@ -234,10 +232,14 @@ class EmailAPI {
|
|||||||
String baseDownloadUrl,
|
String baseDownloadUrl,
|
||||||
AccountRequest accountRequest,
|
AccountRequest accountRequest,
|
||||||
) async {
|
) async {
|
||||||
|
final authentication = accountRequest.authenticationType == AuthenticationType.oidc
|
||||||
|
? accountRequest.bearerToken
|
||||||
|
: accountRequest.basicAuth;
|
||||||
|
|
||||||
return _downloadManager.downloadFileForWeb(
|
return _downloadManager.downloadFileForWeb(
|
||||||
attachment.getDownloadUrl(baseDownloadUrl, accountId),
|
attachment.getDownloadUrl(baseDownloadUrl, accountId),
|
||||||
attachment.name ?? '',
|
attachment.name ?? '',
|
||||||
accountRequest.basicAuth,
|
authentication,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class DownloadAttachmentForWebSuccess extends UIState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DownloadAttachmentForWebFailure extends FeatureFailure {
|
class DownloadAttachmentForWebFailure extends FeatureFailure {
|
||||||
final exception;
|
final dynamic exception;
|
||||||
|
|
||||||
DownloadAttachmentForWebFailure(this.exception);
|
DownloadAttachmentForWebFailure(this.exception);
|
||||||
|
|
||||||
|
|||||||
@@ -1,41 +1,79 @@
|
|||||||
import 'dart:async';
|
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:dartz/dartz.dart';
|
||||||
import 'package:jmap_dart_client/jmap/account_id.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/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/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';
|
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||||
|
|
||||||
class DownloadAttachmentForWebInteractor {
|
class DownloadAttachmentForWebInteractor {
|
||||||
final EmailRepository emailRepository;
|
final EmailRepository emailRepository;
|
||||||
final CredentialRepository credentialRepository;
|
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 {
|
try {
|
||||||
final result = await Future.wait(
|
final currentAccount = await _accountRepository.getCurrentAccount();
|
||||||
[credentialRepository.getUserName(), credentialRepository.getPassword()],
|
|
||||||
eagerError: true
|
final result = await Future.wait([
|
||||||
).then((List responses) async {
|
if (currentAccount.authenticationType == AuthenticationType.oidc)
|
||||||
final accountRequest = AccountRequest(
|
_authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id)
|
||||||
userName: responses.first,
|
else
|
||||||
password: responses.last,
|
...[
|
||||||
authenticationType: AuthenticationType.basic);
|
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(
|
return await emailRepository.downloadAttachmentForWeb(
|
||||||
attachment,
|
attachment,
|
||||||
accountId,
|
accountId,
|
||||||
baseDownloadUrl,
|
baseDownloadUrl,
|
||||||
accountRequest);
|
accountRequest);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
yield Right<Failure, Success>(DownloadAttachmentForWebSuccess());
|
yield Right<Failure, Success>(DownloadAttachmentForWebSuccess());
|
||||||
} else {
|
} else {
|
||||||
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(null));
|
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(null));
|
||||||
}
|
}
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
|
log('DownloadAttachmentForWebInteractor::execute(): exception: $exception');
|
||||||
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(exception));
|
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(exception));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,8 @@ class EmailBindings extends BaseBindings {
|
|||||||
Get.lazyPut(() => DownloadAttachmentForWebInteractor(
|
Get.lazyPut(() => DownloadAttachmentForWebInteractor(
|
||||||
Get.find<EmailRepository>(),
|
Get.find<EmailRepository>(),
|
||||||
Get.find<CredentialRepository>(),
|
Get.find<CredentialRepository>(),
|
||||||
));
|
Get.find<AccountRepository>(),
|
||||||
|
Get.find<AuthenticationOIDCRepository>()));
|
||||||
Get.lazyPut(() => RefreshTokenOIDCInteractor(
|
Get.lazyPut(() => RefreshTokenOIDCInteractor(
|
||||||
Get.find<AuthenticationOIDCRepository>(),
|
Get.find<AuthenticationOIDCRepository>(),
|
||||||
Get.find<AccountRepository>(),
|
Get.find<AccountRepository>(),
|
||||||
|
|||||||
Reference in New Issue
Block a user