From d29da53d6f71f45749c0ede5b4cd7281ed3a270e Mon Sep 17 00:00:00 2001 From: Avi Levkovich Date: Wed, 25 Mar 2020 17:43:49 +0200 Subject: merge --- .../FileSystem/DefaultFileSystemProvider.cs | 216 +++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs (limited to 'Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs') diff --git a/Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs b/Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs new file mode 100644 index 000000000..bcc39d11d --- /dev/null +++ b/Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs @@ -0,0 +1,216 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Core.Threading; +using Tango.FileSystem; +using Tango.FileSystem.Network; +using Tango.FSE.Common.Connection; +using Tango.FSE.Common.FileSystem; +using Tango.Transport; + +namespace Tango.FSE.UI.FileSystem +{ + public class DefaultFileSystemProvider : ExtendedObject, IFileSystemProvider + { + private IMachineProvider _machineProvider; + + public DefaultFileSystemProvider(IMachineProvider machineProvider) + { + _machineProvider = machineProvider; + } + + public async Task GetFolder(string path) + { + var response = await _machineProvider.MachineOperator.SendGenericRequest(new GetFileSystemItemRequest() + { + Path = path + }, new TransportRequestConfig() + { + Timeout = TimeSpan.FromSeconds(30), + }); + + return FileSystemItem.FromDTO(response.FileSystemItem) as IFileSystemContainer; + } + + public async Task GetSpecialFolder(Environment.SpecialFolder specialFolder) + { + var response = await _machineProvider.MachineOperator.SendGenericRequest(new GetFileSystemItemRequest() + { + SpecialFolder = specialFolder + }, new TransportRequestConfig() + { + Timeout = TimeSpan.FromSeconds(30), + }); + + return FileSystemItem.FromDTO(response.FileSystemItem) as IFileSystemContainer; + } + + public async Task GetThisPC() + { + var response = await _machineProvider.MachineOperator.SendGenericRequest(new GetFileSystemItemRequest() + { + //No parameters at all + }, new TransportRequestConfig() + { + Timeout = TimeSpan.FromSeconds(30), + }); + + return FileSystemItem.FromDTO(response.FileSystemItem) as IFileSystemContainer; + } + + public Task Download(FileSystemItem item, string localTargetFolder) + { + String operationId = String.Empty; + String destination = String.Empty; + long downloadLength = 0; + bool aborted = false; + + FileSystemHandler handler = null; + + destination = Path.Combine(localTargetFolder, item.Name); + + + handler = new FileSystemHandler(item, destination, async () => + { + if (!aborted) + { + aborted = true; + try + { + var response = await _machineProvider.MachineOperator.SendGenericRequest( + new AbortOperationRequest() + { + OperationId = operationId + }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(30) }); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error aborting the download operation."); + } + finally + { + handler.RaiseAborted(); + } + } + }); + + ThreadFactory.StartNew(async () => + { + try + { + if (item.Type == FileSystemItemType.File) + { + var response = await _machineProvider.MachineOperator.SendGenericRequest( + new FileDownloadRequest() + { + Path = item.Path + }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(20) }); + + operationId = response.OperationId; + downloadLength = response.Length; + handler.OperationId = operationId; + } + else if (item.Type == FileSystemItemType.Folder) + { + var response = await _machineProvider.MachineOperator.SendGenericRequest( + new FolderDownloadRequest() + { + Path = item.Path + }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(60) }); + + operationId = response.OperationId; + downloadLength = response.Length; + handler.OperationId = operationId; + } + else + { + throw new NotSupportedException("The requested file system item is not supported for downloading."); + } + } + catch (Exception ex) + { + handler.RaiseFailed(ex); + return; + } + + long position = 0; + + var tempFile = TemporaryManager.CreateFile(); + + while (position < downloadLength && !aborted) + { + if (handler.IsPaused) + { + Thread.Sleep(1000); + continue; + } + + try + { + var response = await _machineProvider.MachineOperator.SendGenericRequest( + new ChunkDownloadRequest() + { + MaxChunkSize = 1024 * 10, + OperationId = operationId, + Position = position, + }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(30), Priority = QueuePriority.Low }); + + using (FileStream fs = new FileStream(tempFile, FileMode.Append)) + { + fs.Write(response.Data, 0, response.Data.Length); + } + + position += response.Data.Length; + handler.InvalidateProgress(position, downloadLength); + } + catch (Exception ex) + { + tempFile.Delete(); + handler.RaiseFailed(ex); + return; + } + } + + if (!aborted) + { + try + { + if (item.Type == FileSystemItemType.File) + { + File.Copy(tempFile, destination, true); + tempFile.Delete(); + } + else if (item.Type == FileSystemItemType.Folder) + { + ZipFile.ExtractToDirectory(tempFile, destination); + tempFile.Delete(); + } + + handler.RaiseCompleted(); + } + catch (Exception ex) + { + handler.RaiseFailed(ex); + } + } + else + { + tempFile.Delete(); + } + }); + + return Task.FromResult(handler); + } + + public Task Upload(string sourcePath, string remoteTargetFolder) + { + throw new NotImplementedException(); + } + } +} -- cgit v1.3.1