 14575a4a6b
			
		
	
	14575a4a6b
	
	
	
		
			
			* add jwt expiry * start of 0.9 api impl * some stuff idk * some more impl * some cleanup * remove grouppair, add configuration, rework some pair drawing stuff * do some stuff * rework some ui * I don't even know anymore * add cancellationtoken * token bla * ui fixes etc * probably individual adding/removing now working fully as expected * add working report popup * I guess it's more syncshell shit or so * popup shit idk * work out most of the syncshell bullshit I guess * delete some old crap * are we actually getting closer to the end * update pair info stuff * more fixes/adjustments, idk * refactor some things * some rework * some more cleanup * cleanup * make menu buttons w i d e * better icon text buttons * add all syncshell folder and ordering fixes --------- Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
		
			
				
	
	
		
			142 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using MareSynchronos.MareConfiguration;
 | |
| using MareSynchronos.Services.Mediator;
 | |
| using MareSynchronos.WebAPI.Files.Models;
 | |
| using MareSynchronos.WebAPI.SignalR;
 | |
| using Microsoft.Extensions.Logging;
 | |
| using System.Collections.Concurrent;
 | |
| using System.Net.Http.Headers;
 | |
| using System.Net.Http.Json;
 | |
| using System.Reflection;
 | |
| 
 | |
| namespace MareSynchronos.WebAPI.Files;
 | |
| 
 | |
| public class FileTransferOrchestrator : DisposableMediatorSubscriberBase
 | |
| {
 | |
|     private readonly ConcurrentDictionary<Guid, bool> _downloadReady = new();
 | |
|     private readonly HttpClient _httpClient;
 | |
|     private readonly MareConfigService _mareConfig;
 | |
|     private readonly object _semaphoreModificationLock = new();
 | |
|     private readonly TokenProvider _tokenProvider;
 | |
|     private int _availableDownloadSlots;
 | |
|     private SemaphoreSlim _downloadSemaphore;
 | |
| 
 | |
|     public FileTransferOrchestrator(ILogger<FileTransferOrchestrator> logger, MareConfigService mareConfig,
 | |
|         MareMediator mediator, TokenProvider tokenProvider) : base(logger, mediator)
 | |
|     {
 | |
|         _mareConfig = mareConfig;
 | |
|         _tokenProvider = tokenProvider;
 | |
|         _httpClient = new()
 | |
|         {
 | |
|             Timeout = TimeSpan.FromSeconds(3000)
 | |
|         };
 | |
|         var ver = Assembly.GetExecutingAssembly().GetName().Version;
 | |
|         _httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
 | |
| 
 | |
|         _availableDownloadSlots = mareConfig.Current.ParallelDownloads;
 | |
|         _downloadSemaphore = new(_availableDownloadSlots);
 | |
| 
 | |
|         Mediator.Subscribe<ConnectedMessage>(this, (msg) =>
 | |
|         {
 | |
|             FilesCdnUri = msg.Connection.ServerInfo.FileServerAddress;
 | |
|         });
 | |
| 
 | |
|         Mediator.Subscribe<DisconnectedMessage>(this, (msg) =>
 | |
|         {
 | |
|             FilesCdnUri = null;
 | |
|         });
 | |
|         Mediator.Subscribe<DownloadReadyMessage>(this, (msg) =>
 | |
|         {
 | |
|             _downloadReady[msg.RequestId] = true;
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     public Uri? FilesCdnUri { private set; get; }
 | |
|     public List<FileTransfer> ForbiddenTransfers { get; } = [];
 | |
|     public bool IsInitialized => FilesCdnUri != null;
 | |
| 
 | |
|     public void ClearDownloadRequest(Guid guid)
 | |
|     {
 | |
|         _downloadReady.Remove(guid, out _);
 | |
|     }
 | |
| 
 | |
|     public bool IsDownloadReady(Guid guid)
 | |
|     {
 | |
|         if (_downloadReady.TryGetValue(guid, out bool isReady) && isReady)
 | |
|         {
 | |
|             return true;
 | |
|         }
 | |
| 
 | |
|         return false;
 | |
|     }
 | |
| 
 | |
|     public void ReleaseDownloadSlot()
 | |
|     {
 | |
|         _downloadSemaphore.Release();
 | |
|     }
 | |
| 
 | |
|     public async Task<HttpResponseMessage> SendRequestAsync(HttpMethod method, Uri uri,
 | |
|         CancellationToken? ct = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
 | |
|     {
 | |
|         using var requestMessage = new HttpRequestMessage(method, uri);
 | |
|         return await SendRequestInternalAsync(requestMessage, ct, httpCompletionOption).ConfigureAwait(false);
 | |
|     }
 | |
| 
 | |
|     public async Task<HttpResponseMessage> SendRequestAsync<T>(HttpMethod method, Uri uri, T content, CancellationToken ct) where T : class
 | |
|     {
 | |
|         using var requestMessage = new HttpRequestMessage(method, uri);
 | |
|         if (content is not ByteArrayContent)
 | |
|             requestMessage.Content = JsonContent.Create(content);
 | |
|         else
 | |
|             requestMessage.Content = content as ByteArrayContent;
 | |
|         return await SendRequestInternalAsync(requestMessage, ct).ConfigureAwait(false);
 | |
|     }
 | |
| 
 | |
|     public async Task<HttpResponseMessage> SendRequestStreamAsync(HttpMethod method, Uri uri, ProgressableStreamContent content, CancellationToken ct)
 | |
|     {
 | |
|         using var requestMessage = new HttpRequestMessage(method, uri);
 | |
|         requestMessage.Content = content;
 | |
|         return await SendRequestInternalAsync(requestMessage, ct).ConfigureAwait(false);
 | |
|     }
 | |
| 
 | |
|     public async Task WaitForDownloadSlotAsync(CancellationToken token)
 | |
|     {
 | |
|         lock (_semaphoreModificationLock)
 | |
|         {
 | |
|             if (_availableDownloadSlots != _mareConfig.Current.ParallelDownloads && _availableDownloadSlots == _downloadSemaphore.CurrentCount)
 | |
|             {
 | |
|                 _availableDownloadSlots = _mareConfig.Current.ParallelDownloads;
 | |
|                 _downloadSemaphore = new(_availableDownloadSlots);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         await _downloadSemaphore.WaitAsync(token).ConfigureAwait(false);
 | |
|     }
 | |
| 
 | |
|     private async Task<HttpResponseMessage> SendRequestInternalAsync(HttpRequestMessage requestMessage,
 | |
|         CancellationToken? ct = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
 | |
|     {
 | |
|         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await _tokenProvider.GetOrUpdateToken(ct!.Value).ConfigureAwait(false));
 | |
| 
 | |
|         if (requestMessage.Content != null && requestMessage.Content is not StreamContent && requestMessage.Content is not ByteArrayContent)
 | |
|         {
 | |
|             var content = await ((JsonContent)requestMessage.Content).ReadAsStringAsync().ConfigureAwait(false);
 | |
|             Logger.LogDebug("Sending {method} to {uri} (Content: {content})", requestMessage.Method, requestMessage.RequestUri, content);
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             Logger.LogDebug("Sending {method} to {uri}", requestMessage.Method, requestMessage.RequestUri);
 | |
|         }
 | |
| 
 | |
|         try
 | |
|         {
 | |
|             if (ct != null)
 | |
|                 return await _httpClient.SendAsync(requestMessage, httpCompletionOption, ct.Value).ConfigureAwait(false);
 | |
|             return await _httpClient.SendAsync(requestMessage, httpCompletionOption).ConfigureAwait(false);
 | |
|         }
 | |
|         catch (Exception ex)
 | |
|         {
 | |
|             Logger.LogWarning(ex, "Error during SendRequestInternal for {uri}", requestMessage.RequestUri);
 | |
|             throw;
 | |
|         }
 | |
|     }
 | |
| } |