using System;
using System.Collections.Generic;
using System.IO;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Archives.Zip;
#endif
namespace MPF.Processors.OutputFiles
{
///
/// Represents a single output file with custom detection rules
///
internal class CustomOutputFile : OutputFile
{
///
/// Optional func for determining if a file exists
///
private readonly Func _existsFunc;
///
/// Create an OutputFile with a single filename
///
public CustomOutputFile(string filename, OutputFileFlags flags, Func existsFunc)
: base([filename], flags)
{
_existsFunc = existsFunc;
}
///
/// Create an OutputFile with a single filename
///
public CustomOutputFile(string filename, OutputFileFlags flags, string artifactKey, Func existsFunc)
: base([filename], flags, artifactKey)
{
_existsFunc = existsFunc;
}
///
/// Create an OutputFile with set of filenames
///
public CustomOutputFile(string[] filenames, OutputFileFlags flags, Func existsFunc)
: base(filenames, flags)
{
_existsFunc = existsFunc;
}
///
/// Create an OutputFile with set of filenames
///
public CustomOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey, Func existsFunc)
: base(filenames, flags, artifactKey)
{
_existsFunc = existsFunc;
}
///
public override bool Exists(string outputDirectory)
{
// Ensure the directory exists
if (!Directory.Exists(outputDirectory))
return false;
foreach (string filename in Filenames)
{
// Check for invalid filenames
if (string.IsNullOrEmpty(filename))
continue;
try
{
string possibleFile = Path.Combine(outputDirectory, filename);
if (_existsFunc(possibleFile))
return true;
}
catch { }
}
return false;
}
#if NET462_OR_GREATER || NETCOREAPP
///
public override bool Exists(ZipArchive? archive)
{
// Files aren't extracted so this check can't be done
return false;
}
///
public override bool Extract(ZipArchive? archive, string outputDirectory)
{
// Files aren't extracted so this check can't be done
return false;
}
#endif
///
public override List GetPaths(string outputDirectory)
{
List paths = [];
foreach (string filename in Filenames)
{
string possibleFile = Path.Combine(outputDirectory, filename);
if (!_existsFunc(possibleFile))
continue;
paths.Add(possibleFile);
}
return paths;
}
}
}