TF-3514 Detect base64 image to transfer it to cid attachment
Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
@@ -18,7 +18,8 @@ abstract class HtmlDataSource {
|
||||
|
||||
Future<Tuple2<String, Set<EmailBodyPart>>> replaceImageBase64ToImageCID({
|
||||
required String emailContent,
|
||||
required Map<String, Attachment> inlineAttachments
|
||||
required Map<String, Attachment> inlineAttachments,
|
||||
required Uri? uploadUri,
|
||||
});
|
||||
|
||||
Future<String> removeCollapsedExpandedSignatureEffect({required String emailContent});
|
||||
|
||||
@@ -42,12 +42,14 @@ class HtmlDataSourceImpl extends HtmlDataSource {
|
||||
@override
|
||||
Future<Tuple2<String, Set<EmailBodyPart>>> replaceImageBase64ToImageCID({
|
||||
required String emailContent,
|
||||
required Map<String, Attachment> inlineAttachments
|
||||
required Map<String, Attachment> inlineAttachments,
|
||||
required Uri? uploadUri,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
return await _htmlAnalyzer.replaceImageBase64ToImageCID(
|
||||
emailContent: emailContent,
|
||||
inlineAttachments: inlineAttachments
|
||||
inlineAttachments: inlineAttachments,
|
||||
uploadUri: uploadUri,
|
||||
);
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:core/data/constants/constant.dart';
|
||||
import 'package:core/presentation/utils/html_transformer/html_transform.dart';
|
||||
@@ -5,6 +7,7 @@ import 'package:core/presentation/utils/html_transformer/text/persist_preformatt
|
||||
import 'package:core/presentation/utils/html_transformer/text/sanitize_autolink_html_transformers.dart';
|
||||
import 'package:core/presentation/utils/html_transformer/transform_configuration.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/string_convert.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:html/parser.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email_body_part.dart';
|
||||
@@ -12,13 +15,21 @@ import 'package:model/email/attachment.dart';
|
||||
import 'package:model/email/email_content.dart';
|
||||
import 'package:model/email/email_content_type.dart';
|
||||
import 'package:model/extensions/attachment_extension.dart';
|
||||
import 'package:model/upload/file_info.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/extensions/list_attachments_extension.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/model/event_action.dart';
|
||||
import 'package:tmail_ui_user/features/upload/data/network/file_uploader.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/model/upload_task_id.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class HtmlAnalyzer {
|
||||
static const String cidPrefixKey = 'cid:';
|
||||
|
||||
final HtmlTransform _htmlTransform;
|
||||
final FileUploader _fileUploader;
|
||||
final Uuid _uuid;
|
||||
|
||||
HtmlAnalyzer(this._htmlTransform);
|
||||
HtmlAnalyzer(this._htmlTransform, this._fileUploader, this._uuid);
|
||||
|
||||
Future<EmailContent> transformEmailContent(
|
||||
EmailContent emailContent,
|
||||
@@ -112,35 +123,71 @@ class HtmlAnalyzer {
|
||||
|
||||
Future<Tuple2<String, Set<EmailBodyPart>>> replaceImageBase64ToImageCID({
|
||||
required String emailContent,
|
||||
required Map<String, Attachment> inlineAttachments
|
||||
required Map<String, Attachment> inlineAttachments,
|
||||
required Uri? uploadUri,
|
||||
}) async {
|
||||
final document = parse(emailContent);
|
||||
final listImgTag = document.querySelectorAll('img[src^="data:image/"][id^="cid:"]');
|
||||
final listImgTag = document.querySelectorAll('img[src^="data:image/"]');
|
||||
log('HtmlAnalyzer::replaceImageBase64ToImageCID:listImgTagLength = ${listImgTag.length} | inlineAttachments = ${inlineAttachments.length}');
|
||||
|
||||
final listInlineAttachment = await Future.wait(listImgTag.map((imgTag) async {
|
||||
final idImg = imgTag.attributes['id'];
|
||||
final cid = idImg!.replaceFirst('cid:', '').trim();
|
||||
imgTag.attributes['src'] = 'cid:$cid';
|
||||
imgTag.attributes.remove('id');
|
||||
return cid;
|
||||
})).then((listCid) {
|
||||
final listInlineAttachment = listCid
|
||||
.map((cid) {
|
||||
if (inlineAttachments.containsKey(cid)) {
|
||||
return inlineAttachments[cid]!.toEmailBodyPart(charset: Constant.base64Charset);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.whereNotNull()
|
||||
.toSet();
|
||||
if (listImgTag.isEmpty) {
|
||||
return Tuple2(
|
||||
emailContent,
|
||||
inlineAttachments.isNotEmpty
|
||||
? inlineAttachments.values.toList().toEmailBodyPart(charset: Constant.base64Charset)
|
||||
: {},
|
||||
);
|
||||
}
|
||||
|
||||
return listInlineAttachment;
|
||||
});
|
||||
final Set<EmailBodyPart> inlineAttachmentsSet = {};
|
||||
final List<Future<void>> asyncTasks = [];
|
||||
|
||||
for (final imgTag in listImgTag) {
|
||||
late final LinkedHashMap<Object, String> attributes = imgTag.attributes;
|
||||
final idImg = attributes['id'];
|
||||
final imageSrc = attributes['src'];
|
||||
|
||||
if (imageSrc?.isEmpty ?? true) continue;
|
||||
|
||||
if (idImg?.startsWith(cidPrefixKey) == true) {
|
||||
final cid = idImg!.substring(cidPrefixKey.length).trim();
|
||||
attributes['src'] = '$cidPrefixKey$cid';
|
||||
attributes.remove('id');
|
||||
|
||||
final attachment = inlineAttachments[cid];
|
||||
log('HtmlAnalyzer::replaceImageBase64ToImageCID:attachment = $attachment');
|
||||
if (attachment != null) {
|
||||
inlineAttachmentsSet.add(attachment.toEmailBodyPart(charset: Constant.base64Charset));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (uploadUri == null) continue;
|
||||
|
||||
asyncTasks.add(_retrieveAttachmentFromUpload(
|
||||
uploadUri: uploadUri,
|
||||
base64ImageTag: imageSrc!,
|
||||
).then((newAttachment) {
|
||||
if (newAttachment == null) return;
|
||||
|
||||
final newInlineAttachment = newAttachment.toAttachmentWithDisposition(
|
||||
disposition: ContentDisposition.inline,
|
||||
cid: _uuid.v1(),
|
||||
);
|
||||
final newCid = newInlineAttachment.cid;
|
||||
inlineAttachments[newCid!] = newInlineAttachment;
|
||||
attributes['src'] = '$cidPrefixKey$newCid';
|
||||
|
||||
inlineAttachmentsSet.add(newInlineAttachment.toEmailBodyPart(charset: Constant.base64Charset));
|
||||
}));
|
||||
}
|
||||
|
||||
if (asyncTasks.isNotEmpty) {
|
||||
await Future.wait(asyncTasks);
|
||||
}
|
||||
|
||||
final newContent = document.body?.innerHtml ?? emailContent;
|
||||
|
||||
return Tuple2(newContent, listInlineAttachment);
|
||||
return Tuple2(newContent, inlineAttachmentsSet);
|
||||
}
|
||||
|
||||
Future<String> removeCollapsedExpandedSignatureEffect({required String emailContent}) async {
|
||||
@@ -162,4 +209,32 @@ class HtmlAnalyzer {
|
||||
log('HtmlAnalyzer::removeCollapsedExpandedSignatureEffect: AFTER = $newContent');
|
||||
return newContent;
|
||||
}
|
||||
|
||||
Future<Attachment?> _retrieveAttachmentFromUpload({
|
||||
required Uri uploadUri,
|
||||
required String base64ImageTag,
|
||||
}) async {
|
||||
try {
|
||||
final imageBytes = StringConvert.convertBase64ImageTagToBytes(base64ImageTag);
|
||||
final mediaType = StringConvert.getMediaTypeFromBase64ImageTag(base64ImageTag);
|
||||
log('HtmlAnalyzer::_retrieveAttachmentFromUpload: mimeType = ${mediaType?.mimeType} | imageBytesLength = ${imageBytes.length}');
|
||||
final generateId = _uuid.v1();
|
||||
final fileInfo = FileInfo.fromBytes(
|
||||
bytes: imageBytes,
|
||||
name: '$generateId.${mediaType?.subtype ?? 'png'}',
|
||||
type: mediaType?.mimeType,
|
||||
);
|
||||
|
||||
final attachment = await _fileUploader.uploadAttachment(
|
||||
UploadTaskId(generateId),
|
||||
fileInfo,
|
||||
uploadUri,
|
||||
);
|
||||
log('HtmlAnalyzer::_retrieveAttachmentFromUpload:Attachment = $attachment');
|
||||
return attachment;
|
||||
} catch (e) {
|
||||
logError('HtmlAnalyzer::_retrieveAttachmentFromUpload:Exception = $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ extension DetailedEmailHiveCacheExtension on DetailedEmailHiveCache {
|
||||
emailId: EmailId(Id(emailId)),
|
||||
createdTime: timeSaved,
|
||||
attachments: attachments?.toAttachment(),
|
||||
emailContentPath: emailContentPath,
|
||||
headers: headers?.toSetEmailHeader(),
|
||||
keywords: keywords != null
|
||||
? Map.fromIterables(keywords!.keys.map((value) => KeyWordIdentifier(value)), keywords!.values)
|
||||
|
||||
@@ -68,7 +68,7 @@ class GetEmailContentInteractor {
|
||||
additionalProperties: additionalProperties);
|
||||
final listAttachments = email.allAttachments.getListAttachmentsDisplayedOutside(email.htmlBodyAttachments);
|
||||
final listInlineImages = email.allAttachments.listAttachmentsDisplayedInContent;
|
||||
|
||||
log('GetEmailContentInteractor::_getContentEmailFromServer: listAttachments = ${listAttachments.length} | listInlineImages = ${listInlineImages.length}');
|
||||
if (email.emailContentList.isNotEmpty) {
|
||||
final mapCidImageDownloadUrl = listInlineImages.toMapCidImageDownloadUrl(
|
||||
accountId: accountId,
|
||||
@@ -109,8 +109,8 @@ class GetEmailContentInteractor {
|
||||
{Properties? additionalProperties}
|
||||
) async* {
|
||||
try {
|
||||
log('GetEmailContentInteractor::_getStoredOpenedEmail(): CALLED');
|
||||
final detailedEmail = await emailRepository.getStoredOpenedEmail(session, accountId, emailId);
|
||||
log('GetEmailContentInteractor::_getStoredOpenedEmail: attachments = ${detailedEmail.attachments?.length} | inlineImages = ${detailedEmail.inlineImages?.length}');
|
||||
yield Right<Failure, Success>(GetEmailContentFromCacheSuccess(
|
||||
htmlEmailContent: detailedEmail.htmlEmailContent ?? '',
|
||||
attachments: detailedEmail.attachments ?? [],
|
||||
@@ -144,8 +144,8 @@ class GetEmailContentInteractor {
|
||||
{Properties? additionalProperties}
|
||||
) async* {
|
||||
try {
|
||||
log('GetEmailContentInteractor::_getStoredNewEmail():CALLED');
|
||||
final detailedEmail = await emailRepository.getStoredNewEmail(session, accountId, emailId);
|
||||
log('GetEmailContentInteractor::_getStoredNewEmail: attachments = ${detailedEmail.attachments?.length} | inlineImages = ${detailedEmail.inlineImages?.length}');
|
||||
yield Right<Failure, Success>(GetEmailContentFromCacheSuccess(
|
||||
htmlEmailContent: detailedEmail.htmlEmailContent ?? '',
|
||||
attachments: detailedEmail.attachments ?? [],
|
||||
|
||||
@@ -686,6 +686,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
|
||||
emailId: currentEmail!.id!,
|
||||
createdTime: currentEmail?.receivedAt?.value ?? DateTime.now(),
|
||||
attachments: success.attachments,
|
||||
inlineImages: success.inlineImages,
|
||||
headers: success.emailCurrent?.headers,
|
||||
keywords: success.emailCurrent?.keywords,
|
||||
htmlEmailContent: success.htmlEmailContent,
|
||||
|
||||
Reference in New Issue
Block a user