aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs
blob: ffbba2e7a56751b6b4c4862b92d324e1c2d0d6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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.DI;
using Tango.FileSystem;
using Tango.FileSystem.Network;
using Tango.Integration.ExternalBridge;
using Tango.PPC.Common.ExternalBridge;
using Tango.Transport;

namespace Tango.PPC.Common.FileSystem
{
    [TangoCreateWhenRegistered]
    public class DefaultFileSystemService : ExtendedObject, IFileSystemService, IExternalBridgeRequestHandler
    {
        private enum FileSystemOperationMode
        {
            Upload,
            Download
        }

        private class FileSystemOperation
        {
            public FileSystemOperationMode Mode { get; set; }
            public String Id { get; set; }
            public String Path { get; set; }

            public FileSystemOperation(FileSystemOperationMode mode, String path)
            {
                Mode = mode;
                Id = Guid.NewGuid().ToString();
                Path = path;
            }
        }

        private FileSystemManager _manager;
        private Dictionary<String, FileSystemOperation> _operations;

        public bool Enabled { get; set; } = true;

        public DefaultFileSystemService(IPPCExternalBridgeService externalBridge)
        {
            _manager = new FileSystemManager();
            _operations = new Dictionary<string, FileSystemOperation>();
            externalBridge.RegisterRequestHandler(this);
        }

        [ExternalBridgeRequestHandlerMethod(typeof(GetFileSystemItemRequest))]
        public async void OnGetFileSystemItemRequest(GetFileSystemItemRequest request, String token, ExternalBridgeReceiver receiver)
        {
            try
            {
                FileSystemItemDTO dto = _manager.GetFolder(request);
                await receiver.SendGenericResponse(new GetFileSystemItemResponse() { FileSystemItem = dto }, token);
            }
            catch (Exception ex)
            {
                await receiver.SendErrorResponse(ex, token);
            }
        }

        [ExternalBridgeRequestHandlerMethod(typeof(FileUploadRequest))]
        public async void OnFileUploadRequest(FileUploadRequest request, String token, ExternalBridgeReceiver receiver)
        {
            try
            {
                using (var stream = new FileStream(request.Path, FileMode.Create)) { }

                FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, request.Path);
                _operations.Add(operation.Id, operation);

                await receiver.SendGenericResponse(new FileUploadResponse() { 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)
        {
            try
            {
                if (!File.Exists(request.Path))
                {
                    await receiver.SendErrorResponse(new FileNotFoundException("Could not find the specified file."), token);
                    return;
                }

                FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Download, request.Path);

                _operations.Add(operation.Id, operation);

                await receiver.SendGenericResponse(new FileDownloadResponse()
                {
                    OperationId = operation.Id,
                    Length = new FileInfo(request.Path).Length
                }, token);
            }
            catch (Exception ex)
            {
                await receiver.SendErrorResponse(ex, token);
            }
        }

        [ExternalBridgeRequestHandlerMethod(typeof(ChunkUploadRequest))]
        public async void OnChunkUploadRequest(ChunkUploadRequest request, String token, ExternalBridgeReceiver receiver)
        {
            try
            {
                FileSystemOperation operation;
                _operations.TryGetValue(request.OperationId, out operation);

                if (operation == null)
                {
                    await receiver.SendErrorResponse(new ArgumentException("Invalid operation id."), token);
                    return;
                }

                using (var stream = new FileStream(operation.Path, FileMode.Append))
                {
                    stream.Write(request.Data, 0, request.Data.Length);
                }

                await receiver.SendGenericResponse(new ChunkUploadResponse(), token);
            }
            catch (Exception ex)
            {
                await receiver.SendErrorResponse(ex, token);
            }
        }

        [ExternalBridgeRequestHandlerMethod(typeof(ChunkDownloadRequest))]
        public async void OnChunkDownloadRequest(ChunkDownloadRequest request, String token, ExternalBridgeReceiver receiver)
        {
            FileSystemOperation operation;
            _operations.TryGetValue(request.OperationId, out operation);

            if (operation == null)
            {
                await receiver.SendErrorResponse(new ArgumentException("Invalid operation id."), token);
                return;
            }

            using (FileStream stream = new FileStream(operation.Path, FileMode.Open))
            {
                stream.Position = request.Position;
                byte[] data = new byte[Math.Min(request.MaxChunkSize, stream.Length - stream.Position)];
                await stream.ReadAsync(data, 0, data.Length);
                await receiver.SendGenericResponse(new ChunkDownloadResponse()
                {
                    Data = data
                }, token);
            }
        }

        [ExternalBridgeRequestHandlerMethod(typeof(AbortOperationRequest))]
        public async void OnAbortOperationRequest(AbortOperationRequest request, String token, ExternalBridgeReceiver receiver)
        {
            FileSystemOperation operation;
            _operations.TryGetValue(request.OperationId, out operation);

            if (operation == null)
            {
                await receiver.SendErrorResponse(new ArgumentException("Invalid operation id."), token);
                return;
            }

            try
            {
                if (operation.Mode == FileSystemOperationMode.Upload)
                {
                    if (File.Exists(operation.Path))
                    {
                        File.Delete(operation.Path);
                    }
                    else if (Directory.Exists(operation.Path))
                    {
                        Directory.Delete(operation.Path, true);
                    }
                }

                await receiver.SendGenericResponse(new AbortOperationResponse(), token);
            }
            catch (Exception ex)
            {
                await receiver.SendErrorResponse(ex, token);
            }
        }

        public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
        {

        }
    }
}