Files
MPF/MPF.Processors/RegexOutputFile.cs

108 lines
3.2 KiB
C#
Raw Permalink Normal View History

2024-11-12 22:18:08 -05:00
using System;
using System.Collections.Generic;
2024-08-23 20:58:55 -04:00
using System.IO;
#if NET452_OR_GREATER || NETCOREAPP
using System.IO.Compression;
2024-08-23 20:58:55 -04:00
using System.Linq;
2024-11-12 22:18:08 -05:00
#endif
2024-08-23 20:58:55 -04:00
using System.Text.RegularExpressions;
namespace MPF.Processors
{
/// <summary>
/// Represents a single output file with a Regex-matched name
/// </summary>
internal class RegexOutputFile : OutputFile
2024-08-23 20:58:55 -04:00
{
/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
2024-08-23 21:36:04 -04:00
public RegexOutputFile(string filename, OutputFileFlags flags)
: base([filename], flags)
2024-08-23 20:58:55 -04:00
{
}
/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
2024-08-23 21:36:04 -04:00
public RegexOutputFile(string filename, OutputFileFlags flags, string artifactKey)
: base([filename], flags, artifactKey)
2024-08-23 20:58:55 -04:00
{
}
/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
2024-08-23 21:36:04 -04:00
public RegexOutputFile(string[] filenames, OutputFileFlags flags)
: base(filenames, flags)
2024-08-23 20:58:55 -04:00
{
}
/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
2024-08-23 21:36:04 -04:00
public RegexOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey)
: base(filenames, flags, artifactKey)
2024-08-23 20:58:55 -04:00
{
}
/// <inheritdoc/>
public override bool Exists(string outputDirectory)
2024-08-23 20:58:55 -04:00
{
2024-10-16 12:04:39 -04:00
// Ensure the directory exists
if (!Directory.Exists(outputDirectory))
2024-10-16 12:04:39 -04:00
return false;
2024-08-23 20:58:55 -04:00
// Get list of all files in directory
var directoryFiles = Directory.GetFiles(outputDirectory);
2024-08-23 20:58:55 -04:00
foreach (string file in directoryFiles)
{
2024-11-12 22:18:08 -05:00
if (Array.FindIndex(Filenames, pattern => Regex.IsMatch(file, pattern)) > -1)
2024-08-23 20:58:55 -04:00
return true;
}
return false;
}
#if NET452_OR_GREATER || NETCOREAPP
/// <inheritdoc/>
public override bool Exists(ZipArchive? archive)
{
// If the archive is invalid
if (archive == null)
return false;
// Get list of all files in archive
var archiveFiles = archive.Entries.Select(e => e.Name).ToList();
foreach (string file in archiveFiles)
{
2024-11-16 01:07:47 -05:00
if (Array.Exists(Filenames, pattern => Regex.IsMatch(file, pattern)))
return true;
}
return false;
}
#endif
/// <inheritdoc/>
public override List<string> GetPaths(string outputDirectory)
{
// Ensure the directory exists
if (!Directory.Exists(outputDirectory))
return [];
List<string> paths = [];
// Get list of all files in directory
var directoryFiles = Directory.GetFiles(outputDirectory);
foreach (string file in directoryFiles)
{
var matches = Array.FindAll(Filenames, pattern => Regex.IsMatch(file, pattern));
if (matches != null && matches.Length > 0)
paths.Add(file);
}
return paths;
}
2024-08-23 20:58:55 -04:00
}
}