47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
namespace StellaOps.AirGap.Bundle.Services;
|
|
|
|
public sealed partial class BundleBuilder
|
|
{
|
|
private static string SanitizeFileSegment(string value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
return "artifact";
|
|
}
|
|
|
|
var buffer = new char[value.Length];
|
|
var index = 0;
|
|
foreach (var ch in value)
|
|
{
|
|
if (char.IsLetterOrDigit(ch) || ch == '-' || ch == '_')
|
|
{
|
|
buffer[index++] = ch;
|
|
}
|
|
else
|
|
{
|
|
buffer[index++] = '-';
|
|
}
|
|
}
|
|
|
|
var cleaned = new string(buffer, 0, index).Trim('-');
|
|
return string.IsNullOrWhiteSpace(cleaned) ? "artifact" : cleaned;
|
|
}
|
|
|
|
private static string EnsureSafeFileName(string fileName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fileName))
|
|
{
|
|
throw new ArgumentException("Artifact file name is required.");
|
|
}
|
|
|
|
if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 ||
|
|
fileName.Contains('/') ||
|
|
fileName.Contains('\\'))
|
|
{
|
|
throw new ArgumentException($"Invalid artifact file name: {fileName}");
|
|
}
|
|
|
|
return fileName;
|
|
}
|
|
}
|