Improved FileSystemServic, added methods for html partial templates

This commit is contained in:
Mia Rose Winter 2024-02-14 12:37:27 +01:00
parent fd43ead960
commit cf51a3b4ab
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -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;
} }
} }
} }