From 3d6a882cf14f36297d8b379e0fdf65376064edf7 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sat, 28 Mar 2020 10:02:12 +0300 Subject: Many changes and improvements. --- .../FileSystem/DefaultFileSystemService.cs | 98 ++++++++++++++++++++-- 1 file changed, 91 insertions(+), 7 deletions(-) (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs index edb004344..a7f77855a 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs @@ -33,6 +33,7 @@ namespace Tango.PPC.Common.FileSystem public String Id { get; set; } public String Path { get; set; } public bool IsPathTempZip { get; set; } + public String UploadPostPath { get; set; } public FileSystemOperation(FileSystemOperationMode mode, String path) { @@ -84,6 +85,7 @@ namespace Tango.PPC.Common.FileSystem webRtcTransporter.ComponentName = "File System Passive WebRTC Transporter"; webRtcTransporter.UseKeepAlive = false; webRtcTransporter.RegisterRequestHandler(WebRtcChunkDownloadRequestReceived); + webRtcTransporter.RegisterRequestHandler(WebRtcChunkUploadRequestReceived); await webRtcTransporter.Connect(); await receiver.SendGenericResponse(new InitWebRtcResponse(), token); _webRtcClients[receiver] = webRtcTransporter; @@ -99,6 +101,11 @@ namespace Tango.PPC.Common.FileSystem OnChunkDownloadRequest(request, token, transporter); } + private void WebRtcChunkUploadRequestReceived(ITransporter transporter, ChunkUploadRequest request, string token) + { + OnChunkUploadRequest(request, token, transporter); + } + [ExternalBridgeRequestHandlerMethod(typeof(GetFileSystemItemRequest))] public async void OnGetFileSystemItemRequest(GetFileSystemItemRequest request, String token, ExternalBridgeReceiver receiver) { @@ -118,9 +125,10 @@ namespace Tango.PPC.Common.FileSystem { try { - using (var stream = new FileStream(request.Path, FileMode.Create)) { } + var tempFile = TemporaryManager.CreateFile(); + using (var stream = new FileStream(tempFile, FileMode.Create)) { } - FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, request.Path); + FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, tempFile) { UploadPostPath = request.Path }; _operations.Add(operation.Id, operation); await receiver.SendGenericResponse(new FileUploadResponse() { OperationId = operation.Id }, token); @@ -131,6 +139,25 @@ namespace Tango.PPC.Common.FileSystem } } + [ExternalBridgeRequestHandlerMethod(typeof(FolderUploadRequest))] + public async void OnFolderUploadRequest(FolderUploadRequest request, String token, ExternalBridgeReceiver receiver) + { + try + { + var tempFile = TemporaryManager.CreateFile(); + using (var stream = new FileStream(tempFile, FileMode.Create)) { } + + FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, tempFile) { UploadPostPath = request.Path, IsPathTempZip = true }; + _operations.Add(operation.Id, operation); + + await receiver.SendGenericResponse(new FolderUploadResponse() { OperationId = operation.Id }, token); + } + catch (Exception ex) + { + await receiver.SendErrorResponse(ex, token); + } + } + [ExternalBridgeRequestHandlerMethod(typeof(FileDownloadRequest))] public async void OnFileDownloadRequest(FileDownloadRequest request, String token, ExternalBridgeReceiver receiver) { @@ -191,7 +218,7 @@ namespace Tango.PPC.Common.FileSystem } [ExternalBridgeRequestHandlerMethod(typeof(ChunkUploadRequest))] - public async void OnChunkUploadRequest(ChunkUploadRequest request, String token, ExternalBridgeReceiver receiver) + public async void OnChunkUploadRequest(ChunkUploadRequest request, String token, ITransporter receiver) { try { @@ -209,6 +236,32 @@ namespace Tango.PPC.Common.FileSystem stream.Write(request.Data, 0, request.Data.Length); } + if (request.IsCompleted) + { + if (!operation.IsPathTempZip) + { + File.Copy(operation.Path, operation.UploadPostPath, true); + try + { + File.Delete(operation.Path); + } + catch { } + } + else + { + using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(operation.Path)) + { + zip.ExtractAll(operation.UploadPostPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently); + } + + try + { + File.Delete(operation.Path); + } + catch { } + } + } + await receiver.SendGenericResponse(new ChunkUploadResponse(), token, new TransportResponseConfig() { Priority = QueuePriority.Low }); } catch (Exception ex) @@ -230,12 +283,19 @@ namespace Tango.PPC.Common.FileSystem } FileStream stream = null; + bool removeTempZipFile = false; try { stream = new FileStream(operation.Path, FileMode.Open); stream.Position = request.Position; byte[] data = new byte[Math.Min(request.MaxChunkSize, stream.Length - stream.Position)]; + + if (stream.Position + data.Length == stream.Length) + { + removeTempZipFile = true; + } + await stream.ReadAsync(data, 0, data.Length); stream.Dispose(); stream = null; @@ -249,6 +309,20 @@ namespace Tango.PPC.Common.FileSystem stream?.Dispose(); await receiver.SendErrorResponse(ex, token); } + finally + { + if (operation.IsPathTempZip && removeTempZipFile) + { + try + { + if (File.Exists(operation.Path)) + { + File.Delete(operation.Path); + } + } + catch { } + } + } } [ExternalBridgeRequestHandlerMethod(typeof(AbortOperationRequest))] @@ -271,10 +345,6 @@ namespace Tango.PPC.Common.FileSystem { File.Delete(operation.Path); } - else if (Directory.Exists(operation.Path)) - { - Directory.Delete(operation.Path, true); - } } else if (operation.IsPathTempZip) { @@ -348,6 +418,20 @@ namespace Tango.PPC.Common.FileSystem } } + [ExternalBridgeRequestHandlerMethod(typeof(PerformDiskSpaceOptimizationRequest))] + public async void OnPerformDiskSpaceOptimizationRequest(PerformDiskSpaceOptimizationRequest request, String token, ExternalBridgeReceiver receiver) + { + try + { + var deletedBytes = _manager.PerformDiskSpaceOptimization(); + await receiver.SendGenericResponse(new PerformDiskSpaceOptimizationResponse() { DeletedBytes = deletedBytes }, token); + } + catch (Exception ex) + { + await receiver.SendErrorResponse(ex, token); + } + } + public void OnReceiverDisconnected(ExternalBridgeReceiver receiver) { if (_webRtcClients.ContainsKey(receiver)) -- cgit v1.3.1