Initial NginxMan release
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user