Skip to content

Commit a23dd90

Browse files
Merge pull request #8 from easymorph/ver.1.4
Ver.1.4
2 parents 0f02a94 + 34f7119 commit a23dd90

13 files changed

+175
-199
lines changed

src/BusinessLogic/Commands/BrowseFilesCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task Execute(Parameters parameters)
3434
using (var apiSession = await OpenSession(parameters))
3535
{
3636

37-
var data = await _apiClient.BrowseSpaceAsync(apiSession, parameters.Location, _cancellationTokenSource.Token);
37+
var data = await _apiClient.SpaceBrowseAsync(apiSession, parameters.Location, _cancellationTokenSource.Token);
3838
_output.WriteInfo("Space: " + data.SpaceName);
3939
_output.WriteInfo("Free space: " + data.FreeSpaceBytes + " bytes");
4040
foreach (var folder in data.Folders)

src/BusinessLogic/Commands/DeleteFileCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task Execute(Parameters parameters)
3030
using (var apiSession = await OpenSession(parameters))
3131
{
3232
_output.WriteInfo(string.Format("Deleting file {0} in space {1}...", parameters.Target, apiSession.SpaceName));
33-
await _apiClient.DeleteFileAsync(apiSession, parameters.Target, null, _cancellationTokenSource.Token);
33+
await _apiClient.SpaceDeleteFileAsync(apiSession, parameters.Target, _cancellationTokenSource.Token);
3434
_output.WriteInfo("Operation completed");
3535
}
3636

src/BusinessLogic/Commands/DownloadFileCommand.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task Execute(Parameters parameters)
4747
_output.WriteInfo(string.Format("Downloading file '{0}' from space '{1}' into '{2}'...", parameters.Source, apiSession.SpaceName, parameters.Target));
4848

4949
ProgressBar progress = new ProgressBar(_output, 40);
50-
_apiClient.FileProgress += (object sender, FileEventArgs e) =>
50+
_apiClient.OnDataDownloadProgress += (object sender, FileTransferProgressEventArgs e) =>
5151
{
5252
if (e.State == FileProgressState.Starting)
5353
{
@@ -82,16 +82,20 @@ public async Task Execute(Parameters parameters)
8282
{
8383
try
8484
{
85-
await _apiClient.DownloadFileAsync(apiSession, parameters.Source, (fileInfo) =>
85+
86+
using (var serverStreamingData = await _apiClient.SpaceOpenStreamingDataAsync(apiSession, parameters.Source, _cancellationTokenSource.Token))
87+
using(var reader = new BinaryReader(serverStreamingData.Stream))
8688
{
87-
destFileName = Path.Combine(parameters.Target, fileInfo.FileName);
89+
90+
destFileName = Path.Combine(parameters.Target, serverStreamingData.FileName);
8891

8992
if (!parameters.YesToAll && File.Exists(destFileName))
9093
throw new FileExistsException("File already exists");
9194
allowLoading = true;
92-
return true;
9395

94-
}, streamToWriteTo, _cancellationTokenSource.Token);
96+
await serverStreamingData.Stream.CopyToAsync(streamToWriteTo, 81920, _cancellationTokenSource.Token);
97+
}
98+
9599
}
96100
catch (FileExistsException)
97101
{
@@ -120,10 +124,10 @@ await _apiClient.DownloadFileAsync(apiSession, parameters.Source, (fileInfo) =>
120124
if (allowLoading)
121125
{
122126
_output.WriteInfo(string.Format("Downloading '{0}' ...", parameters.Source));
123-
await _apiClient.DownloadFileAsync(apiSession, parameters.Source, (fileInfo) =>
127+
using (var serverStreamingData = await _apiClient.SpaceOpenStreamingDataAsync(apiSession, parameters.Source, _cancellationTokenSource.Token))
124128
{
125-
return true;
126-
}, streamToWriteTo, _cancellationTokenSource.Token);
129+
await serverStreamingData.Stream.CopyToAsync(streamToWriteTo, 81920, _cancellationTokenSource.Token);
130+
}
127131
}
128132
}
129133

src/BusinessLogic/Commands/RunTaskAndForgetCommand.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ public async Task Execute(Parameters parameters)
3737
{
3838

3939
var info = await _apiClient.StartTaskAsync(apiSession,
40-
parameters.TaskId.Value,
41-
_cancellationTokenSource.Token,
42-
parameters.TaskRunParameters.Select(x => new TaskStringParameter(x.Name, x.Value)).ToArray()
40+
new StartTaskRequest()
41+
{
42+
TaskId = parameters.TaskId.Value,
43+
TaskParameters = parameters.TaskRunParameters.Select(x => new TaskStringParameter(x.Name, x.Value)).ToArray()
44+
},
45+
_cancellationTokenSource.Token
4346
);
4447
_output.WriteInfo(string.Format("Project '{0}' is running.", info.ProjectName));
4548
}

src/BusinessLogic/Commands/RunTaskAndWaitCommand.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@ namespace MorphCmd.BusinessLogic.Commands
1313
{
1414
internal class RunTaskAndWaitCommand : BaseCommand, ICommand
1515
{
16-
16+
1717

1818
public RunTaskAndWaitCommand(IOutputEndpoint output, IInputEndpoint input, IMorphServerApiClient apiClient) : base(output, input, apiClient)
1919
{
20-
20+
2121
}
2222

2323
public bool IsApiSessionRequired => true;
24-
24+
2525

2626
public async Task Execute(Parameters parameters)
27-
{
27+
{
2828

2929
if (parameters.TaskId == null)
3030
{
3131
throw new WrongCommandFormatException("TaskId is required");
3232
}
33-
_output.WriteInfo($"Attempting to start task {parameters.TaskId.Value.ToString("D")} in space '{parameters.SpaceName}'" );
33+
_output.WriteInfo($"Attempting to start task {parameters.TaskId.Value.ToString("D")} in space '{parameters.SpaceName}'");
3434
foreach (var parameter in parameters.TaskRunParameters)
3535
{
3636
_output.WriteInfo($"Parameter '{parameter.Name}' = '{parameter.Value}'");
3737
}
3838

3939
using (var apiSession = await OpenSession(parameters))
40-
{
40+
{
4141

4242
var status = await _apiClient.GetTaskStatusAsync(apiSession, parameters.TaskId.Value, _cancellationTokenSource.Token);
4343
if (status.IsRunning)
@@ -47,9 +47,13 @@ public async Task Execute(Parameters parameters)
4747

4848
var info = await _apiClient.StartTaskAsync(
4949
apiSession,
50-
parameters.TaskId.Value,
51-
_cancellationTokenSource.Token,
52-
parameters.TaskRunParameters.Select(x => new TaskStringParameter(x.Name, x.Value)).ToArray());
50+
new StartTaskRequest()
51+
{
52+
TaskId = parameters.TaskId.Value,
53+
TaskParameters = parameters.TaskRunParameters.Select(x => new TaskStringParameter(x.Name, x.Value)).ToArray()
54+
},
55+
_cancellationTokenSource.Token);
56+
5357

5458
_output.WriteInfo(string.Format("Project '{0}' is running. Waiting until done.", info.ProjectName));
5559

src/BusinessLogic/Commands/ServerStatusCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public async Task Execute(Parameters parameters)
2424
_output.WriteInfo("Retrieving server status...");
2525
var status = await _apiClient.GetServerStatusAsync(_cancellationTokenSource.Token);
2626
_output.WriteInfo("STATUS:");
27-
_output.WriteInfo(string.Format("StatusCode: {0}\nStatusMessage: {1}\nServerVersion:{2}", status.StatusCode, status.StatusMessage, status.Version.ToString()));
27+
_output.WriteInfo($"StatusCode: {status.StatusCode}");
28+
_output.WriteInfo($"StatusMessage: {status.StatusMessage}");
29+
_output.WriteInfo($"ServerVersion: {status.Version}");
30+
_output.WriteInfo($"InstanceRunId: {status.InstanceRunId}");
2831

2932
}
3033
}

src/BusinessLogic/Commands/SpaceStatusCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task Execute(Parameters parameters)
2929
var data = await _apiClient.GetSpaceStatusAsync(apiSession, _cancellationTokenSource.Token);
3030
_output.WriteInfo("Space: " + data.SpaceName);
3131
_output.WriteInfo("IsPublic: " + data.IsPublic);
32-
_output.WriteInfo("Permissions: " + string.Join(", ", data.SpacePermissions));
32+
_output.WriteInfo("Permissions: " + string.Join(", ", data.UserPermissions));
3333

3434
_output.WriteInfo("done");
3535
}

src/BusinessLogic/Commands/UploadFileCommand.cs

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task Execute(Parameters parameters)
3030
throw new WrongCommandFormatException("Source is required");
3131
}
3232

33-
33+
3434
if (string.IsNullOrWhiteSpace(parameters.Target))
3535
{
3636
_output.WriteInfo(string.Format("Uploading file '{0}' to the root folder of space '{1}'...", parameters.Source, parameters.SpaceName));
@@ -48,7 +48,7 @@ public async Task Execute(Parameters parameters)
4848

4949

5050
ProgressBar progress = null;
51-
_apiClient.FileProgress += (object sender, FileEventArgs e) =>
51+
_apiClient.OnDataUploadProgress += (object sender, FileTransferProgressEventArgs e) =>
5252
{
5353
if (e.State == FileProgressState.Starting)
5454
{
@@ -64,8 +64,8 @@ public async Task Execute(Parameters parameters)
6464
progress.Dispose();
6565
progress = null;
6666
}
67-
else if(e.State == FileProgressState.Cancelled)
68-
{
67+
else if (e.State == FileProgressState.Cancelled)
68+
{
6969
progress.Dispose();
7070
progress = null;
7171
_output.WriteError("File upload canceled.");
@@ -74,51 +74,56 @@ public async Task Execute(Parameters parameters)
7474

7575
using (var apiSession = await OpenSession(parameters))
7676
{
77-
var browsing = await _apiClient.BrowseSpaceAsync(apiSession, parameters.Target, _cancellationTokenSource.Token);
78-
if (!browsing.CanUploadFiles)
79-
{
80-
throw new Exception("Uploading to this space is disabled");
81-
}
8277

83-
84-
if (parameters.YesToAll)
85-
{
86-
// don't care if file exists.
87-
_output.WriteInfo(string.Format("YES key was passed. File will be overridden if it already exists"));
88-
await _apiClient.UploadFileAsync(apiSession, parameters.Source, parameters.Target, _cancellationTokenSource.Token, overwriteFileifExists: true);
89-
}
90-
else
9178
{
79+
var spaceBrowsing = await _apiClient.SpaceBrowseAsync(apiSession, parameters.Target, _cancellationTokenSource.Token);
80+
var spaceStatus = await _apiClient.GetSpaceStatusAsync(apiSession, _cancellationTokenSource.Token);
81+
if (!spaceStatus.UserPermissions.Contains(UserSpacePermission.FileUpload))
82+
{
83+
throw new Exception("Uploading to this space is disabled");
84+
}
85+
86+
var transferUtility = new DataTransferUtility(_apiClient, apiSession);
9287

93-
var fileExists = browsing.FileExists(Path.GetFileName(parameters.Source));
94-
if (fileExists)
88+
if (parameters.YesToAll)
9589
{
96-
if (_output.IsOutputRedirected)
97-
{
98-
_output.WriteError(string.Format("Unable to upload file '{0}' due to file already exists. Use /y to override it ", parameters.Target));
99-
}
100-
else
90+
// don't care if file exists.
91+
_output.WriteInfo(string.Format("YES key was passed. File will be overridden if it already exists"));
92+
await transferUtility.SpaceUploadFileAsync(parameters.Source, parameters.Target, _cancellationTokenSource.Token, overwriteExistingFile: true);
93+
}
94+
else
95+
{
96+
97+
var fileExists = spaceBrowsing.FileExists(Path.GetFileName(parameters.Source));
98+
if (fileExists)
10199
{
102-
_output.WriteInfo("Uploading file already exists. Would you like to override it? Y/N");
103-
_output.WriteInfo("You may pass /y parameter to override file without any questions");
104-
var answer = _input.ReadLine();
105-
if (answer.Trim().ToLowerInvariant().StartsWith("y"))
100+
if (_output.IsOutputRedirected)
106101
{
107-
_output.WriteInfo("Uploading file...");
108-
await _apiClient.UploadFileAsync(apiSession, parameters.Source, parameters.Target, _cancellationTokenSource.Token, overwriteFileifExists: true);
109-
_output.WriteInfo("Operation complete");
102+
_output.WriteError(string.Format("Unable to upload file '{0}' due to file already exists. Use /y to override it ", parameters.Target));
110103
}
111104
else
112105
{
113-
_output.WriteInfo("Operation canceled");
106+
_output.WriteInfo("Uploading file already exists. Would you like to override it? Y/N");
107+
_output.WriteInfo("You may pass /y parameter to override file without any questions");
108+
var answer = _input.ReadLine();
109+
if (answer.Trim().ToLowerInvariant().StartsWith("y"))
110+
{
111+
_output.WriteInfo("Uploading file...");
112+
await transferUtility.SpaceUploadFileAsync(parameters.Source, parameters.Target, _cancellationTokenSource.Token, overwriteExistingFile: true);
113+
_output.WriteInfo("Operation complete");
114+
}
115+
else
116+
{
117+
_output.WriteInfo("Operation canceled");
118+
}
114119
}
115120
}
116-
}
117-
else
118-
{
119-
await _apiClient.UploadFileAsync(apiSession, parameters.Source, parameters.Target, _cancellationTokenSource.Token, overwriteFileifExists: false);
120-
}
121+
else
122+
{
123+
await transferUtility.SpaceUploadFileAsync(parameters.Source, parameters.Target, _cancellationTokenSource.Token, overwriteExistingFile: false);
124+
}
121125

126+
}
122127
}
123128
}
124129
}

src/BusinessLogic/CommandsHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public CommandsHandler(IOutputEndpoint output, IInputEndpoint input, IMorphServe
3535
}
3636
public async Task Handle(Parameters parameters)
3737
{
38+
39+
40+
3841
ApiSession apiSession = null;
3942

4043
var cmd = CommandsFactory.Construct(parameters.Command, _output, _input, _apiClient);

src/BusinessLogic/NetworkUtil.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using System;
1+
using Morph.Server.Sdk.Client;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Net;
6+
#if NETCOREAPP2_0
7+
using System.Net.Http;
8+
#endif
59
using System.Net.Security;
610
using System.Security.Cryptography.X509Certificates;
711
using System.Text;
@@ -35,7 +39,30 @@ public static void ConfigureServicePointManager(bool suppressSslErrors)
3539

3640
};
3741
// Allow SSL3. Default value is: Tls, Tls11, Tls12
42+
#if NET45
3843
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
44+
#else
45+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
46+
#endif
3947
}
48+
49+
#if NETCOREAPP2_0
50+
internal static void ConfigureServerCertificateCustomValidationCallback(bool suppressSslErrors)
51+
{
52+
53+
MorphServerApiClientGlobalConfig.ServerCertificateCustomValidationCallback = (request, certificate, chain, sslPolicyErrors) =>
54+
{
55+
if (suppressSslErrors)
56+
{
57+
return (!sslPolicyErrors.HasFlag(SslPolicyErrors.RemoteCertificateNotAvailable));
58+
}
59+
else
60+
{
61+
return !(sslFatalErrors.Any(x => sslPolicyErrors.HasFlag(x)));
62+
}
63+
};
64+
65+
}
66+
#endif
4067
}
4168
}

0 commit comments

Comments
 (0)