mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
71 lines
2.6 KiB
C#
71 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
using Alphaleonis.Win32.Filesystem;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using MongoDB.Driver;
|
|
using MongoDB.Driver.Linq;
|
|
using Wabbajack.Common;
|
|
using Directory =Alphaleonis.Win32.Filesystem.Directory;
|
|
using File = Alphaleonis.Win32.Filesystem.File;
|
|
using Path = Alphaleonis.Win32.Filesystem.Path;
|
|
|
|
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();
|
|
}
|
|
|
|
public static void UseJobManager(this IApplicationBuilder b)
|
|
{
|
|
var manager = (JobManager)b.ApplicationServices.GetService(typeof(JobManager));
|
|
var tsk = manager.JobScheduler();
|
|
|
|
manager.StartJobRunners();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static AuthenticationBuilder AddApiKeySupport(this AuthenticationBuilder authenticationBuilder, Action<ApiKeyAuthenticationOptions> options)
|
|
{
|
|
return authenticationBuilder.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthenticationOptions.DefaultScheme, options);
|
|
}
|
|
|
|
private static ConcurrentDictionary<string, string> PathForArchiveHash = new ConcurrentDictionary<string, string>();
|
|
public static string PathForArchive(this AppSettings settings, string hash)
|
|
{
|
|
if (PathForArchiveHash.TryGetValue(hash, out string result))
|
|
return result;
|
|
|
|
var hexHash = hash.FromBase64().ToHex();
|
|
|
|
var ends = "_" + hexHash + "_";
|
|
var file = Directory.EnumerateFiles(settings.ArchiveDir, DirectoryEnumerationOptions.Files,
|
|
new DirectoryEnumerationFilters
|
|
{
|
|
InclusionFilter = f => Path.GetFileNameWithoutExtension(f.FileName).EndsWith(ends)
|
|
}).FirstOrDefault();
|
|
|
|
if (file != null)
|
|
PathForArchiveHash.TryAdd(hash, file);
|
|
return file;
|
|
}
|
|
}
|
|
}
|