ClientsFileHub.cs 1.5 KB

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