DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Email/BaseFactory.cs

74 lines
2.9 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
2023-04-13 15:34:16 +05:00
using AsbCloudApp.Exceptions;
using Microsoft.Extensions.Configuration;
using System;
using System.Configuration;
using System.IO;
namespace AsbCloudInfrastructure.Services.Email
{
public class BaseFactory
{
protected readonly string platformName;
protected readonly string platformUrl;
protected readonly string companyName;
protected readonly string supportMail;
public BaseFactory(IConfiguration configuration)
{
platformName = configuration.GetValue<string>("email:platformName")
?? throw new ConfigurationErrorsException("email:platformName не определен");
platformUrl = configuration.GetValue<string>("email:platformUrl")
?? throw new ConfigurationErrorsException("email:platformUrl не определен");
companyName = configuration.GetValue<string>("email:companyName")
?? throw new ConfigurationErrorsException("email:companyName не определен");
supportMail = configuration.GetValue<string>("email:supportMail")
?? throw new ConfigurationErrorsException("email:supportMail не определен");
}
public static string GetOrEmptyImageBase64(string resourceFileName)
{
if (string.IsNullOrEmpty(resourceFileName))
2023-09-29 12:06:46 +05:00
throw new ArgumentInvalidException(nameof(resourceFileName), "ResourceFileName doesn`t exist");
2023-04-13 15:34:16 +05:00
var baseDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
?? string.Empty;
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 imageBytes = File.ReadAllBytes(logoFilePath);
var format = Path.GetExtension(resourceFileName).Trim('.');
return "data:image/" + format + ";base64," + Convert.ToBase64String(imageBytes);
}
public static string MakeHref(string url, string text)
=> $"<a href=\"{url}\">{text}</a>";
public string MakeSignatue()
{
var logo = GetOrEmptyImageBase64("logo_32.png");
var sign = $"<br><br>---<br><img src=\"{logo}\"/><br>" +
$"{companyName}<br>" +
$"Это письмо сформировано автоматически.<br>" +
$"Для получения помощи по работе на портале {platformName}" +
$"обращайтесь по адресу {supportMail}";
return sign;
}
public virtual string MakeSubject(WellDto well, string action)
=> $"{well.Deposit}, {well.Cluster}, {well.Caption}. {action}";
}
}