diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-03-19 16:16:48 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-03-19 16:16:48 +0200 |
| commit | 7f0b3b209792cb935b9cfca4542c45318a309717 (patch) | |
| tree | 88fd8e48e496d41f22c663c41f4dc5701cc8ea8a /Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs | |
| parent | 376d9a518e33649ccbd9ea0091acc2b888cc4c71 (diff) | |
| download | Tango-7f0b3b209792cb935b9cfca4542c45318a309717.tar.gz Tango-7f0b3b209792cb935b9cfca4542c45318a309717.zip | |
Working on FSE/PPC download file system
Diffstat (limited to 'Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs')
| -rw-r--r-- | Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs | 123 |
1 files changed, 119 insertions, 4 deletions
diff --git a/Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs b/Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs index 48da65f16..f9f08c6a4 100644 --- a/Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs +++ b/Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs @@ -1,8 +1,12 @@ using System; using System.Collections.Generic; +using System.IO; +using System.IO.Compression; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.Core; +using Tango.Core.Threading; using Tango.FileSystem; using Tango.FileSystem.Network; using Tango.FSE.Common.Connection; @@ -11,7 +15,7 @@ using Tango.Transport; namespace Tango.FSE.UI.FileSystem { - public class DefaultFileSystemProvider : IFileSystemProvider + public class DefaultFileSystemProvider : ExtendedObject, IFileSystemProvider { private IMachineProvider _machineProvider; @@ -59,12 +63,123 @@ namespace Tango.FSE.UI.FileSystem return FileSystemItem.FromDTO(response.FileSystemItem) as IFileSystemContainer; } - public Task<FileSystemHandler> Download(FileSystemItem item, string targetFolderPath) + public async Task<FileSystemHandler> Download(FileSystemItem item, string localTargetFolder) { - throw new NotImplementedException(); + String operationId = String.Empty; + String destination = String.Empty; + long downloadLength = 0; + + if (item.Type == FileSystemItemType.File) + { + destination = Path.Combine(localTargetFolder, item.Name); + + var response = await _machineProvider.MachineOperator.SendGenericRequest<FileDownloadRequest, FileDownloadResponse>( + new FileDownloadRequest() + { + Path = item.Path + }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(20) }); + + operationId = response.OperationId; + downloadLength = response.Length; + } + else if (item.Type == FileSystemItemType.Folder) + { + destination = Path.Combine(localTargetFolder, item.Name); + + var response = await _machineProvider.MachineOperator.SendGenericRequest<FolderDownloadRequest, FolderDownloadResponse>( + new FolderDownloadRequest() + { + Path = item.Path + }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(20) }); + + operationId = response.OperationId; + downloadLength = response.Length; + } + else + { + throw new NotSupportedException("The requested file system item is not supported for downloading."); + } + + bool aborted = false; + + FileSystemHandler handler = new FileSystemHandler(item.Name, destination, async () => + { + if (!aborted) + { + aborted = true; + try + { + var response = await _machineProvider.MachineOperator.SendGenericRequest<AbortOperationRequest, AbortOperationResponse>( + new AbortOperationRequest() + { + OperationId = operationId + }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(30) }); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error aborting the download operation."); + } + } + }); + + long position = 0; + + ThreadFactory.StartNew(async () => + { + var tempFile = TemporaryManager.CreateFile(); + + while (position < downloadLength && !aborted) + { + try + { + var response = await _machineProvider.MachineOperator.SendGenericRequest<ChunkDownloadRequest, ChunkDownloadResponse>( + new ChunkDownloadRequest() + { + OperationId = operationId, + Position = position, + }); + + 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; + } + } + + 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(); + } + } + catch (Exception ex) + { + handler.RaiseFailed(ex); + } + + handler.RaiseCompleted(); + }); + + return handler; } - public Task<FileSystemHandler> Upload(string sourcePath, string targetPath) + public Task<FileSystemHandler> Upload(string sourcePath, string remoteTargetFolder) { throw new NotImplementedException(); } |
