FileHub.cs 1.2 KB

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