2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/Pulsar/Features/Interfaces/IJournalHandler.cs
Ben Parsons b44d822eb9 Rework JournalHandler Again
Reduce Code Dupe
Finished ModulesInfo
Started ShipLocker
2024-04-21 23:40:44 +10:00

58 lines
1.4 KiB
C#

using Observatory.Framework.Files.Journal;
namespace Pulsar.Features;
/// <summary>
/// Interface for Handling Journal Files.
/// </summary>
public interface IJournalHandler : IFileHandler
{
string FileName { get; }
public bool ValidateFile(string filePath);
}
/// <summary>
/// Interface for Getting Journal Files.
/// Only used for Controllers
/// </summary>
public interface IJournalHandler<T> : IJournalHandler
where T: IJournal
{
Task<T> Get();
}
public abstract class JournalHandlerBase<T>(ILogger logger) : IJournalHandler<T>
where T: IJournal
{
public abstract string FileName { get; }
public abstract Task HandleFile(string filePath);
public bool ValidateFile(string filePath)
{
if (!File.Exists(filePath))
{
logger.LogWarning("Journal file {JournalFile} does not exist", filePath);
return false;
}
var fileInfo = new FileInfo(filePath);
if (!string.Equals(fileInfo.Name, FileName, StringComparison.InvariantCultureIgnoreCase))
{
logger.LogWarning("Journal file {JournalFile} is not a {NameOfCurrentHandler} file", filePath, nameof(T));
return false;
}
if (fileInfo.Length == 0)
{
logger.LogWarning("Journal file {JournalFile} is empty", filePath);
return false;
}
return true;
}
public abstract Task<T> Get();
}