Improved FileSystemServic, added methods for html partial templates
This commit is contained in:
parent
fd43ead960
commit
cf51a3b4ab
|
@ -7,49 +7,64 @@ public class FileSystemService(ILogger<FileSystemService> logger) {
|
|||
|
||||
private ILogger<FileSystemService> Logger { get; } = logger;
|
||||
|
||||
public async Task<string?> GetEmailTemplateAsync(string name, string? defaultTemplate = null) {
|
||||
public Task<string?> GetEmailTemplateAsync(string name, string? defaultTemplate = null) {
|
||||
string path = Path.Combine(ConfigurationDirectory, "templates", "email", name + ".mjml");
|
||||
|
||||
if (!File.Exists(path)) {
|
||||
try {
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
if (!string.IsNullOrWhiteSpace(defaultTemplate)) {
|
||||
await File.WriteAllTextAsync(path, defaultTemplate, Encoding.UTF8);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.LogError(ex, "File system access failed trying write default E-Mail template '{template}'", name);
|
||||
return defaultTemplate;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await File.ReadAllTextAsync(path, Encoding.UTF8);
|
||||
} catch (Exception ex) {
|
||||
Logger.LogError(ex, "File system access failed trying to retrieve E-Mail template '{template}'", name);
|
||||
return defaultTemplate;
|
||||
}
|
||||
return GetFileContentAsync(path, defaultTemplate);
|
||||
}
|
||||
|
||||
public string? GetEmailTemplate(string name, string? defaultTemplate = null) {
|
||||
string path = Path.Combine(ConfigurationDirectory, "templates", "email", name + ".mjml");
|
||||
return GetFileContent(path, defaultTemplate);
|
||||
}
|
||||
|
||||
public Task<string?> GetPartialTemplateAsync(string name, string? defaultTemplate = null) {
|
||||
string path = Path.Combine(ConfigurationDirectory, "templates", "partials", name + ".html");
|
||||
return GetFileContentAsync(path, defaultTemplate);
|
||||
}
|
||||
|
||||
public string? GetPartialTemplate(string name, string? defaultTemplate = null) {
|
||||
string path = Path.Combine(ConfigurationDirectory, "templates", "partials", name + ".html");
|
||||
return GetFileContent(path, defaultTemplate);
|
||||
}
|
||||
|
||||
private string? GetFileContent(string path, string? defaultContent = null) {
|
||||
if (!File.Exists(path)) {
|
||||
try {
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
if (!string.IsNullOrWhiteSpace(defaultTemplate)) {
|
||||
File.WriteAllText(path, defaultTemplate, Encoding.UTF8);
|
||||
if (!string.IsNullOrWhiteSpace(defaultContent)) {
|
||||
File.WriteAllText(path, defaultContent, Encoding.UTF8);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.LogError(ex, "File system access failed trying write default E-Mail template '{template}'", name);
|
||||
return defaultTemplate;
|
||||
Logger.LogError(ex, "File system access failed trying write default content of '{template}'", path);
|
||||
return defaultContent;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return File.ReadAllText(path, Encoding.UTF8);
|
||||
} catch (Exception ex) {
|
||||
Logger.LogError(ex, "File system access failed trying to retrieve E-Mail template '{template}'", name);
|
||||
return defaultTemplate;
|
||||
Logger.LogError(ex, "File system access failed trying to retrieve content of '{template}'", path);
|
||||
return defaultContent;
|
||||
}
|
||||
}
|
||||
private async Task<string?> GetFileContentAsync(string path, string? defaultContent = null) {
|
||||
if (!File.Exists(path)) {
|
||||
try {
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
if (!string.IsNullOrWhiteSpace(defaultContent)) {
|
||||
await File.WriteAllTextAsync(path, defaultContent, Encoding.UTF8);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.LogError(ex, "File system access failed trying write default content of '{template}'", path);
|
||||
return defaultContent;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await File.ReadAllTextAsync(path, Encoding.UTF8);
|
||||
} catch (Exception ex) {
|
||||
Logger.LogError(ex, "File system access failed trying to retrieve content of '{template}'", path);
|
||||
return defaultContent;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue