ClientProvider.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Diagnostics;
  2. using Universal;
  3. namespace EEMSClientCore;
  4. public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider baseProvider) : IClientProvider
  5. {
  6. public Task<bool> RequestFile(Guid guid, FileType fileType)
  7. {
  8. Debug.WriteLine($"RequestFile {guid} {fileType}");
  9. string? filePath = fileType switch
  10. {
  11. FileType.Config => baseProvider.ConfigPath,
  12. FileType.Recipe => baseProvider.RecipePath,
  13. _ => default
  14. };
  15. if (string.IsNullOrEmpty(filePath))
  16. return Task.FromResult(false);
  17. if (!Directory.Exists(filePath))
  18. return Task.FromResult(false);
  19. using MemoryStream stream = new();
  20. Compressor.CompressZipFileDirectory(new(filePath), stream);
  21. return SendFile(guid, stream);
  22. }
  23. private async Task<bool> SendFile(Guid guid, MemoryStream stream)
  24. {
  25. if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
  26. return false;
  27. for (int i = 0; i < buffer.Count; i++)
  28. {
  29. try
  30. {
  31. if (!await clientCaller.FilePack(guid, buffer[i], i + 1, buffer.Count))
  32. return false;
  33. }
  34. catch
  35. {
  36. return false;
  37. }
  38. }
  39. return true;
  40. }
  41. }
  42. public interface IClientBaseProvider
  43. {
  44. public string? RecipePath { get; set; }
  45. public string? ConfigPath { get; set; }
  46. }