51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
using System.Reflection;
|
|
|
|
namespace NginxMan;
|
|
|
|
internal static class BundledNginx
|
|
{
|
|
private const string ResourcePrefix = "NginxMan.NginxAssets/";
|
|
|
|
public static string? ExtractTo(string dataDirectory)
|
|
{
|
|
var assembly = typeof(BundledNginx).Assembly;
|
|
var resources = assembly.GetManifestResourceNames()
|
|
.Where(name => name.StartsWith(ResourcePrefix, StringComparison.Ordinal))
|
|
.ToArray();
|
|
|
|
const string exeResourceName = ResourcePrefix + "nginx.exe";
|
|
if (!resources.Contains(exeResourceName, StringComparer.Ordinal))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var targetDirectory = Path.Combine(dataDirectory, "nginx");
|
|
var targetRoot = Path.GetFullPath(targetDirectory) + Path.DirectorySeparatorChar;
|
|
|
|
foreach (var resourceName in resources)
|
|
{
|
|
var relativePath = resourceName[ResourcePrefix.Length..]
|
|
.Replace('/', Path.DirectorySeparatorChar)
|
|
.Replace('\\', Path.DirectorySeparatorChar);
|
|
var targetPath = Path.GetFullPath(Path.Combine(targetDirectory, relativePath));
|
|
if (!targetPath.StartsWith(targetRoot, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
throw new InvalidOperationException("Invalid embedded Nginx resource path.");
|
|
}
|
|
|
|
if (File.Exists(targetPath))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(targetPath)!);
|
|
using var input = assembly.GetManifestResourceStream(resourceName)
|
|
?? throw new InvalidOperationException($"Cannot read embedded resource: {resourceName}");
|
|
using var output = File.Create(targetPath);
|
|
input.CopyTo(output);
|
|
}
|
|
|
|
return Path.Combine(targetDirectory, "nginx.exe");
|
|
}
|
|
}
|