-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from easymorph/ver.1.4
Ver.1.4
- Loading branch information
Showing
55 changed files
with
2,865 additions
and
872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.