Skip to content

Commit

Permalink
Fix message box after file transfer.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Apr 22, 2020
1 parent b01ac18 commit 2da785e
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions ScreenCast.Core/Services/FileTransferService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using Remotely.Shared.Win32;

namespace Remotely.ScreenCast.Core.Services
{
Expand All @@ -23,9 +24,10 @@ public interface IFileTransferService

public class FileTransferService : IFileTransferService
{
private static readonly ConcurrentDictionary<string, FileStream> _partialTransfers = new ConcurrentDictionary<string, FileStream>();
private static readonly ConcurrentDictionary<string, FileStream> partialTransfers = new ConcurrentDictionary<string, FileStream>();

private static readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1);
private static readonly SemaphoreSlim writeLock = new SemaphoreSlim(1);
private static volatile bool messageBoxPending;

public string GetBaseDirectory()
{
Expand All @@ -48,7 +50,7 @@ public async Task ReceiveFile(byte[] buffer, string fileName, string messageId,
{
try
{
await _writeLock.WaitAsync();
await writeLock.WaitAsync();

var baseDir = GetBaseDirectory();

Expand All @@ -71,10 +73,10 @@ public async Task ReceiveFile(byte[] buffer, string fileName, string messageId,
File.Create(filePath).Close();
SetFileOrFolderPermissions(filePath);
var fs = new FileStream(filePath, FileMode.OpenOrCreate);
_partialTransfers.AddOrUpdate(messageId, fs, (k,v) => fs);
partialTransfers.AddOrUpdate(messageId, fs, (k,v) => fs);
}

var fileStream = _partialTransfers[messageId];
var fileStream = partialTransfers[messageId];

if (buffer?.Length > 0)
{
Expand All @@ -85,7 +87,7 @@ public async Task ReceiveFile(byte[] buffer, string fileName, string messageId,
if (endOfFile)
{
fileStream.Close();
_partialTransfers.Remove(messageId, out _);
partialTransfers.Remove(messageId, out _);
if (EnvironmentHelper.IsWindows)
{
Process.Start("explorer.exe", baseDir);
Expand All @@ -98,7 +100,12 @@ public async Task ReceiveFile(byte[] buffer, string fileName, string messageId,
}
finally
{
_writeLock.Release();
writeLock.Release();

if (endOfFile)
{
await Task.Run(ShowTransferComplete);
}
}
}

Expand Down Expand Up @@ -149,5 +156,29 @@ private void SetFileOrFolderPermissions(string path)
}
}
}

private void ShowTransferComplete()
{
// Prevent multiple dialogs from popping up.
if (!messageBoxPending)
{
if (EnvironmentHelper.IsWindows)
{
messageBoxPending = true;
var result = Win32Interop.ShowMessageBox(IntPtr.Zero,
"File transfer complete. Show folder?",
"Transfer Complete",
User32.MessageBoxType.MB_YESNO |
User32.MessageBoxType.MB_ICONINFORMATION |
User32.MessageBoxType.MB_TOPMOST);

if (result == User32.MessageBoxResult.IDYES)
{
Process.Start("explorer.exe", GetBaseDirectory());
}
messageBoxPending = false;
}
}
}
}
}

0 comments on commit 2da785e

Please sign in to comment.