|
@@ -1,10 +1,43 @@
|
|
|
-using System.Diagnostics;
|
|
|
+using System.Collections.Concurrent;
|
|
|
+using System.Diagnostics;
|
|
|
using Universal;
|
|
|
|
|
|
namespace EEMSClientCore;
|
|
|
|
|
|
public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider baseProvider) : IClientProvider
|
|
|
{
|
|
|
+ private static readonly ConcurrentDictionary<FileType, MemoryStream> _streamBuffer = [];
|
|
|
+ public static readonly ConcurrentDictionary<FileType, string> _savePath = [];
|
|
|
+
|
|
|
+ //Receive Files from Server
|
|
|
+ public Task<bool> PushFile(Guid guid, FileType fileType, byte[] buffer, int current, int total)
|
|
|
+ {
|
|
|
+ if (!_streamBuffer.TryGetValue(fileType, out MemoryStream? stream) || stream is null)
|
|
|
+ {
|
|
|
+ if (current != 1)
|
|
|
+ return Task.FromResult(false);
|
|
|
+
|
|
|
+ stream = new();
|
|
|
+ _streamBuffer[fileType] = stream;
|
|
|
+ Timer timer = new(FileCleanerTimerCallback, fileType, 600000, Timeout.Infinite);
|
|
|
+ }
|
|
|
+
|
|
|
+ stream.Write(buffer);
|
|
|
+
|
|
|
+ if (current != total)
|
|
|
+ return Task.FromResult(true);
|
|
|
+
|
|
|
+ string path = Path.Combine(Environment.CurrentDirectory, "Receive");
|
|
|
+ if (!Compressor.DecompressZipFile(new(path), stream))
|
|
|
+ return Task.FromResult(false);
|
|
|
+
|
|
|
+ if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
|
|
|
+ memoryStream.Dispose();
|
|
|
+
|
|
|
+ return Task.FromResult(true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public Task<bool> RequestFile(Guid guid, FileType fileType)
|
|
|
{
|
|
|
Debug.WriteLine($"RequestFile {guid} {fileType}");
|
|
@@ -21,7 +54,7 @@ public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider base
|
|
|
|
|
|
if (!Directory.Exists(filePath))
|
|
|
return Task.FromResult(false);
|
|
|
-
|
|
|
+
|
|
|
using MemoryStream stream = new();
|
|
|
Compressor.CompressZipFileDirectory(new(filePath), stream);
|
|
|
|
|
@@ -47,10 +80,19 @@ public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider base
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ private void FileCleanerTimerCallback(object? state)
|
|
|
+ {
|
|
|
+ if (state is not FileType fileType)
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
|
|
|
+ memoryStream.Dispose();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public interface IClientBaseProvider
|
|
|
{
|
|
|
public string? RecipePath { get; set; }
|
|
|
public string? ConfigPath { get; set; }
|
|
|
-}
|
|
|
+}
|