naps2/NAPS2.Lib/Dependencies/MultiFileExternalComponent.cs
2023-04-30 14:06:53 -07:00

43 lines
1.2 KiB
C#

namespace NAPS2.Dependencies;
public class MultiFileExternalComponent : IExternalComponent
{
private readonly string[] _subPaths;
public MultiFileExternalComponent(string id, string rootPath, string[] subPaths, DownloadInfo downloadInfo)
{
Id = id;
RootPath = rootPath;
DownloadInfo = downloadInfo;
_subPaths = subPaths;
}
public string Id { get; }
public string RootPath { get; }
public DownloadInfo DownloadInfo { get; }
public bool IsInstalled => _subPaths.Any(sub => File.Exists(Path.Combine(RootPath, sub)));
public void Install(string sourcePath)
{
MoveRecursive(new DirectoryInfo(sourcePath), new DirectoryInfo(RootPath));
}
private void MoveRecursive(DirectoryInfo sourceDir, DirectoryInfo destDir)
{
if (!destDir.Exists)
{
destDir.Create();
}
foreach (var srcFile in sourceDir.EnumerateFiles())
{
srcFile.MoveTo(Path.Combine(destDir.FullName, srcFile.Name));
}
foreach (var subDir in sourceDir.EnumerateDirectories())
{
MoveRecursive(subDir, new DirectoryInfo(Path.Combine(destDir.FullName, subDir.Name)));
}
}
}