Skip to content

Commit

Permalink
Add remote computer's upload progress when downloading from console.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 8, 2020
1 parent 0bdada4 commit 15ef6d4
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 8 deletions.
9 changes: 9 additions & 0 deletions Agent/Services/AgentSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ private void RegisterMessageHandlers()
}
using var wc = new WebClient();
var lastProgressPercent = 0;
wc.UploadProgressChanged += async (sender, args) =>
{
if (args.ProgressPercentage > lastProgressPercent)
{
lastProgressPercent = args.ProgressPercentage;
await HubConnection.SendAsync("DownloadFileProgress", lastProgressPercent, senderConnectionID);
}
};
var response = await wc.UploadFileTaskAsync($"{ConnectionInfo.Host}/API/FileSharing/", filePath);
var fileIDs = JsonSerializer.Deserialize<string[]>(Encoding.UTF8.GetString(response));
await HubConnection.SendAsync("DownloadFile", fileIDs[0], senderConnectionID);
Expand Down
5 changes: 5 additions & 0 deletions Server/Hubs/AgentHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public Task DownloadFile(string fileID, string requesterID)
{
return BrowserHubContext.Clients.Client(requesterID).SendAsync("DownloadFile", fileID);
}
public Task DownloadFileProgress(int progressPercent, string requesterID)
{
return BrowserHubContext.Clients.Client(requesterID).SendAsync("DownloadFileProgress", progressPercent);
}

public override Task OnConnectedAsync()
{
return base.OnConnectedAsync();
Expand Down
6 changes: 3 additions & 3 deletions Server/wwwroot/scripts/Commands/WebCommands.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Server/wwwroot/scripts/Commands/WebCommands.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Server/wwwroot/scripts/Commands/WebCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ var commands: Array<ConsoleCommand> = [

function uploadFiles(fileList: FileList): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
AddConsoleOutput("File upload started...");
AddConsoleOutput("File upload started.");

var strPath = "/API/FileSharing/";
var fd = new FormData();
Expand All @@ -525,7 +525,7 @@ function uploadFiles(fileList: FileList): Promise<string[]> {
xhr.open('POST', strPath, true);
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
AddConsoleOutput("File upload completed.");
AddConsoleOutput("File upload completed. It might take a while for the agent to download it.");
resolve(JSON.parse(xhr.responseText));
}
else {
Expand All @@ -541,7 +541,7 @@ function uploadFiles(fileList: FileList): Promise<string[]> {
xhr.upload.onprogress = (e) => {
var currentPercent = isFinite(e.loaded / e.total) ? Math.round(e.loaded / e.total * 100) : 0;

if (currentPercent != uploadPercent) {
if (currentPercent > uploadPercent) {
var uploadPercent = currentPercent;
AddConsoleOutput("File upload progress: " + String(currentPercent) + "%");
}
Expand Down
3 changes: 3 additions & 0 deletions Server/wwwroot/scripts/HubConnection.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Server/wwwroot/scripts/HubConnection.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Server/wwwroot/scripts/HubConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ function applyMessageHandlers(hubConnection) {
hubConnection.on("DownloadFile", (fileID: string) => {
location.assign(`/API/FileSharing/${fileID}`);
});
hubConnection.on("DownloadFileProgress", (progressPercent: number) => {
AddConsoleOutput(`Remote computer upload progress: ${progressPercent}%`)
});
hubConnection.on("TransferCompleted", (transferID: string) => {
var completedWrapper = document.getElementById(transferID + "-completed");
var count = parseInt(completedWrapper.innerHTML);
Expand Down

0 comments on commit 15ef6d4

Please sign in to comment.