FileHub.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Windows;
  2. namespace EEMSService.Hubs;
  3. internal partial class ClientsHub : HubBase
  4. {
  5. private static readonly ConcurrentDictionary<Guid, MemoryStream> _streamBuffer = [];
  6. public Task<bool> FilePack(Guid guid, byte[] buffer, int current, int total)
  7. {
  8. Console.WriteLine($"FilePack {guid} {current} {total}");
  9. if (!_streamBuffer.TryGetValue(guid, out MemoryStream? stream) || stream is null)
  10. {
  11. if (current != 1)
  12. return Task.FromResult(false);
  13. stream = new();
  14. _streamBuffer[guid] = stream;
  15. Timer timer = new(FileCleanerTimerCallback, guid, 600000, Timeout.Infinite);
  16. }
  17. stream.Write(buffer);
  18. if (current != total)
  19. return Task.FromResult(true);
  20. if (!Compressor.DecompressZipFile(Environment.CurrentDirectory, stream))
  21. return Task.FromResult(false);
  22. if (_streamBuffer.TryRemove(guid, out MemoryStream? memoryStream) && memoryStream is not null)
  23. memoryStream.Dispose();
  24. MessageBox.Show("File Received");
  25. return Task.FromResult(true);
  26. }
  27. private void FileCleanerTimerCallback(object? state)
  28. {
  29. if (state is not Guid guid)
  30. return;
  31. if (_streamBuffer.TryRemove(guid, out MemoryStream? memoryStream) && memoryStream is not null)
  32. memoryStream.Dispose();
  33. }
  34. }