TF-2245 Fix upload attachment when token expired on web

Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit 0b2ab3c6ff4213bccc064822e640cc40592a39e8)
This commit is contained in:
dab246
2023-10-24 11:22:38 +07:00
committed by Dat H. Pham
parent ebd9663a5c
commit 14047c9ccc
9 changed files with 89 additions and 41 deletions
@@ -10,6 +10,7 @@ import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart';
import 'package:get/get_connect/http/src/request/request.dart';
import 'package:model/email/attachment.dart';
import 'package:model/upload/file_info.dart';
import 'package:model/upload/upload_response.dart';
@@ -22,6 +23,12 @@ import 'package:worker_manager/worker_manager.dart' as worker;
class FileUploader {
static const String uploadAttachmentExtraKey = 'upload-attachment';
static const String platformExtraKey = 'platform';
static const String bytesExtraKey = 'bytes';
static const String typeExtraKey = 'type';
static const String sizeExtraKey = 'size';
final DioClient _dioClient;
final worker.Executor _isolateExecutor;
@@ -104,12 +111,23 @@ class FileUploader {
final headerParam = _dioClient.getHeaders();
headerParam[HttpHeaders.contentTypeHeader] = fileInfo.mimeType;
headerParam[HttpHeaders.contentLengthHeader] = fileInfo.fileSize;
final data = fileInfo.readStream;
final mapExtra = <String, dynamic>{
uploadAttachmentExtraKey: {
platformExtraKey: 'web',
bytesExtraKey: fileInfo.bytes,
typeExtraKey: fileInfo.mimeType,
sizeExtraKey: fileInfo.fileSize,
}
};
final resultJson = await _dioClient.post(
Uri.decodeFull(uploadUri.toString()),
options: Options(headers: headerParam),
data: data,
options: Options(
headers: headerParam,
extra: mapExtra
),
data: BodyBytesStream.fromBytes(fileInfo.bytes!),
cancelToken: cancelToken,
onSendProgress: (count, total) {
log('FileUploader::_handleUploadAttachmentActionOnWeb():onSendProgress: [${uploadId.id}] = $count');
@@ -1,9 +1,10 @@
import 'package:core/core.dart';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/platform_info.dart';
import 'package:dartz/dartz.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:model/model.dart';
import 'package:model/upload/file_info.dart';
import 'package:tmail_ui_user/features/upload/domain/state/local_file_picker_state.dart';
class LocalFilePickerInteractor {
@@ -12,14 +13,20 @@ class LocalFilePickerInteractor {
Stream<Either<Failure, Success>> execute({FileType fileType = FileType.any}) async* {
try {
final filesResult = await FilePicker.platform.pickFiles(type: fileType, allowMultiple: true, withReadStream: true);
final filesResult = await FilePicker.platform.pickFiles(
type: fileType,
allowMultiple: true,
withData: PlatformInfo.isWeb
);
if (filesResult != null && filesResult.files.isNotEmpty) {
final fileInfoResults = filesResult.files
.map((platformFile) => FileInfo(
platformFile.name,
kIsWeb ? '' : platformFile.path ?? '',
PlatformInfo.isWeb ? '' : platformFile.path ?? '',
platformFile.size,
readStream: platformFile.readStream)).toList();
bytes: PlatformInfo.isWeb ? platformFile.bytes : null
))
.toList();
yield Right<Failure, Success>(LocalFilePickerSuccess(fileInfoResults));
} else {
yield Left<Failure, Success>(LocalFilePickerCancel());
@@ -265,7 +265,6 @@ class UploadController extends BaseController {
}
void _handleUploadAttachmentsSuccess(SuccessAttachmentUploadState success) {
log('UploadController::_handleUploadAttachmentsSuccess(): $success');
if (currentContext != null && currentOverlayContext != null && _uploadingStateFiles.allSuccess) {
_appToast.showToastSuccessMessage(
currentOverlayContext!,