ClientsFileHub.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. namespace EEMSServerCore.Hubs;
  2. internal partial class ClientsMainHub
  3. {
  4. private readonly ConcurrentDictionary<Guid, MemoryStream> _streamBuffer = [];
  5. public readonly ConcurrentDictionary<Guid, string> _savePath = [];
  6. public Task<bool> FilePack(Guid guid, byte[] buffer, int current, int total)
  7. {
  8. Debug.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. string path = Path.Combine(Environment.CurrentDirectory, "Receive");
  21. if (!Compressor.DecompressZipFile(new(path), 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. }