Files

280 lines
8.2 KiB
C#
Raw Permalink Normal View History

2024-08-22 13:18:47 -04:00
using System;
using System.Collections.Generic;
2024-08-22 13:18:47 -04:00
using System.IO;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
2024-08-23 19:47:10 -04:00
#endif
2024-08-22 13:18:47 -04:00
namespace MPF.Processors.OutputFiles
2024-08-22 12:57:07 -04:00
{
/// <summary>
/// Represents a single output file
/// </summary>
internal class OutputFile
2024-08-22 12:57:07 -04:00
{
/// <summary>
/// Set of all filename variants
/// </summary>
public string[] Filenames { get; private set; }
2024-08-22 14:46:50 -04:00
/// <summary>
/// Key used when creating an artifact
/// </summary>
2024-08-23 16:32:33 -04:00
public string? ArtifactKey { get; private set; }
2024-08-22 14:46:50 -04:00
2024-08-22 12:57:07 -04:00
/// <summary>
2024-08-22 13:18:47 -04:00
/// Indicates if the file is required
2024-08-22 12:57:07 -04:00
/// </summary>
2024-08-22 13:18:47 -04:00
public bool IsRequired
{
get
{
#if NET20 || NET35
return (_flags & OutputFileFlags.Required) != 0;
#else
return _flags.HasFlag(OutputFileFlags.Required);
#endif
}
}
2024-08-22 12:57:07 -04:00
/// <summary>
2024-08-22 13:18:47 -04:00
/// Indicates if the file is an artifact
2024-08-22 12:57:07 -04:00
/// </summary>
2024-08-22 13:18:47 -04:00
public bool IsArtifact
{
get
{
#if NET20 || NET35
return (_flags & OutputFileFlags.Artifact) != 0
|| (_flags & OutputFileFlags.Binary) != 0;
#else
return _flags.HasFlag(OutputFileFlags.Artifact)
|| _flags.HasFlag(OutputFileFlags.Binary);
#endif
}
}
2024-08-22 12:57:07 -04:00
/// <summary>
2024-08-22 13:18:47 -04:00
/// Indicates if the file is a binary artifact
2024-08-22 12:57:07 -04:00
/// </summary>
2024-08-22 13:18:47 -04:00
public bool IsBinaryArtifact
{
get
{
#if NET20 || NET35
return (_flags & OutputFileFlags.Binary) != 0;
#else
return _flags.HasFlag(OutputFileFlags.Binary);
#endif
}
}
2024-08-22 12:57:07 -04:00
/// <summary>
2024-08-22 13:18:47 -04:00
/// Indicates if the file is deleteable after processing
2024-08-22 12:57:07 -04:00
/// </summary>
2024-08-22 13:18:47 -04:00
public bool IsDeleteable
{
get
{
#if NET20 || NET35
return (_flags & OutputFileFlags.Deleteable) != 0;
#else
return _flags.HasFlag(OutputFileFlags.Deleteable);
#endif
}
}
2024-08-22 12:57:07 -04:00
2024-08-22 13:18:47 -04:00
/// <summary>
/// Indicates if the file is zippable after processing
/// </summary>
public bool IsZippable
{
get
{
#if NET20 || NET35
return (_flags & OutputFileFlags.Zippable) != 0
|| (_flags & OutputFileFlags.Preserve) != 0;
2024-08-22 13:18:47 -04:00
#else
return _flags.HasFlag(OutputFileFlags.Zippable)
|| _flags.HasFlag(OutputFileFlags.Preserve);
#endif
}
}
/// <summary>
/// Indicates if the file is preserved after zipping
/// </summary>
public bool IsPreserved
{
get
{
#if NET20 || NET35
return (_flags & OutputFileFlags.Preserve) != 0;
#else
return _flags.HasFlag(OutputFileFlags.Preserve);
2024-08-22 13:18:47 -04:00
#endif
}
}
/// <summary>
/// Represents attributes about the current file
/// </summary>
2024-08-23 20:58:55 -04:00
protected readonly OutputFileFlags _flags;
2024-08-22 13:18:47 -04:00
/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
2024-08-23 21:36:04 -04:00
public OutputFile(string filename, OutputFileFlags flags)
: this([filename], flags)
2024-08-22 12:57:07 -04:00
{
2024-08-22 14:46:50 -04:00
}
/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
2024-08-23 21:36:04 -04:00
public OutputFile(string filename, OutputFileFlags flags, string artifactKey)
: this([filename], flags, artifactKey)
2024-08-22 14:46:50 -04:00
{
2024-08-22 13:18:47 -04:00
}
/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
2024-08-23 21:36:04 -04:00
public OutputFile(string[] filenames, OutputFileFlags flags)
2024-08-22 13:18:47 -04:00
{
Filenames = filenames;
2024-08-22 14:46:50 -04:00
ArtifactKey = null;
_flags = flags;
2024-08-23 19:47:10 -04:00
// Validate the inputs
if (filenames.Length == 0)
throw new ArgumentException($"{nameof(filenames)} must contain at least one value");
if (IsArtifact && string.IsNullOrEmpty(ArtifactKey))
2024-08-23 19:47:10 -04:00
throw new ArgumentException($"{nameof(flags)} should not contain the Artifact or Binary flag");
2024-08-22 14:46:50 -04:00
}
/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
2024-08-23 21:36:04 -04:00
public OutputFile(string[] filenames, OutputFileFlags flags, string artifactKey)
2024-08-22 14:46:50 -04:00
{
Filenames = filenames;
ArtifactKey = artifactKey;
2024-08-22 13:18:47 -04:00
_flags = flags;
2024-08-23 19:47:10 -04:00
// Validate the inputs
if (filenames.Length == 0)
throw new ArgumentException($"{nameof(filenames)} must contain at least one value");
if (IsArtifact && string.IsNullOrEmpty(ArtifactKey))
2024-08-23 19:47:10 -04:00
throw new ArgumentException($"{nameof(flags)} should not contain the Artifact or Binary flag");
2024-08-22 13:18:47 -04:00
}
/// <summary>
/// Indicates if an output file exists in a base directory
/// </summary>
/// <param name="outputDirectory">Base directory to check in</param>
public virtual bool Exists(string outputDirectory)
2024-08-22 13:18:47 -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-22 13:18:47 -04:00
foreach (string filename in Filenames)
{
// Check for invalid filenames
if (string.IsNullOrEmpty(filename))
continue;
try
{
string possibleFile = Path.Combine(outputDirectory, filename);
2024-10-16 11:45:06 -04:00
if (File.Exists(possibleFile))
2024-08-23 21:36:04 -04:00
return true;
2024-08-22 13:18:47 -04:00
}
catch { }
}
return false;
2024-08-22 12:57:07 -04:00
}
2024-08-23 19:47:10 -04:00
#if NET462_OR_GREATER || NETCOREAPP
2024-08-23 19:47:10 -04:00
/// <summary>
/// Indicates if an output file exists in an archive
/// </summary>
/// <param name="archive">Zip archive to check in</param>
public virtual bool Exists(ZipArchive? archive)
2024-08-23 19:47:10 -04:00
{
// If the archive is invalid
2026-01-25 18:09:00 -05:00
if (archive is null)
return false;
// Get list of all files in archive
foreach (var entry in archive.Entries)
2024-08-23 19:47:10 -04:00
{
2026-01-25 18:09:00 -05:00
if (entry.Key is null)
2024-08-23 19:47:10 -04:00
continue;
if (Array.Exists(Filenames, filename => entry.Key == filename))
return true;
2024-08-23 19:47:10 -04:00
}
return false;
}
/// <summary>
/// Extracts an output file from a zip archive
/// </summary>
/// <param name="archive">Zip archive to check in</param>
/// <param name="outputDirectory">Base directory to extract to</param>
/// <returns>True if file extracted, False otherwise</returns>
public virtual bool Extract(ZipArchive? archive, string outputDirectory)
{
// If the archive is invalid
2026-01-25 18:09:00 -05:00
if (archive is null)
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)
continue;
var matches = Array.FindAll(Filenames, filename => entry.Key == filename);
foreach (string match in matches)
{
try
{
string outputPath = Path.Combine(outputDirectory, match);
entry.WriteToFile(outputPath);
}
catch { }
}
}
return true;
}
#endif
/// <summary>
/// Get all matching paths for the file
/// </summary>
/// <param name="outputDirectory">Base directory to check in</param>
public virtual List<string> GetPaths(string outputDirectory)
{
List<string> paths = [];
foreach (string filename in Filenames)
{
string possibleFile = Path.Combine(outputDirectory, filename);
if (!File.Exists(possibleFile))
continue;
paths.Add(possibleFile);
}
return paths;
}
2024-08-22 12:57:07 -04:00
}
2025-11-11 15:52:26 -05:00
}