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

109 lines
3.9 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.Text;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
2020-03-28 20:42:45 +00:00
using FluentFTP;
2020-01-08 04:41:50 +00:00
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Wabbajack.BuildServer.Model.Models;
2020-01-08 04:41:50 +00:00
using Wabbajack.BuildServer.Models;
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(DBContext db, 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 db.DownloadStates.AsQueryable().Where(f => f.Key == pk_str).Take(1).ToListAsync();
if (found.Count > 0)
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 db.DownloadStates.InsertOneAsync(new DownloadState
{
Key = pk_str, Hash = archive.Hash, State = Archive.State, IsValid = true
});
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
}
2020-01-09 04:42:25 +00:00
2020-01-08 04:41:50 +00:00
return JobResult.Success();
}
2020-03-28 20:42:45 +00:00
/*
2020-01-08 04:41:50 +00:00
private List<IndexedFile> ConvertArchive(List<IndexedFile> files, VirtualFile file, bool isTop = true)
{
2020-03-28 20:42:45 +00:00
var name = isTop ? file.Name.FileName : file.Name;
2020-01-08 04:41:50 +00:00
var ifile = new IndexedFile
{
Hash = file.Hash,
SHA256 = file.ExtendedHashes.SHA256,
SHA1 = file.ExtendedHashes.SHA1,
MD5 = file.ExtendedHashes.MD5,
CRC = file.ExtendedHashes.CRC,
Size = file.Size,
Children = file.Children != null ? file.Children.Select(
f =>
{
ConvertArchive(files, f, false);
return new ChildFile
{
Hash = f.Hash,
2020-03-28 20:42:45 +00:00
Name = f.Name,
2020-01-08 04:41:50 +00:00
Extension = Path.GetExtension(f.Name.ToLowerInvariant())
};
}).ToList() : new List<ChildFile>()
};
ifile.IsArchive = ifile.Children.Count > 0;
files.Add(ifile);
return files;
2020-03-28 20:42:45 +00:00
}*/
2020-01-08 04:41:50 +00:00
}
}