|
| 1 | +using Immense.RemoteControl.Desktop.Shared.Abstractions; |
| 2 | +using Immense.RemoteControl.Desktop.Shared.Services; |
| 3 | +using Immense.RemoteControl.Desktop.Shared.ViewModels; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using Immense.RemoteControl.Desktop.UI.Controls.Dialogs; |
| 7 | +using Immense.RemoteControl.Desktop.UI.Views; |
| 8 | +using Immense.RemoteControl.Desktop.UI.Services; |
| 9 | +using System.Threading; |
| 10 | +using System.IO; |
| 11 | + |
| 12 | +namespace Immense.RemoteControl.Desktop.Linux.Services; |
| 13 | + |
| 14 | +public class FileTransferServiceLinux : IFileTransferService |
| 15 | +{ |
| 16 | + private static readonly ConcurrentDictionary<string, FileTransferWindow> _fileTransferWindows = new(); |
| 17 | + private static readonly ConcurrentDictionary<string, FileStream> _partialTransfers = new(); |
| 18 | + private static readonly SemaphoreSlim _writeLock = new(1, 1); |
| 19 | + private static volatile bool _messageBoxPending; |
| 20 | + private readonly IViewModelFactory _viewModelFactory; |
| 21 | + private readonly IUiDispatcher _dispatcher; |
| 22 | + private readonly IDialogProvider _dialogProvider; |
| 23 | + private readonly ILogger<FileTransferServiceLinux> _logger; |
| 24 | + |
| 25 | + public FileTransferServiceLinux( |
| 26 | + IViewModelFactory viewModelFactory, |
| 27 | + IUiDispatcher dispatcher, |
| 28 | + IDialogProvider dialogProvider, |
| 29 | + ILogger<FileTransferServiceLinux> logger) |
| 30 | + { |
| 31 | + _viewModelFactory = viewModelFactory; |
| 32 | + _dispatcher = dispatcher; |
| 33 | + _dialogProvider = dialogProvider; |
| 34 | + _logger = logger; |
| 35 | + } |
| 36 | + |
| 37 | + public string GetBaseDirectory() |
| 38 | + { |
| 39 | + var desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); |
| 40 | + if (Directory.Exists(desktopDir)) |
| 41 | + { |
| 42 | + return desktopDir; |
| 43 | + } |
| 44 | + |
| 45 | + return Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "RemoteControl")).FullName; |
| 46 | + } |
| 47 | + |
| 48 | + public void OpenFileTransferWindow(IViewer viewer) |
| 49 | + { |
| 50 | + _dispatcher.Post(() => |
| 51 | + { |
| 52 | + if (_fileTransferWindows.TryGetValue(viewer.ViewerConnectionId, out var window)) |
| 53 | + { |
| 54 | + window.Activate(); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + window = new FileTransferWindow |
| 59 | + { |
| 60 | + DataContext = _viewModelFactory.CreateFileTransferWindowViewModel(viewer) |
| 61 | + }; |
| 62 | + window.Closed += (sender, arg) => |
| 63 | + { |
| 64 | + _fileTransferWindows.Remove(viewer.ViewerConnectionId, out _); |
| 65 | + }; |
| 66 | + _fileTransferWindows.AddOrUpdate(viewer.ViewerConnectionId, window, (k, v) => window); |
| 67 | + window.Show(); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + public async Task ReceiveFile(byte[] buffer, string fileName, string messageId, bool endOfFile, bool startOfFile) |
| 73 | + { |
| 74 | + try |
| 75 | + { |
| 76 | + await _writeLock.WaitAsync(); |
| 77 | + |
| 78 | + var baseDir = GetBaseDirectory(); |
| 79 | + |
| 80 | + if (startOfFile) |
| 81 | + { |
| 82 | + var filePath = Path.Combine(baseDir, fileName); |
| 83 | + |
| 84 | + if (File.Exists(filePath)) |
| 85 | + { |
| 86 | + var count = 0; |
| 87 | + var ext = Path.GetExtension(fileName); |
| 88 | + var fileWithoutExt = Path.GetFileNameWithoutExtension(fileName); |
| 89 | + while (File.Exists(filePath)) |
| 90 | + { |
| 91 | + filePath = Path.Combine(baseDir, $"{fileWithoutExt}-{count}{ext}"); |
| 92 | + count++; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + File.Create(filePath).Close(); |
| 97 | + |
| 98 | + var fs = new FileStream(filePath, FileMode.OpenOrCreate); |
| 99 | + _partialTransfers.AddOrUpdate(messageId, fs, (k, v) => fs); |
| 100 | + } |
| 101 | + |
| 102 | + var fileStream = _partialTransfers[messageId]; |
| 103 | + |
| 104 | + if (buffer?.Length > 0) |
| 105 | + { |
| 106 | + await fileStream.WriteAsync(buffer); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + if (endOfFile) |
| 111 | + { |
| 112 | + fileStream.Close(); |
| 113 | + _partialTransfers.Remove(messageId, out _); |
| 114 | + } |
| 115 | + } |
| 116 | + catch (Exception ex) |
| 117 | + { |
| 118 | + _logger.LogError(ex, "Error while receiving file."); |
| 119 | + } |
| 120 | + finally |
| 121 | + { |
| 122 | + _writeLock.Release(); |
| 123 | + if (endOfFile) |
| 124 | + { |
| 125 | + await Task.Run(ShowTransferComplete); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public async Task UploadFile( |
| 131 | + FileUpload fileUpload, |
| 132 | + IViewer viewer, |
| 133 | + Action<double> progressUpdateCallback, |
| 134 | + CancellationToken cancelToken) |
| 135 | + { |
| 136 | + try |
| 137 | + { |
| 138 | + await viewer.SendFile(fileUpload, progressUpdateCallback, cancelToken); |
| 139 | + } |
| 140 | + catch (Exception ex) |
| 141 | + { |
| 142 | + _logger.LogError(ex, "Error while uploading file."); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private async Task ShowTransferComplete() |
| 147 | + { |
| 148 | + // Prevent multiple dialogs from popping up. |
| 149 | + if (!_messageBoxPending) |
| 150 | + { |
| 151 | + _messageBoxPending = true; |
| 152 | + |
| 153 | + await _dialogProvider.Show($"File tranfer complete. Files saved to directory:\n\n{GetBaseDirectory()}", |
| 154 | + "Tranfer Complete", |
| 155 | + MessageBoxType.OK); |
| 156 | + |
| 157 | + _messageBoxPending = false; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments