Files
jobtrackingapp/JobTrackerApi/Services/AppPaths.cs
T
2026-03-21 11:55:27 +01:00

39 lines
1.5 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace JobTrackerApi.Services
{
public sealed class AppPaths
{
public string DataRoot { get; }
public string AttachmentsRoot { get; }
public AppPaths(IConfiguration cfg, IHostEnvironment env)
{
var dataRoot = (cfg["Data:Root"] ?? "").Trim();
if (string.IsNullOrWhiteSpace(dataRoot)) dataRoot = env.ContentRootPath;
if (!Path.IsPathRooted(dataRoot)) dataRoot = Path.Combine(env.ContentRootPath, dataRoot);
Directory.CreateDirectory(dataRoot);
DataRoot = dataRoot;
var attachmentsRoot = (cfg["Data:AttachmentsRoot"] ?? "").Trim();
if (string.IsNullOrWhiteSpace(attachmentsRoot)) attachmentsRoot = Path.Combine(DataRoot, "Attachments");
if (!Path.IsPathRooted(attachmentsRoot)) attachmentsRoot = Path.Combine(env.ContentRootPath, attachmentsRoot);
Directory.CreateDirectory(attachmentsRoot);
AttachmentsRoot = attachmentsRoot;
}
public string GetDbPath(string fileName = "jobtracker.db") => Path.Combine(DataRoot, fileName);
public string GetExportsRoot(string? configuredFolder)
{
var folder = (configuredFolder ?? "").Trim();
if (string.IsNullOrWhiteSpace(folder)) return Path.Combine(DataRoot, "exports");
return Path.IsPathRooted(folder) ? folder : Path.Combine(DataRoot, folder);
}
}
}