diff options
| author | Avi Levkovich <avi@twine-s.com> | 2020-03-25 17:43:49 +0200 |
|---|---|---|
| committer | Avi Levkovich <avi@twine-s.com> | 2020-03-25 17:43:49 +0200 |
| commit | d29da53d6f71f45749c0ede5b4cd7281ed3a270e (patch) | |
| tree | fd83afc7771c0f4f19c581e1cf407bcf7c14818b /Software/Visual_Studio/FSE/Tango.FSE.UI/FileSystem/DefaultFileSystemProvider.cs | |
| parent | 0208e9f1800c044ec3bd002b7aa7fd00621c81be (diff) | |
| download | Tango-d29da53d6f71f45749c0ede5b4cd7281ed3a270e.tar.gz Tango-d29da53d6f71f45749c0ede5b4cd7281ed3a270e.zip | |
merge
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 | 216 |
1 files changed, 216 insertions, 0 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 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<IFileSystemContainer> GetFolder(string path) + { + var response = await _machineProvider.MachineOperator.SendGenericRequest<GetFileSystemItemRequest, GetFileSystemItemResponse>(new GetFileSystemItemRequest() + { + Path = path + }, new TransportRequestConfig() + { + Timeout = TimeSpan.FromSeconds(30), + }); + + return FileSystemItem.FromDTO(response.FileSystemItem) as IFileSystemContainer; + } + + public async Task<IFileSystemContainer> GetSpecialFolder(Environment.SpecialFolder specialFolder) + { + var response = await _machineProvider.MachineOperator.SendGenericRequest<GetFileSystemItemRequest, GetFileSystemItemResponse>(new GetFileSystemItemRequest() + { + SpecialFolder = specialFolder + }, new TransportRequestConfig() + { + Timeout = TimeSpan.FromSeconds(30), + }); + + return FileSystemItem.FromDTO(response.FileSystemItem) as IFileSystemContainer; + } + + public async Task<IFileSystemContainer> GetThisPC() + { + var response = await _machineProvider.MachineOperator.SendGenericRequest<GetFileSystemItemRequest, GetFileSystemItemResponse>(new GetFileSystemItemRequest() + { + //No parameters at all + }, new TransportRequestConfig() + { + Timeout = TimeSpan.FromSeconds(30), + }); + + return FileSystemItem.FromDTO(response.FileSystemItem) as IFileSystemContainer; + } + + public Task<FileSystemHandler> 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<AbortOperationRequest, AbortOperationResponse>( + 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<FileDownloadRequest, FileDownloadResponse>( + 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<FolderDownloadRequest, FolderDownloadResponse>( + 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<ChunkDownloadRequest, ChunkDownloadResponse>( + 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<FileSystemHandler> Upload(string sourcePath, string remoteTargetFolder) + { + throw new NotImplementedException(); + } + } +} |
