AuthorBuddy/Models/ProjectPathHelper.cs
2026-05-31 19:31:27 +02:00

32 lines
1.1 KiB
C#

namespace AuthorBuddy.Web.Models;
public static class ProjectPathHelper
{
public static string AllowedBasePath =>
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"AuthorBuddy"
);
public static bool IsPathAllowed(string path)
{
if (string.IsNullOrWhiteSpace(path)) return false;
var full = Path.GetFullPath(path);
var allowed = Path.GetFullPath(AllowedBasePath);
return full.StartsWith(allowed + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)
|| string.Equals(full, allowed, StringComparison.OrdinalIgnoreCase);
}
public static string SuggestPath(string projectName)
{
var safeName = Sanitize(projectName);
return Path.Combine(AllowedBasePath, safeName);
}
private static string Sanitize(string name)
{
var invalid = Path.GetInvalidFileNameChars();
var result = new string(name.Where(c => !invalid.Contains(c)).ToArray());
return string.IsNullOrWhiteSpace(result) ? "Projekt" : result.Trim();
}
}