mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 12:16:30 +00:00
queue + local sqlite WIP
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System.Reflection;
|
||||
using Azaion.CommonSecurity.DTO;
|
||||
|
||||
namespace Azaion.CommonSecurity.Services;
|
||||
|
||||
public interface IResourceLoader
|
||||
{
|
||||
Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default);
|
||||
Assembly? LoadAssembly(string asmName);
|
||||
}
|
||||
|
||||
public class ResourceLoader(AzaionApiClient api, ApiCredentials credentials) : IResourceLoader
|
||||
{
|
||||
private static readonly List<string> EncryptedResources =
|
||||
[
|
||||
"Azaion.Annotator",
|
||||
"Azaion.Dataset"
|
||||
];
|
||||
|
||||
public Assembly? LoadAssembly(string resourceName)
|
||||
{
|
||||
var assemblyName = resourceName.Split(',').First();
|
||||
if (EncryptedResources.Contains(assemblyName))
|
||||
{
|
||||
try
|
||||
{
|
||||
var stream = Load($"{assemblyName}.dll").GetAwaiter().GetResult();
|
||||
return Assembly.Load(stream.ToArray());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
var currentLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
|
||||
var dllPath = Path.Combine(currentLocation, "dummy", $"{assemblyName}.dll");
|
||||
return Assembly.LoadFile(dllPath);
|
||||
}
|
||||
}
|
||||
|
||||
var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.FirstOrDefault(a => a.GetName().Name == assemblyName);
|
||||
|
||||
return loadedAssembly;
|
||||
}
|
||||
|
||||
public async Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var hardwareService = new HardwareService();
|
||||
var hardwareInfo = hardwareService.GetHardware();
|
||||
|
||||
var encryptedStream = Task.Run(() => api.GetResource(fileName, credentials.Password, hardwareInfo), cancellationToken).Result;
|
||||
|
||||
var key = Security.MakeEncryptionKey(credentials.Email, credentials.Password, hardwareInfo.Hash);
|
||||
var stream = new MemoryStream();
|
||||
await encryptedStream.DecryptTo(stream, key, cancellationToken);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user