2020-05-13 03:04:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Wabbajack.BuildServer;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using File = System.IO.File;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Server.Services
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-05-13 12:09:20 +00:00
|
|
|
|
/// Maintains a concurrent cache of all the files we've downloaded, indexed by Hash.
|
2020-05-13 03:04:32 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class ArchiveMaintainer
|
|
|
|
|
{
|
|
|
|
|
private AppSettings _settings;
|
|
|
|
|
private ILogger<ArchiveMaintainer> _logger;
|
|
|
|
|
|
|
|
|
|
public ArchiveMaintainer(ILogger<ArchiveMaintainer> logger, AppSettings settings)
|
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_logger = logger;
|
2020-06-05 02:55:11 +00:00
|
|
|
|
_logger.Log(LogLevel.Information, "Creating Archive Maintainer");
|
2020-05-13 03:04:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2020-06-05 02:55:11 +00:00
|
|
|
|
_logger.Log(LogLevel.Information, $"Found {_settings.ArchivePath.EnumerateFiles(false).Count()} archives");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AbsolutePath ArchivePath(Hash hash)
|
|
|
|
|
{
|
|
|
|
|
return _settings.ArchivePath.Combine(hash.ToHex());
|
2020-05-13 03:04:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AbsolutePath> Ingest(AbsolutePath file)
|
|
|
|
|
{
|
|
|
|
|
var hash = await file.FileHashAsync();
|
2020-06-05 02:55:11 +00:00
|
|
|
|
var path = ArchivePath(hash);
|
2020-05-13 03:04:32 +00:00
|
|
|
|
if (HaveArchive(hash))
|
|
|
|
|
{
|
2020-05-26 11:31:11 +00:00
|
|
|
|
await file.DeleteAsync();
|
2020-06-05 02:55:11 +00:00
|
|
|
|
return path;
|
2020-05-13 03:04:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newPath = _settings.ArchivePath.Combine(hash.ToHex());
|
|
|
|
|
await file.MoveToAsync(newPath);
|
2020-06-05 02:55:11 +00:00
|
|
|
|
return path;
|
2020-05-13 03:04:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HaveArchive(Hash hash)
|
|
|
|
|
{
|
2020-06-05 02:55:11 +00:00
|
|
|
|
return ArchivePath(hash).Exists;
|
2020-05-13 03:04:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryGetPath(Hash hash, out AbsolutePath path)
|
|
|
|
|
{
|
2020-06-05 02:55:11 +00:00
|
|
|
|
path = ArchivePath(hash);
|
|
|
|
|
return path.Exists;
|
2020-05-13 03:04:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ArchiveMaintainerExtensions
|
|
|
|
|
{
|
|
|
|
|
public static void UseArchiveMaintainer(this IApplicationBuilder b)
|
|
|
|
|
{
|
|
|
|
|
var poll = (ArchiveMaintainer)b.ApplicationServices.GetService(typeof(ArchiveMaintainer));
|
|
|
|
|
poll.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|