Skip to content

Commit

Permalink
Merge pull request #11 from easymorph/ver.1.4
Browse files Browse the repository at this point in the history
Ver.1.4
  • Loading branch information
strongtigerman authored Mar 11, 2020
2 parents e5d2ff8 + 97333a3 commit 46c54fe
Show file tree
Hide file tree
Showing 55 changed files with 2,865 additions and 872 deletions.
177 changes: 177 additions & 0 deletions src/Client/DataTransferUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using Morph.Server.Sdk.Model;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Morph.Server.Sdk.Client
{
/// <summary>
/// Transfer file from/to server to/from local file
/// </summary>
public class DataTransferUtility : IDataTransferUtility
{
private int BufferSize { get; set; } = 81920;
private readonly IMorphServerApiClient _morphServerApiClient;
private readonly ApiSession _apiSession;

public DataTransferUtility(IMorphServerApiClient morphServerApiClient, ApiSession apiSession)
{
this._morphServerApiClient = morphServerApiClient ?? throw new ArgumentNullException(nameof(morphServerApiClient));
this._apiSession = apiSession ?? throw new ArgumentNullException(nameof(apiSession));
}

public async Task SpaceUploadFileAsync(string localFilePath, string serverFolder, CancellationToken cancellationToken, bool overwriteExistingFile = false)
{
if (!File.Exists(localFilePath))
{
throw new FileNotFoundException(string.Format("File '{0}' not found", localFilePath));
}
var fileSize = new FileInfo(localFilePath).Length;
var fileName = Path.GetFileName(localFilePath);
using (var fsSource = new FileStream(localFilePath, FileMode.Open, FileAccess.Read))
{
var request = new SpaceUploadDataStreamRequest
{
DataStream = fsSource,
FileName = fileName,
FileSize = fileSize,
OverwriteExistingFile = overwriteExistingFile,
ServerFolder = serverFolder
};
await _morphServerApiClient.SpaceUploadDataStreamAsync(_apiSession, request, cancellationToken);
return;
}
}




public async Task SpaceDownloadFileIntoFileAsync(string remoteFilePath, string targetLocalFilePath, CancellationToken cancellationToken, bool overwriteExistingFile = false)
{
if (remoteFilePath == null)
{
throw new ArgumentNullException(nameof(remoteFilePath));
}

if (targetLocalFilePath == null)
{
throw new ArgumentNullException(nameof(targetLocalFilePath));
}


var localFolder = Path.GetDirectoryName(targetLocalFilePath);
var tempFile = Path.Combine(localFolder, Guid.NewGuid().ToString("D") + ".emtmp");

if (!overwriteExistingFile && File.Exists(targetLocalFilePath))
{
throw new Exception($"Destination file '{targetLocalFilePath}' already exists.");
}


try
{
using (Stream tempFileStream = File.Open(tempFile, FileMode.Create))
{
using (var serverStreamingData = await _morphServerApiClient.SpaceOpenStreamingDataAsync(_apiSession, remoteFilePath, cancellationToken))
{
await serverStreamingData.Stream.CopyToAsync(tempFileStream, BufferSize, cancellationToken);
}
}

if (File.Exists(targetLocalFilePath))
{
File.Delete(targetLocalFilePath);
}
File.Move(tempFile, targetLocalFilePath);

}
finally
{
//drop file
if (tempFile != null && File.Exists(tempFile))
{
File.Delete(tempFile);
}

}
}

public async Task SpaceDownloadFileIntoFolderAsync(string remoteFilePath, string targetLocalFolder, CancellationToken cancellationToken, bool overwriteExistingFile = false)
{
if (remoteFilePath == null)
{
throw new ArgumentNullException(nameof(remoteFilePath));
}

if (targetLocalFolder == null)
{
throw new ArgumentNullException(nameof(targetLocalFolder));
}

string destFileName = null;
var tempFile = Path.Combine(targetLocalFolder, Guid.NewGuid().ToString("D") + ".emtmp");
try
{
using (Stream tempFileStream = File.Open(tempFile, FileMode.Create))
{

using (var serverStreamingData = await _morphServerApiClient.SpaceOpenStreamingDataAsync(_apiSession, remoteFilePath, cancellationToken))
{
destFileName = Path.Combine(targetLocalFolder, serverStreamingData.FileName);

if (!overwriteExistingFile && File.Exists(destFileName))
{
throw new Exception($"Destination file '{destFileName}' already exists.");
}

await serverStreamingData.Stream.CopyToAsync(tempFileStream, BufferSize, cancellationToken);
}
}

if (File.Exists(destFileName))
{
File.Delete(destFileName);
}
File.Move(tempFile, destFileName);

}
finally
{
//drop file
if (tempFile != null && File.Exists(tempFile))
{
File.Delete(tempFile);
}

}

}

public async Task SpaceUploadFileAsync(string localFilePath, string serverFolder, string destFileName, CancellationToken cancellationToken, bool overwriteExistingFile = false)
{
if (!File.Exists(localFilePath))
{
throw new FileNotFoundException(string.Format("File '{0}' not found", localFilePath));
}
var fileSize = new FileInfo(localFilePath).Length;

using (var fsSource = new FileStream(localFilePath, FileMode.Open, FileAccess.Read))
{
var request = new SpaceUploadDataStreamRequest
{
DataStream = fsSource,
FileName = destFileName,
FileSize = fileSize,
OverwriteExistingFile = overwriteExistingFile,
ServerFolder = serverFolder
};
await _morphServerApiClient.SpaceUploadDataStreamAsync(_apiSession, request, cancellationToken);
return;
}
}
}

}


14 changes: 14 additions & 0 deletions src/Client/IDataTransferUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading;
using System.Threading.Tasks;

namespace Morph.Server.Sdk.Client
{
public interface IDataTransferUtility
{
Task SpaceUploadFileAsync(string localFilePath, string serverFolder, CancellationToken cancellationToken, bool overwriteExistingFile = false);
Task SpaceUploadFileAsync(string localFilePath, string serverFolder, string destFileName, CancellationToken cancellationToken, bool overwriteExistingFile = false);
Task SpaceDownloadFileIntoFileAsync(string remoteFilePath, string targetLocalFilePath, CancellationToken cancellationToken, bool overwriteExistingFile = false);
Task SpaceDownloadFileIntoFolderAsync(string remoteFilePath, string targetLocalFolder, CancellationToken cancellationToken, bool overwriteExistingFile = false);
}

}
63 changes: 63 additions & 0 deletions src/Client/ILowLevelApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Morph.Server.Sdk.Dto;
using Morph.Server.Sdk.Model;
using System;
using System.Threading;
using System.Threading.Tasks;
using Morph.Server.Sdk.Dto.Commands;
using System.Collections.Generic;
using Morph.Server.Sdk.Model.InternalModels;
using Morph.Server.Sdk.Events;

namespace Morph.Server.Sdk.Client
{
internal interface ILowLevelApiClient: IDisposable
{
IRestClient RestClient { get; }

// TASKS
Task<ApiResult<TaskStatusDto>> GetTaskStatusAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken);
Task<ApiResult<SpaceTasksListDto>> GetTasksListAsync(ApiSession apiSession, CancellationToken cancellationToken);
Task<ApiResult<SpaceTaskDto>> GetTaskAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken);
Task<ApiResult<SpaceTaskDto>> TaskChangeModeAsync(ApiSession apiSession, Guid taskId, SpaceTaskChangeModeRequestDto requestDto, CancellationToken cancellationToken);

// RUN-STOP Task
Task<ApiResult<RunningTaskStatusDto>> GetRunningTaskStatusAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken);
Task<ApiResult<RunningTaskStatusDto>> StartTaskAsync(ApiSession apiSession, Guid taskId, TaskStartRequestDto taskStartRequestDto, CancellationToken cancellationToken);
Task<ApiResult<NoContentResult>> StopTaskAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken);

// Tasks validation
Task<ApiResult<ValidateTasksResponseDto>> ValidateTasksAsync(ApiSession apiSession, ValidateTasksRequestDto validateTasksRequestDto, CancellationToken cancellationToken);


// Auth and sessions
Task<ApiResult<NoContentResult>> AuthLogoutAsync(ApiSession apiSession, CancellationToken cancellationToken);
Task<ApiResult<LoginResponseDto>> AuthLoginPasswordAsync(LoginRequestDto loginRequestDto, CancellationToken cancellationToken);
Task<ApiResult<GenerateNonceResponseDto>> AuthGenerateNonce(CancellationToken cancellationToken);



// Server interaction
Task<ApiResult<ServerStatusDto>> ServerGetStatusAsync(CancellationToken cancellationToken);


// spaces

Task<ApiResult<SpacesEnumerationDto>> SpacesGetListAsync(CancellationToken cancellationToken);
Task<ApiResult<SpaceStatusDto>> SpacesGetSpaceStatusAsync(ApiSession apiSession, string spaceName, CancellationToken cancellationToken);

// WEB FILES
Task<ApiResult<SpaceBrowsingResponseDto>> WebFilesBrowseSpaceAsync(ApiSession apiSession, string folderPath, CancellationToken cancellationToken);
Task<ApiResult<bool>> WebFileExistsAsync(ApiSession apiSession, string serverFilePath, CancellationToken cancellationToken);
Task<ApiResult<NoContentResult>> WebFilesDeleteFileAsync(ApiSession apiSession, string serverFilePath, CancellationToken cancellationToken);
Task<ApiResult<FetchFileStreamData>> WebFilesDownloadFileAsync(ApiSession apiSession, string serverFilePath, Action<FileTransferProgressEventArgs> onReceiveProgress, CancellationToken cancellationToken);
Task<ApiResult<NoContentResult>> WebFilesPutFileStreamAsync(ApiSession apiSession, string serverFolder, SendFileStreamData sendFileStreamData, Action<FileTransferProgressEventArgs> onSendProgress, CancellationToken cancellationToken);
Task<ApiResult<NoContentResult>> WebFilesPostFileStreamAsync(ApiSession apiSession, string serverFolder, SendFileStreamData sendFileStreamData, Action<FileTransferProgressEventArgs> onSendProgress, CancellationToken cancellationToken);

Task<ApiResult<ServerPushStreaming>> WebFilesOpenContiniousPostStreamAsync(ApiSession apiSession, string serverFolder, string fileName, CancellationToken cancellationToken);
Task<ApiResult<ServerPushStreaming>> WebFilesOpenContiniousPutStreamAsync(ApiSession apiSession, string serverFolder, string fileName, CancellationToken cancellationToken);

}
}



45 changes: 34 additions & 11 deletions src/Client/IMorphServerApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Morph.Server.Sdk.Events;
Expand All @@ -9,29 +12,49 @@

namespace Morph.Server.Sdk.Client
{
public interface IMorphServerApiClient

public interface IHasConfig
{
event EventHandler<FileEventArgs> FileProgress;
IClientConfiguration Config { get; }
}

Task<SpaceBrowsingInfo> BrowseSpaceAsync(ApiSession apiSession, string folderPath, CancellationToken cancellationToken);
internal interface ICanCloseSession: IHasConfig, IDisposable
{
Task CloseSessionAsync(ApiSession apiSession, CancellationToken cancellationToken);
Task DeleteFileAsync(ApiSession apiSession, string serverFolder, string fileName, CancellationToken cancellationToken);
Task DownloadFileAsync(ApiSession apiSession, string remoteFilePath, Func<DownloadFileInfo, bool> handleFile, Stream streamToWriteTo, CancellationToken cancellationToken);
Task<DownloadFileInfo> DownloadFileAsync(ApiSession apiSession, string remoteFilePath, Stream streamToWriteTo, CancellationToken cancellationToken);
Task<bool> FileExistsAsync(ApiSession apiSession, string serverFolder, string fileName, CancellationToken cancellationToken);

}

public interface IMorphServerApiClient: IHasConfig, IDisposable
{
event EventHandler<FileTransferProgressEventArgs> OnDataDownloadProgress;
event EventHandler<FileTransferProgressEventArgs> OnDataUploadProgress;



Task<ServerStatus> GetServerStatusAsync(CancellationToken cancellationToken);
Task<Model.TaskStatus> GetTaskStatusAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken);
Task<ApiSession> OpenSessionAsync(OpenSessionRequest openSessionRequest, CancellationToken cancellationToken);

Task<RunningTaskStatus> StartTaskAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken, IEnumerable<TaskParameterBase> taskParameters = null);
Task<RunningTaskStatus> StartTaskAsync(ApiSession apiSession, StartTaskRequest startTaskRequest, CancellationToken cancellationToken);
Task StopTaskAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken);
Task UploadFileAsync(ApiSession apiSession, Stream inputStream, string fileName, long fileSize, string destFolderPath, CancellationToken cancellationToken, bool overwriteFileifExists = false);
Task UploadFileAsync(ApiSession apiSession, string localFilePath, string destFolderPath, string destFileName, CancellationToken cancellationToken, bool overwriteFileifExists = false);
Task UploadFileAsync(ApiSession apiSession, string localFilePath, string destFolderPath, CancellationToken cancellationToken, bool overwriteFileifExists = false);


Task<SpaceTask> TaskChangeModeAsync(ApiSession apiSession, Guid taskId, TaskChangeModeRequest taskChangeModeRequest, CancellationToken cancellationToken);
Task<ValidateTasksResult> ValidateTasksAsync(ApiSession apiSession, string projectPath, CancellationToken cancellationToken);
Task<SpacesEnumerationList> GetSpacesListAsync(CancellationToken cancellationToken);
Task<SpaceStatus> GetSpaceStatusAsync(ApiSession apiSession, CancellationToken cancellationToken);
Task<SpaceTasksList> GetTasksListAsync(ApiSession apiSession, CancellationToken cancellationToken);
Task<SpaceTask> GetTaskAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken);

Task<SpaceBrowsingInfo> SpaceBrowseAsync(ApiSession apiSession, string folderPath, CancellationToken cancellationToken);
Task SpaceDeleteFileAsync(ApiSession apiSession, string remoteFilePath, CancellationToken cancellationToken);
Task<bool> SpaceFileExistsAsync(ApiSession apiSession, string remoteFilePath, CancellationToken cancellationToken);

Task<ServerStreamingData> SpaceOpenStreamingDataAsync(ApiSession apiSession, string remoteFilePath, CancellationToken cancellationToken);
Task<Stream> SpaceOpenDataStreamAsync(ApiSession apiSession, string remoteFilePath, CancellationToken cancellationToken);

Task SpaceUploadDataStreamAsync(ApiSession apiSession, SpaceUploadDataStreamRequest spaceUploadFileRequest, CancellationToken cancellationToken);
Task<ContiniousStreamingConnection> SpaceUploadContiniousStreamingAsync(ApiSession apiSession, SpaceUploadContiniousStreamRequest continiousStreamRequest, CancellationToken cancellationToken);

}
}
43 changes: 43 additions & 0 deletions src/Client/IRestClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Specialized;
using Morph.Server.Sdk.Model.InternalModels;
using Morph.Server.Sdk.Events;
using Morph.Server.Sdk.Model;

namespace Morph.Server.Sdk.Client
{
public interface IRestClient : IDisposable
{
HttpClient HttpClient { get; set; }
Task<ApiResult<TResult>> GetAsync<TResult>(string url, NameValueCollection urlParameters, HeadersCollection headersCollection, CancellationToken cancellationToken)
where TResult : new();
Task<ApiResult<TResult>> HeadAsync<TResult>(string url, NameValueCollection urlParameters, HeadersCollection headersCollection, CancellationToken cancellationToken)
where TResult : new();
Task<ApiResult<TResult>> PostAsync<TModel, TResult>(string url, TModel model, NameValueCollection urlParameters, HeadersCollection headersCollection, CancellationToken cancellationToken)
where TResult : new();
Task<ApiResult<TResult>> PutAsync<TModel, TResult>(string url, TModel model, NameValueCollection urlParameters, HeadersCollection headersCollection, CancellationToken cancellationToken)
where TResult : new();
Task<ApiResult<TResult>> DeleteAsync<TResult>(string url, NameValueCollection urlParameters, HeadersCollection headersCollection, CancellationToken cancellationToken)
where TResult : new();
Task<ApiResult<TResult>> PutFileStreamAsync<TResult>(string url, SendFileStreamData sendFileStreamData, NameValueCollection urlParameters, HeadersCollection headersCollection, Action<FileTransferProgressEventArgs> onSendProgress, CancellationToken cancellationToken)
where TResult : new();
Task<ApiResult<TResult>> PostFileStreamAsync<TResult>(string url, SendFileStreamData sendFileStreamData, NameValueCollection urlParameters, HeadersCollection headersCollection, Action<FileTransferProgressEventArgs> onSendProgress, CancellationToken cancellationToken)
where TResult : new();

Task<ApiResult<FetchFileStreamData>> RetrieveFileGetAsync(string url, NameValueCollection urlParameters, HeadersCollection headersCollection, Action<FileTransferProgressEventArgs> onReceiveProgress, CancellationToken cancellationToken);


Task<ApiResult<ServerPushStreaming>> PushContiniousStreamingDataAsync<TResult>(
HttpMethod httpMethod, string path, ContiniousStreamingRequest startContiniousStreamingRequest, NameValueCollection urlParameters, HeadersCollection headersCollection,
CancellationToken cancellationToken)
where TResult : new();


}



}
Loading

0 comments on commit 46c54fe

Please sign in to comment.