TF-764 Fix inline images are not saved and shown successfully in Draft

This commit is contained in:
dab246
2022-08-02 14:30:39 +07:00
committed by Dat H. Pham
parent e08429d42c
commit fc83cae62b
6 changed files with 49 additions and 82 deletions
@@ -112,7 +112,7 @@ class DownloadClient {
if (fileName.contains('.')) { if (fileName.contains('.')) {
fileName = fileName.split('.').first; fileName = fileName.split('.').first;
} }
final base64Uri = '<img src="data:$mimeType;base64,$base64Data" alt="$fileName" id="$cid" style="max-width: 100%" />'; final base64Uri = '<img src="data:$mimeType;base64,$base64Data" alt="$fileName" id="cid:$cid" style="max-width: 100%" />';
return base64Uri; return base64Uri;
} }
} }
@@ -21,26 +21,22 @@ class ImageTransformer extends DomTransformer {
} }
) async { ) async {
final compressFileUtils = CompressFileUtils(); final compressFileUtils = CompressFileUtils();
final imageElements = document.getElementsByTagName('img'); final imageElements = document.querySelectorAll('img[src^="cid:"]');
log('ImageTransformer::process(): imageElements: ${imageElements.length}');
await Future.wait(imageElements.map((imageElement) async { await Future.wait(imageElements.map((imageElement) async {
imageElement.attributes['style'] = 'display: inline;max-width: 100%;height: auto;'; imageElement.attributes['style'] = 'display: inline;max-width: 100%;height: auto;';
final src = imageElement.attributes['src']; final src = imageElement.attributes['src'];
if (src != null log('ImageTransformer::process(): src: $src');
&& src.isNotEmpty final cid = src?.replaceFirst('cid:', '').trim();
&& src.startsWith('cid:') final urlDownloadCid = mapUrlDownloadCID?[cid];
&& mapUrlDownloadCID != null log('ImageTransformer::process(): urlDownloadCid: $urlDownloadCid');
) { if (urlDownloadCid?.isNotEmpty == true && dioClient != null) {
final cid = src.replaceFirst('cid:', '').trim(); final imgBase64Uri = await loadAsyncNetworkImageToBase64(
final cidUrlDownload = mapUrlDownloadCID[cid]; dioClient,
if (cidUrlDownload != null && cidUrlDownload.isNotEmpty && dioClient != null) { compressFileUtils,
final imgBase64Uri = await loadAsyncNetworkImageToBase64( urlDownloadCid!);
dioClient, if (imgBase64Uri.isNotEmpty) {
compressFileUtils, imageElement.attributes['src'] = imgBase64Uri;
cidUrlDownload);
if (imgBase64Uri.isNotEmpty) {
imageElement.attributes['src'] = imgBase64Uri;
}
} }
} }
})); }));
@@ -890,6 +890,8 @@ class ComposerController extends BaseController {
mailboxDashBoardController.consumeState( mailboxDashBoardController.consumeState(
_saveEmailAsDraftsInteractor.execute(accountId, newEmail)); _saveEmailAsDraftsInteractor.execute(accountId, newEmail));
} }
uploadController.clearInlineFileUploaded();
} }
} }
@@ -1,9 +1,14 @@
import 'package:collection/collection.dart';
import 'package:core/presentation/views/dialog/color_picker_dialog_builder.dart'; import 'package:core/presentation/views/dialog/color_picker_dialog_builder.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:model/email/attachment.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart';
import 'package:html/parser.dart' show parse;
abstract class BaseRichTextController extends GetxController { abstract class BaseRichTextController extends GetxController {
@@ -33,4 +38,32 @@ abstract class BaseRichTextController extends GetxController {
} }
).show(); ).show();
} }
Future<Tuple2<String, List<Attachment>>> refactorContentHasInlineImage(
String emailContent,
Map<String, Attachment> mapInlineAttachments
) async {
final document = parse(emailContent);
final listImgTag = document.querySelectorAll('img[src^="data:image/"][id^="cid:"]');
final listInlineAttachment = await Future.wait(listImgTag.map((imgTag) async {
final idImg = imgTag.attributes['id'];
final cid = idImg!.replaceFirst('cid:', '').trim();
log('BaseRichTextController::refactorContentHasInlineImage(): $cid');
imgTag.attributes['src'] = 'cid:$cid';
imgTag.attributes.remove('id');
return cid;
})).then((listCid) {
log('BaseRichTextController::refactorContentHasInlineImage(): $listCid');
final listInlineAttachment = listCid
.whereNotNull()
.map((cid) => mapInlineAttachments[cid])
.whereNotNull()
.toList();
return listInlineAttachment;
});
final newContent = document.body?.innerHtml ?? emailContent;
log('BaseRichTextController::refactorContentHasInlineImage(): $newContent');
log('BaseRichTextController::refactorContentHasInlineImage(): listInlineAttachment: $listInlineAttachment');
return Tuple2(newContent, listInlineAttachment);
}
} }
@@ -1,12 +1,8 @@
import 'package:collection/collection.dart';
import 'package:core/core.dart'; import 'package:core/core.dart';
import 'package:custom_pop_up_menu/custom_pop_up_menu.dart'; import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:dartz/dartz.dart';
import 'package:enough_html_editor/enough_html_editor.dart'; import 'package:enough_html_editor/enough_html_editor.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:html/parser.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/composer/presentation/controller/base_rich_text_controller.dart'; import 'package:tmail_ui_user/features/composer/presentation/controller/base_rich_text_controller.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/dropdown_menu_font_status.dart'; import 'package:tmail_ui_user/features/composer/presentation/model/dropdown_menu_font_status.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/header_style_type.dart'; import 'package:tmail_ui_user/features/composer/presentation/model/header_style_type.dart';
@@ -128,34 +124,6 @@ class RichTextMobileTabletController extends BaseRichTextController {
} }
} }
Future<Tuple2<String, List<Attachment>>> refactorContentHasInlineImage(
String emailContent,
Map<String, Attachment> mapInlineAttachments,
) async {
final document = parse(emailContent);
final listImgTag = document.querySelectorAll('img[src^="data:image/"]');
final listInlineAttachment =
await Future.wait(listImgTag.map((imgTag) async {
final cid = imgTag.attributes['id'];
log('RichTextMobileTabletController::refactorContentHasInlineImage(): cid: $cid');
imgTag.attributes['src'] = 'cid:$cid';
imgTag.attributes.remove('id');
return cid;
})).then((listCid) {
log('RichTextMobileTabletController::refactorContentHasInlineImage(): $listCid');
final listInlineAttachment = listCid
.whereNotNull()
.map((cid) => mapInlineAttachments[cid])
.whereNotNull()
.toList();
return listInlineAttachment;
});
final newContent = document.body?.innerHtml ?? emailContent;
log('RichTextMobileTabletController::refactorContentHasInlineImage(): $newContent');
log('RichTextMobileTabletController::refactorContentHasInlineImage(): listInlineAttachment: $listInlineAttachment');
return Tuple2(newContent, listInlineAttachment);
}
bool isTextStyleTypeSelected(RichTextStyleType richTextStyleType) { bool isTextStyleTypeSelected(RichTextStyleType richTextStyleType) {
return listTextStyleApply.contains(richTextStyleType); return listTextStyleApply.contains(richTextStyleType);
} }
@@ -1,14 +1,9 @@
import 'package:collection/collection.dart';
import 'package:custom_pop_up_menu/custom_pop_up_menu.dart'; import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:dartz/dartz.dart';
import 'package:html/parser.dart' show parse;
import 'package:core/utils/app_logger.dart'; import 'package:core/utils/app_logger.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:html_editor_enhanced/html_editor.dart'; import 'package:html_editor_enhanced/html_editor.dart';
import 'package:model/email/attachment.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/code_view_state.dart'; import 'package:tmail_ui_user/features/composer/presentation/model/code_view_state.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/dropdown_menu_font_status.dart'; import 'package:tmail_ui_user/features/composer/presentation/model/dropdown_menu_font_status.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/header_style_type.dart'; import 'package:tmail_ui_user/features/composer/presentation/model/header_style_type.dart';
@@ -164,33 +159,6 @@ class RichTextWebController extends BaseRichTextController {
} }
} }
Future<Tuple2<String, List<Attachment>>> refactorContentHasInlineImage(
String emailContent,
Map<String, Attachment> mapInlineAttachments
) async {
final document = parse(emailContent);
final listImgTag = document.querySelectorAll('img[src^="data:image/"]');
final listInlineAttachment = await Future.wait(listImgTag.map((imgTag) async {
final cid = imgTag.attributes['id'];
log('RichTextWebController::refactorContentHasInlineImage(): cid: $cid');
imgTag.attributes['src'] = 'cid:$cid';
imgTag.attributes.remove('id');
return cid;
})).then((listCid) {
log('RichTextWebController::refactorContentHasInlineImage(): $listCid');
final listInlineAttachment = listCid
.whereNotNull()
.map((cid) => mapInlineAttachments[cid])
.whereNotNull()
.toList();
return listInlineAttachment;
});
final newContent = document.body?.innerHtml ?? emailContent;
log('RichTextWebController::refactorContentHasInlineImage(): $newContent');
log('RichTextWebController::refactorContentHasInlineImage(): listInlineAttachment: $listInlineAttachment');
return Tuple2(newContent, listInlineAttachment);
}
void applyNewFontStyle(FontNameType? newFont) { void applyNewFontStyle(FontNameType? newFont) {
final fontSelected = newFont ?? FontNameType.sansSerif; final fontSelected = newFont ?? FontNameType.sansSerif;
selectedFontName.value = fontSelected; selectedFontName.value = fontSelected;