2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Data;
|
2023-04-13 15:34:16 +05:00
|
|
|
using AsbCloudApp.Exceptions;
|
2022-09-08 12:05:56 +05:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using System;
|
2024-05-24 10:55:05 +05:00
|
|
|
using System.Configuration;
|
2022-09-08 12:05:56 +05:00
|
|
|
using System.IO;
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
namespace AsbCloudInfrastructure.Services.Email;
|
2023-04-18 16:22:53 +05:00
|
|
|
|
2022-09-08 12:05:56 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public class BaseFactory
|
|
|
|
{
|
|
|
|
protected readonly string platformName;
|
|
|
|
protected readonly string platformUrl;
|
|
|
|
protected readonly string companyName;
|
|
|
|
protected readonly string supportMail;
|
2024-05-24 10:55:05 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public BaseFactory(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
platformName = configuration.GetValue<string>("email:platformName")
|
|
|
|
?? throw new ConfigurationErrorsException("email:platformName не определен");
|
2024-05-24 10:55:05 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
platformUrl = configuration.GetValue<string>("email:platformUrl")
|
|
|
|
?? throw new ConfigurationErrorsException("email:platformUrl не определен");
|
2024-05-24 10:55:05 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
companyName = configuration.GetValue<string>("email:companyName")
|
|
|
|
?? throw new ConfigurationErrorsException("email:companyName не определен");
|
2022-09-08 12:05:56 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
supportMail = configuration.GetValue<string>("email:supportMail")
|
|
|
|
?? throw new ConfigurationErrorsException("email:supportMail не определен");
|
|
|
|
}
|
2022-09-08 12:05:56 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public static string GetOrEmptyImageBase64(string resourceFileName)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(resourceFileName))
|
|
|
|
throw new ArgumentInvalidException(nameof(resourceFileName), "ResourceFileName doesn`t exist");
|
2023-04-17 18:04:38 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
var baseDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
|
|
|
|
?? string.Empty;
|
|
|
|
var resourcesDir = "Res";
|
2023-04-17 18:04:38 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
var logoFilePath = Path.Combine(baseDir, resourcesDir, resourceFileName);
|
2022-09-08 12:05:56 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
if (!File.Exists(logoFilePath))
|
|
|
|
{
|
|
|
|
System.Diagnostics.Trace.TraceWarning($"GetOrEmptyImageBase64(). File {logoFilePath} not found.");
|
|
|
|
return string.Empty;
|
2022-09-08 12:05:56 +05:00
|
|
|
}
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
var imageBytes = File.ReadAllBytes(logoFilePath);
|
|
|
|
var format = Path.GetExtension(resourceFileName).Trim('.');
|
|
|
|
return "data:image/" + format + ";base64," + Convert.ToBase64String(imageBytes);
|
|
|
|
}
|
2022-09-08 12:05:56 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public static string MakeHref(string url, string text)
|
|
|
|
=> $"<a href=\"{url}\">{text}</a>";
|
2022-09-08 12:05:56 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
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;
|
2022-09-08 12:05:56 +05:00
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public virtual string MakeSubject(WellDto well, string action)
|
|
|
|
=> $"{well.Deposit}, {well.Cluster}, {well.Caption}. {action}";
|
2022-09-08 12:05:56 +05:00
|
|
|
}
|