wabbajack/Wabbajack.BuildServer/Models/Jobs/IndexJob.cs

67 lines
2.5 KiB
C#
Raw Normal View History

2020-01-08 04:41:50 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using Wabbajack.BuildServer.Model.Models;
2020-01-08 04:41:50 +00:00
using Wabbajack.BuildServer.Models.JobQueue;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.VirtualFileSystem;
namespace Wabbajack.BuildServer.Models.Jobs
{
public class IndexJob : AJobPayload, IBackEndJob
2020-01-08 04:41:50 +00:00
{
public Archive Archive { get; set; }
2020-01-10 13:25:01 +00:00
public override string Description => $"Index ${Archive.State.PrimaryKeyString} and save the download/file state";
2020-01-08 04:41:50 +00:00
public override bool UsesNexus { get => Archive.State is NexusDownloader.State; }
public override async Task<JobResult> Execute(SqlService sql, AppSettings settings)
2020-01-08 04:41:50 +00:00
{
if (Archive.State is ManualDownloader.State)
return JobResult.Success();
2020-01-08 04:41:50 +00:00
var pk = new List<object>();
pk.Add(AbstractDownloadState.TypeToName[Archive.State.GetType()]);
pk.AddRange(Archive.State.PrimaryKey);
var pk_str = string.Join("|",pk.Select(p => p.ToString()));
var found = await sql.DownloadStateByPrimaryKey(pk_str);
if (found == null)
2020-01-08 04:41:50 +00:00
return JobResult.Success();
2020-01-08 04:41:50 +00:00
string fileName = Archive.Name;
string folder = Guid.NewGuid().ToString();
Utils.Log($"Indexer is downloading {fileName}");
2020-03-28 20:42:45 +00:00
var downloadDest = settings.DownloadDir.Combine(folder, fileName);
2020-01-08 04:41:50 +00:00
await Archive.State.Download(downloadDest);
using (var queue = new WorkQueue())
{
var vfs = new Context(queue, true);
2020-03-28 20:42:45 +00:00
await vfs.AddRoot(settings.DownloadDir.Combine(folder));
var archive = vfs.Index.ByRootPath.First().Value;
2020-01-08 04:41:50 +00:00
await sql.MergeVirtualFile(archive);
await sql.AddDownloadState(archive.Hash, Archive.State);
2020-03-28 20:42:45 +00:00
var to_path = settings.ArchiveDir.Combine(
2020-03-22 15:50:53 +00:00
$"{Path.GetFileName(fileName)}_{archive.Hash.ToHex()}_{Path.GetExtension(fileName)}");
2020-03-28 20:42:45 +00:00
if (to_path.Exists)
downloadDest.Delete();
2020-01-08 04:41:50 +00:00
else
2020-03-28 20:42:45 +00:00
downloadDest.MoveTo(to_path);
await settings.DownloadDir.Combine(folder).DeleteDirectory();
2020-01-08 04:41:50 +00:00
}
return JobResult.Success();
}
}
}