wabbajack/Wabbajack.Server/Services/ArchiveMaintainer.cs

66 lines
1.7 KiB
C#
Raw Normal View History

2021-10-23 16:51:17 +00:00
using System.Threading;
using System.Threading.Tasks;
2021-09-27 12:42:46 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Wabbajack.BuildServer;
2021-09-27 12:42:46 +00:00
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.VFS;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Server.Services;
/// <summary>
/// Maintains a concurrent cache of all the files we've downloaded, indexed by Hash.
/// </summary>
public class ArchiveMaintainer
{
2021-10-23 16:51:17 +00:00
private readonly ILogger<ArchiveMaintainer> _logger;
private readonly AppSettings _settings;
public ArchiveMaintainer(ILogger<ArchiveMaintainer> logger, AppSettings settings)
{
2021-10-23 16:51:17 +00:00
_settings = settings;
_logger = logger;
_logger.Log(LogLevel.Information, "Creating Archive Maintainer");
}
2021-10-23 16:51:17 +00:00
private AbsolutePath ArchivePath(Hash hash)
{
return _settings.ArchivePath.Combine(hash.ToHex());
}
2021-10-23 16:51:17 +00:00
public async Task Ingest(AbsolutePath file)
{
var hash = await file.Hash(CancellationToken.None);
if (hash == default) return;
2021-10-23 16:51:17 +00:00
var newPath = ArchivePath(hash);
if (HaveArchive(hash))
{
2021-10-23 16:51:17 +00:00
file.Delete();
return;
}
2021-10-23 16:51:17 +00:00
await file.MoveToAsync(newPath, true, CancellationToken.None);
}
2021-10-23 16:51:17 +00:00
public bool HaveArchive(Hash hash)
{
return ArchivePath(hash).FileExists();
}
2021-10-23 16:51:17 +00:00
public bool TryGetPath(Hash hash, out AbsolutePath path)
{
2021-10-23 16:51:17 +00:00
path = ArchivePath(hash);
return path.FileExists();
}
}
2021-10-23 16:51:17 +00:00
public static class ArchiveMaintainerExtensions
{
public static IServiceCollection UseArchiveMaintainer(this IServiceCollection b)
{
return b.AddSingleton<ArchiveMaintainer>();
}
}