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;
|
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");
|
string path = Path.Combine(ConfigurationDirectory, "templates", "email", name + ".mjml");
|
||||||
|
return GetFileContentAsync(path, defaultTemplate);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string? GetEmailTemplate(string name, string? defaultTemplate = null) {
|
public string? GetEmailTemplate(string name, string? defaultTemplate = null) {
|
||||||
string path = Path.Combine(ConfigurationDirectory, "templates", "email", name + ".mjml");
|
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)) {
|
if (!File.Exists(path)) {
|
||||||
try {
|
try {
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||||
if (!string.IsNullOrWhiteSpace(defaultTemplate)) {
|
if (!string.IsNullOrWhiteSpace(defaultContent)) {
|
||||||
File.WriteAllText(path, defaultTemplate, Encoding.UTF8);
|
File.WriteAllText(path, defaultContent, Encoding.UTF8);
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.LogError(ex, "File system access failed trying write default E-Mail template '{template}'", name);
|
Logger.LogError(ex, "File system access failed trying write default content of '{template}'", path);
|
||||||
return defaultTemplate;
|
return defaultContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return File.ReadAllText(path, Encoding.UTF8);
|
return File.ReadAllText(path, Encoding.UTF8);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.LogError(ex, "File system access failed trying to retrieve E-Mail template '{template}'", name);
|
Logger.LogError(ex, "File system access failed trying to retrieve content of '{template}'", path);
|
||||||
return defaultTemplate;
|
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