Skip to content

Commit

Permalink
Merge pull request #7 from easymorph/ver.1.3
Browse files Browse the repository at this point in the history
Ver.1.3
  • Loading branch information
strongtigerman authored Oct 16, 2018
2 parents 2eac160 + be99fdf commit 3ed9d6f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/Client/IMorphServerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public interface IMorphServerApiClient
Task<RunningTaskStatus> StartTaskAsync(ApiSession apiSession, Guid taskId, CancellationToken cancellationToken, IEnumerable<TaskParameterBase> taskParameters = null);
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<ValidateTasksResult> ValidateTasksAsync(ApiSession apiSession, string projectPath, CancellationToken cancellationToken);
Task<SpacesList> GetSpacesListAsync(CancellationToken cancellationToken);
Expand Down
61 changes: 51 additions & 10 deletions src/Client/MorphServerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public async Task<RunningTaskStatus> StartTaskAsync(ApiSession apiSession, Guid
using (var response = await GetHttpClient().SendAsync(BuildHttpRequestMessage(HttpMethod.Post, url, request, apiSession), cancellationToken))
{
var info = await HandleResponse<RunningTaskStatusDto>(response);
return RunningTaskStatusMapper.RunningTaskStatusFromDto(info);
return RunningTaskStatusMapper.RunningTaskStatusFromDto(info);
}

}
Expand Down Expand Up @@ -363,6 +363,13 @@ public async Task DownloadFileAsync(ApiSession apiSession, string remoteFilePath

while (true)
{
// cancel download if cancellation token triggered
if (cancellationToken.IsCancellationRequested)
{
fileProgress.ChangeState(FileProgressState.Cancelled);
throw new OperationCanceledException();
}

var length = await streamToReadFrom.ReadAsync(buffer, 0, buffer.Length);
if (length <= 0) break;
await streamToWriteTo.WriteAsync(buffer, 0, length);
Expand All @@ -373,6 +380,7 @@ public async Task DownloadFileAsync(ApiSession apiSession, string remoteFilePath
fileProgress.ChangeState(FileProgressState.Processing);
lastUpdate = DateTime.Now;
}

}

fileProgress.ChangeState(FileProgressState.Finishing);
Expand Down Expand Up @@ -420,6 +428,38 @@ public async Task UploadFileAsync(ApiSession apiSession, string localFilePath, s
}


/// <summary>
/// Uploads local file to the server folder.
/// </summary>
/// <param name="apiSession">api session</param>
/// <param name="localFilePath">path to the local file</param>
/// <param name="destFolderPath">destination folder like /path/to/folder </param>
/// <param name="destFileName">destination filename. If it's empty then original file name will be used</param>
/// <param name="cancellationToken">cancellation token</param>
/// <param name="overwriteFileifExists">overwrite file</param>
/// <returns></returns>
public async Task UploadFileAsync(ApiSession apiSession, string localFilePath, string destFolderPath, string destFileName, CancellationToken cancellationToken, bool overwriteFileifExists = false)
{
if (apiSession == null)
{
throw new ArgumentNullException(nameof(apiSession));
}

if (!File.Exists(localFilePath))
{
throw new FileNotFoundException(string.Format("File '{0}' not found", localFilePath));
}
var fileName = String.IsNullOrWhiteSpace(destFileName)? Path.GetFileName(localFilePath): destFileName;
var fileSize = new FileInfo(localFilePath).Length;
using (var fsSource = new FileStream(localFilePath, FileMode.Open, FileAccess.Read))
{
await UploadFileAsync(apiSession, fsSource, fileName, fileSize, destFolderPath, cancellationToken, overwriteFileifExists);
return;
}

}


protected HttpRequestMessage BuildHttpRequestMessage(HttpMethod httpMethod, string url, HttpContent content, ApiSession apiSession)
{
var requestMessage = new HttpRequestMessage()
Expand Down Expand Up @@ -464,20 +504,21 @@ public async Task UploadFileAsync(ApiSession apiSession, Stream inputStream, str
{
var downloadProgress = new FileProgress(fileName, fileSize);
downloadProgress.StateChanged += DownloadProgress_StateChanged;
using (var streamContent = new ProgressStreamContent(inputStream, downloadProgress))
using (cancellationToken.Register(() => downloadProgress.ChangeState(FileProgressState.Cancelled)))
{
content.Add(streamContent, "files", Path.GetFileName(fileName));

var requestMessage = BuildHttpRequestMessage(overwriteFileifExists ? HttpMethod.Put : HttpMethod.Post, url, content, apiSession);
using (requestMessage)
using (var streamContent = new ProgressStreamContent(inputStream, downloadProgress))
{
using (var response = await GetHttpClient().SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
content.Add(streamContent, "files", Path.GetFileName(fileName));

var requestMessage = BuildHttpRequestMessage(overwriteFileifExists ? HttpMethod.Put : HttpMethod.Post, url, content, apiSession);
using (requestMessage)
{
await HandleResponse(response);
using (var response = await GetHttpClient().SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
{
await HandleResponse(response);
}
}
}


}
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/Helper/FileProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ internal class FileProgress : IFileProgress
public void ChangeState(FileProgressState state)
{
State = state;
if (StateChanged != null)
StateChanged?.Invoke(this, new FileEventArgs
{
StateChanged(this, new FileEventArgs
{
ProcessedBytes = ProcessedBytes,
State = state,
Guid = _guid,
FileName = FileName,
FileSize = FileSize
ProcessedBytes = ProcessedBytes,
State = state,
Guid = _guid,
FileName = FileName,
FileSize = FileSize

});
}
});
}
public void SetProcessedBytes(long np)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Model/FileProgressState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum FileProgressState
{
Starting,
Processing,
Finishing
Finishing,
Cancelled
}
}
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
[assembly: Guid("72ecc66f-62fe-463f-afad-e1ff5cc19cd9")]


[assembly: AssemblyVersion("1.3.2.3")]
[assembly: AssemblyFileVersion("1.3.2.3")]
[assembly: AssemblyVersion("1.3.4.1")]
[assembly: AssemblyFileVersion("1.3.4.1")]

0 comments on commit 3ed9d6f

Please sign in to comment.