2024-11-12 22:18:08 -05:00
|
|
|
using System;
|
2024-12-03 15:38:59 -05:00
|
|
|
using System.Collections.Generic;
|
2024-08-23 20:58:55 -04:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2025-10-07 17:33:01 -04:00
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
2025-10-11 09:27:55 -04:00
|
|
|
using SharpCompress.Archives;
|
2025-10-07 17:33:01 -04:00
|
|
|
using SharpCompress.Archives.Zip;
|
|
|
|
|
#endif
|
2024-08-23 20:58:55 -04:00
|
|
|
|
2025-10-11 09:34:00 -04:00
|
|
|
namespace MPF.Processors.OutputFiles
|
2024-08-23 20:58:55 -04:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a single output file with a Regex-matched name
|
|
|
|
|
/// </summary>
|
2024-08-23 21:39:50 -04:00
|
|
|
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/>
|
2024-12-29 14:12:04 -05:00
|
|
|
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
|
2024-12-29 14:12:04 -05:00
|
|
|
if (!Directory.Exists(outputDirectory))
|
2024-10-16 12:04:39 -04:00
|
|
|
return false;
|
2025-10-07 17:33:01 -04:00
|
|
|
|
2024-08-23 20:58:55 -04:00
|
|
|
// Get list of all files in directory
|
2024-12-29 14:12:04 -05:00
|
|
|
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;
|
|
|
|
|
}
|
2024-08-23 21:08:43 -04:00
|
|
|
|
2025-10-07 17:33:01 -04:00
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
2024-12-03 15:38:59 -05:00
|
|
|
/// <inheritdoc/>
|
2024-08-23 21:08:43 -04:00
|
|
|
public override bool Exists(ZipArchive? archive)
|
|
|
|
|
{
|
|
|
|
|
// If the archive is invalid
|
2026-01-25 18:09:00 -05:00
|
|
|
if (archive is null)
|
2024-08-23 21:08:43 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Get list of all files in archive
|
2025-10-07 17:33:01 -04:00
|
|
|
foreach (var entry in archive.Entries)
|
2024-08-23 21:08:43 -04:00
|
|
|
{
|
2026-01-25 18:09:00 -05:00
|
|
|
if (entry.Key is null)
|
2025-10-07 17:33:01 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (Array.Exists(Filenames, pattern => Regex.IsMatch(entry.Key, pattern)))
|
2024-08-23 21:08:43 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-10-11 09:27:55 -04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool Extract(ZipArchive? archive, string outputDirectory)
|
|
|
|
|
{
|
|
|
|
|
// If the archive is invalid
|
2026-01-25 18:09:00 -05:00
|
|
|
if (archive is null)
|
2025-10-11 09:27:55 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Get list of all files in archive
|
|
|
|
|
foreach (var entry in archive.Entries)
|
|
|
|
|
{
|
2026-01-25 18:09:00 -05:00
|
|
|
if (entry.Key is null)
|
2025-10-11 09:27:55 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var matches = Array.FindAll(Filenames, pattern => Regex.IsMatch(entry.Key, pattern));
|
2025-10-19 12:47:21 -04:00
|
|
|
if (matches.Length > 0)
|
2025-10-11 09:27:55 -04:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-10-19 12:47:21 -04:00
|
|
|
string outputPath = Path.Combine(outputDirectory, entry.Key);
|
2025-10-11 09:27:55 -04:00
|
|
|
entry.WriteToFile(outputPath);
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-08-23 21:08:43 -04:00
|
|
|
#endif
|
2024-12-03 15:38:59 -05:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2024-12-29 14:12:04 -05:00
|
|
|
public override List<string> GetPaths(string outputDirectory)
|
2024-12-03 15:38:59 -05:00
|
|
|
{
|
|
|
|
|
// Ensure the directory exists
|
2024-12-29 14:12:04 -05:00
|
|
|
if (!Directory.Exists(outputDirectory))
|
2024-12-03 15:38:59 -05:00
|
|
|
return [];
|
|
|
|
|
|
|
|
|
|
List<string> paths = [];
|
2025-10-07 17:33:01 -04:00
|
|
|
|
2024-12-03 15:38:59 -05:00
|
|
|
// Get list of all files in directory
|
2024-12-29 14:12:04 -05:00
|
|
|
var directoryFiles = Directory.GetFiles(outputDirectory);
|
2024-12-03 15:38:59 -05:00
|
|
|
foreach (string file in directoryFiles)
|
|
|
|
|
{
|
|
|
|
|
var matches = Array.FindAll(Filenames, pattern => Regex.IsMatch(file, pattern));
|
2026-01-25 18:09:00 -05:00
|
|
|
if (matches is not null && matches.Length > 0)
|
2024-12-03 15:38:59 -05:00
|
|
|
paths.Add(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
2024-08-23 20:58:55 -04:00
|
|
|
}
|
2025-11-11 15:52:26 -05:00
|
|
|
}
|