wabbajack/Wabbajack.BuildServer/Extensions.cs

68 lines
2.5 KiB
C#
Raw Normal View History

2020-01-08 04:41:50 +00:00
using System;
using System.Collections.Concurrent;
2020-01-16 05:06:25 +00:00
using System.IO;
2020-01-08 04:41:50 +00:00
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
2020-01-16 05:06:25 +00:00
using Microsoft.AspNetCore.Authentication;
2020-01-09 04:42:25 +00:00
using Microsoft.AspNetCore.Builder;
2020-01-08 04:41:50 +00:00
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Wabbajack.Common;
using Directory =Alphaleonis.Win32.Filesystem.Directory;
2020-01-16 05:06:25 +00:00
using File = Alphaleonis.Win32.Filesystem.File;
using Path = Alphaleonis.Win32.Filesystem.Path;
2020-01-08 04:41:50 +00:00
namespace Wabbajack.BuildServer
{
public static class Extensions
{
public static async Task<T> FindOneAsync<T>(this IMongoCollection<T> coll, Expression<Func<T, bool>> expr)
{
return (await coll.AsQueryable().Where(expr).Take(1).ToListAsync()).FirstOrDefault();
}
2020-01-09 04:42:25 +00:00
public static void UseJobManager(this IApplicationBuilder b)
{
var manager = (JobManager)b.ApplicationServices.GetService(typeof(JobManager));
var tsk = manager.JobScheduler();
manager.StartJobRunners();
}
2020-01-16 05:06:25 +00:00
public static async Task CopyFileAsync(string sourcePath, string destinationPath)
{
using (Stream source = File.OpenRead(sourcePath))
{
using(Stream destination = File.Create(destinationPath))
{
await source.CopyToAsync(destination);
}
}
}
2020-01-16 05:06:25 +00:00
public static AuthenticationBuilder AddApiKeySupport(this AuthenticationBuilder authenticationBuilder, Action<ApiKeyAuthenticationOptions> options)
{
return authenticationBuilder.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthenticationOptions.DefaultScheme, options);
}
2020-03-28 20:42:45 +00:00
private static readonly ConcurrentDictionary<Hash, AbsolutePath> PathForArchiveHash = new ConcurrentDictionary<Hash, AbsolutePath>();
public static AbsolutePath PathForArchive(this AppSettings settings, Hash hash)
{
2020-03-28 20:42:45 +00:00
if (PathForArchiveHash.TryGetValue(hash, out AbsolutePath result))
return result;
2020-03-22 15:50:53 +00:00
var hexHash = hash.ToHex();
var ends = "_" + hexHash + "_";
2020-03-28 20:42:45 +00:00
var file = settings.ArchiveDir.EnumerateFiles()
.FirstOrDefault(f => ((string)f.FileNameWithoutExtension).EndsWith(ends));
if (file != default)
PathForArchiveHash.TryAdd(hash, file);
return file;
}
2020-01-08 04:41:50 +00:00
}
}