@@ -33,8 +33,9 @@ public RemoteSync(string host, string username, string password,
3333 _username = username ;
3434 _password = password ;
3535 _searchPattern = searchPattern ;
36- _localRootDirectory = localRootDirectory ;
37- _remoteRootDirectory = remoteRootDirectory ;
36+ _localRootDirectory = Path . GetFullPath ( localRootDirectory )
37+ . TrimEnd ( Path . DirectorySeparatorChar , Path . AltDirectorySeparatorChar ) ;
38+ _remoteRootDirectory = remoteRootDirectory . TrimEnd ( '/' , '\\ ' ) ;
3839 _director = director ;
3940 _excludedFolders = excludedFolders ?? new List < string > ( ) ;
4041 _sftp = new SftpClient ( host , username , password ) ;
@@ -62,8 +63,9 @@ public RemoteSync(string host, string username, string password,
6263 _username = username ;
6364 _password = password ;
6465 _searchPattern = searchPattern ;
65- _localRootDirectory = localRootDirectory ;
66- _remoteRootDirectory = remoteRootDirectory ;
66+ _localRootDirectory = Path . GetFullPath ( localRootDirectory )
67+ . TrimEnd ( Path . DirectorySeparatorChar , Path . AltDirectorySeparatorChar ) ;
68+ _remoteRootDirectory = remoteRootDirectory . TrimEnd ( '/' , '\\ ' ) ;
6769 _director = director ;
6870 _excludedFolders = excludedFolders ?? new List < string > ( ) ;
6971 _sftp = new SftpClient ( host , username , password ) ;
@@ -150,7 +152,7 @@ public static async Task RunSharedInitialSyncAsync(
150152 /// <param name="destinationPath">Path to the destination file</param>
151153 public static void SyncFile ( SftpClient sftp , string sourcePath , string destinationPath )
152154 {
153- Logger . LogInfo ( $ "Syncing { sourcePath } -> { destinationPath } ") ;
155+ Logger . LogInfo ( $ "Syncing { sourcePath } ") ;
154156
155157 int retryCount = 0 ;
156158 const int maxRetries = 5 ;
@@ -214,7 +216,7 @@ private string[] FilteredDirectories(string localPath)
214216
215217 public async Task CreateDirectories ( string localPath , string remotePath )
216218 {
217- Logger . LogInfo ( $ "Creating directory { localPath } -> { remotePath } ") ;
219+ Logger . LogInfo ( $ "Creating directory { remotePath } ") ;
218220
219221 try
220222 {
@@ -383,6 +385,19 @@ public static bool IsFileReady(String sFilename)
383385 }
384386 }
385387
388+ private string GetRemotePathForLocal ( string localPath )
389+ {
390+ var relativePath = Path . GetRelativePath ( _localRootDirectory , localPath ) ;
391+ if ( relativePath == "." || string . IsNullOrEmpty ( relativePath ) )
392+ return _remoteRootDirectory ;
393+
394+ relativePath = relativePath . Replace ( '\\ ' , '/' ) . TrimStart ( '/' ) ;
395+ if ( relativePath . Length == 0 )
396+ return _remoteRootDirectory ;
397+
398+ return _remoteRootDirectory + "/" + relativePath ;
399+ }
400+
386401 private void EnsureConnected ( )
387402 {
388403 if ( _disposed )
@@ -417,8 +432,8 @@ private async void Fsw_Changed(object? sender, FileSystemEventArgs arg)
417432 || arg . ChangeType == WatcherChangeTypes . Renamed )
418433 {
419434 var changedPath = Path . GetDirectoryName ( arg . FullPath ) ;
420- var relativePath = _localRootDirectory == changedPath ? "" : changedPath ? . Substring ( _localRootDirectory . Length ) . Replace ( ' \\ ' , '/' ) ;
421- var fullRemotePath = _remoteRootDirectory + relativePath ;
435+ var fullRemotePath = GetRemotePathForLocal ( changedPath ?? _localRootDirectory ) ;
436+ var fullRemoteFilePath = GetRemotePathForLocal ( arg . FullPath ) ;
422437 await Task . Yield ( ) ;
423438 bool makeDirectory = true ;
424439 lock ( _activeDirSync )
@@ -439,7 +454,7 @@ private async void Fsw_Changed(object? sender, FileSystemEventArgs arg)
439454 if ( connectionOk )
440455 {
441456 //check if we're a new directory
442- if ( makeDirectory && Directory . Exists ( arg . FullPath ) && ! _sftp . Exists ( fullRemotePath ) )
457+ if ( makeDirectory && changedPath != null && Directory . Exists ( changedPath ) && ! _sftp . Exists ( fullRemotePath ) )
443458 {
444459 _sftp . CreateDirectory ( fullRemotePath ) ;
445460 }
@@ -501,7 +516,7 @@ private async void Fsw_Changed(object? sender, FileSystemEventArgs arg)
501516 fileConnectionOk = EnsureConnectedSafe ( ) ;
502517 if ( fileConnectionOk )
503518 {
504- SyncFile ( _sftp , arg . FullPath , fullRemotePath + "/" + Path . GetFileName ( arg . FullPath ) ) ;
519+ SyncFile ( _sftp , arg . FullPath , fullRemoteFilePath ) ;
505520 }
506521 }
507522 finally
0 commit comments