Email.BaseFactory.GetImageBase64() Возвращает пустую строку если файл не найден.

This commit is contained in:
ngfrolov 2023-04-17 18:04:38 +05:00
parent 1289d782cf
commit fe29dae8f5
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7

View File

@ -22,16 +22,23 @@ namespace AsbCloudInfrastructure.Services.Email
supportMail = configuration.GetValue("email:supportMail", "support@digitaldrilling.ru"); supportMail = configuration.GetValue("email:supportMail", "support@digitaldrilling.ru");
} }
public static string GetImageBase64(string resourceFileName) public static string GetOrEmptyImageBase64(string resourceFileName)
{ {
if (string.IsNullOrEmpty(resourceFileName)) if (string.IsNullOrEmpty(resourceFileName))
throw new ArgumentInvalidException("ResourceFileName doesn`t exist", nameof(resourceFileName)); throw new ArgumentInvalidException("ResourceFileName doesn`t exist", nameof(resourceFileName));
var baseDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) var baseDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
?? string.Empty; ?? string.Empty;
var resoursesDir = "Res"; var resourcesDir = "Res";
var logoFilePath = Path.Combine(baseDir, resourcesDir, resourceFileName);
if (!File.Exists(logoFilePath))
{
System.Diagnostics.Trace.TraceWarning($"GetOrEmptyImageBase64(). File {logoFilePath} not found.");
return string.Empty;
}
var logoFilePath = Path.Combine(baseDir, resoursesDir, resourceFileName);
var imageBytes = File.ReadAllBytes(logoFilePath); var imageBytes = File.ReadAllBytes(logoFilePath);
var format = Path.GetExtension(resourceFileName).Trim('.'); var format = Path.GetExtension(resourceFileName).Trim('.');
return "data:image/" + format + ";base64," + Convert.ToBase64String(imageBytes); return "data:image/" + format + ";base64," + Convert.ToBase64String(imageBytes);
@ -42,7 +49,7 @@ namespace AsbCloudInfrastructure.Services.Email
public string MakeSignatue() public string MakeSignatue()
{ {
var logo = GetImageBase64("logo_32.png"); var logo = GetOrEmptyImageBase64("logo_32.png");
var sign = $"<br><br>---<br><img src=\"{logo}\"/><br>" + var sign = $"<br><br>---<br><img src=\"{logo}\"/><br>" +
$"{companyName}<br>" + $"{companyName}<br>" +
$"Это письмо сформировано автоматически.<br>" + $"Это письмо сформировано автоматически.<br>" +