ClientsFileHub.cs 1.5 KB

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