Skip to content

Commit

Permalink
TF-2401 Fix cannot upload svg image as signature
Browse files Browse the repository at this point in the history
Signed-off-by: dab246 <[email protected]>
  • Loading branch information
dab246 committed Dec 14, 2023
1 parent d534646 commit d3fbecb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
11 changes: 2 additions & 9 deletions core/lib/data/network/download/download_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:io';

import 'package:core/data/network/dio_client.dart';
import 'package:core/data/utils/compress_file_utils.dart';
import 'package:core/presentation/extensions/html_extension.dart';
import 'package:core/presentation/utils/html_transformer/html_utils.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:dio/dio.dart';
Expand Down Expand Up @@ -103,18 +103,11 @@ class DownloadClient {
var mimeType = entryParam['mimeType'];
final cid = entryParam['cid'];
var fileName = entryParam['fileName'];

final base64Data = base64Encode(bytesData);
if (mimeType.endsWith('svg')) {
mimeType = 'image/svg+xml';
}
if (!base64Data.endsWith('==')) {
base64Data.append('==');
}
if (fileName.contains('.')) {
fileName = fileName.split('.').first;
}
final base64Uri = '<img src="data:$mimeType;base64,$base64Data" alt="$fileName" id="cid:$cid" style="max-width: 100%" />';
final base64Uri = '<img src="${HtmlUtils.convertBase64ToImageResourceData(base64Data: base64Data, mimeType: mimeType)}" alt="$fileName" id="cid:$cid" style="max-width: 100%" />';
return base64Uri;
}
}
23 changes: 23 additions & 0 deletions core/lib/presentation/utils/html_transformer/html_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import 'package:core/presentation/extensions/html_extension.dart';
import 'package:core/presentation/utils/html_transformer/html_event_action.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -122,4 +124,25 @@ class HtmlUtils {
return '';
}
}

static String validateHtmlImageResourceMimeType(String mimeType) {
if (mimeType.endsWith('svg')) {
mimeType = 'image/svg+xml';
}
log('HtmlUtils::validateHtmlImageResourceMimeType:mimeType: $mimeType');
return mimeType;
}

static String convertBase64ToImageResourceData({
required String base64Data,
required String mimeType
}) {
mimeType = validateHtmlImageResourceMimeType(mimeType);
if (!base64Data.endsWith('==')) {
base64Data.append('==');
}
final imageResource = 'data:$mimeType;base64,$base64Data';
log('HtmlUtils::convertBase64ToImageResourceData:imageResource: $imageResource');
return imageResource;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:core/presentation/utils/html_transformer/html_utils.dart';
import 'package:core/utils/app_logger.dart';
import 'package:file_picker/file_picker.dart';
import 'package:rich_text_composer/rich_text_composer.dart';
Expand Down Expand Up @@ -40,7 +41,7 @@ class RichTextMobileTabletController extends BaseRichTextController {
final bytesData = await File(platformFile.path!).readAsBytes();
await htmlEditorApi?.insertImageData(
bytesData,
'image/${platformFile.extension}',
HtmlUtils.validateHtmlImageResourceMimeType('image/${platformFile.extension}'),
maxWidth: maxWidth
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
import 'dart:convert';

import 'package:core/presentation/extensions/color_extension.dart';
import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:core/presentation/utils/html_transformer/html_utils.dart';
import 'package:core/utils/app_logger.dart';
import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:html_editor_enhanced/html_editor.dart';
import 'package:tmail_ui_user/features/composer/presentation/controller/base_rich_text_controller.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/font_name_type.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/formatting_options_state.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/header_style_type.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/image_source.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/inline_image.dart';
import 'package:tmail_ui_user/features/composer/presentation/controller/base_rich_text_controller.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/font_name_type.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/order_list_type.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/paragraph_type.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/rich_text_style_type.dart';
Expand Down Expand Up @@ -288,7 +289,7 @@ class RichTextWebController extends BaseRichTextController {
if (platformFile.bytes != null) {
final base64Data = base64Encode(platformFile.bytes!);
editorController.insertHtml(
'<img src="data:image/${platformFile.extension};base64,$base64Data" data-filename="${platformFile.name}" alt="Image in my signature" style="max-width: 100%"/>'
'<img src="${HtmlUtils.convertBase64ToImageResourceData(base64Data: base64Data, mimeType: 'image/${platformFile.extension}')}" data-filename="${platformFile.name}" alt="Image in my signature" style="max-width: 100%"/>'
);
} else {
logError("RichTextWebController::insertImageAsBase64: bytes is null");
Expand Down

0 comments on commit d3fbecb

Please sign in to comment.