11using FileSyncLibNet . Commons ;
2+ using Microsoft . Extensions . Logging ;
23using Renci . SshNet ;
34using System ;
45using System . Collections . Generic ;
@@ -15,13 +16,14 @@ internal class ScpAccessProvider : IAccessProvider
1516 NetworkCredential Credentials { get ; }
1617 SftpClient ftpClient ;
1718 public string AccessPath { get ; private set ; }
18-
19- public ScpAccessProvider ( NetworkCredential credentials )
19+ private readonly ILogger logger ;
20+ public ScpAccessProvider ( NetworkCredential credentials , ILogger logger )
2021 {
22+ this . logger = logger ;
2123 Credentials = credentials ;
2224 }
23- public void UpdateAccessPath ( string accessPath )
24- {
25+ public void UpdateAccessPath ( string accessPath )
26+ {
2527 AccessPathUri = accessPath ;
2628 var pattern = @"scp://(?:(?<user>[^@]+)@)?(?<host>[^:/]+)(?::(?<port>\d+))?(?<path>/.*)?" ;
2729 var match = Regex . Match ( AccessPathUri , pattern ) ;
@@ -62,7 +64,7 @@ void CreateClient()
6264 ftpClient = new SftpClient ( host , port , Credentials . UserName , Credentials . Password ) ;
6365 }
6466 }
65-
67+
6668 void EnsureConnected ( )
6769 {
6870 if ( ftpClient == null )
@@ -130,6 +132,7 @@ public bool MatchesPattern(string fileName, string pattern)
130132
131133 public List < FileInfo2 > GetFiles ( DateTime minimumLastWriteTime , string pattern , string path = null , bool recursive = false , List < string > subfolders = null )
132134 {
135+ //add try catch for non existent folders
133136 EnsureConnected ( ) ;
134137 string basePath = string . IsNullOrEmpty ( path ) ? AccessPath : path ;
135138 List < FileInfo2 > ret_val = new List < FileInfo2 > ( ) ;
@@ -143,17 +146,24 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
143146 }
144147 else
145148 {
146- var files = ftpClient . ListDirectory ( basePath ) ;
147- if ( recursive )
149+ try
148150 {
149- foreach ( var folder in files . Where ( x => x . IsDirectory && ! ( x . Name == "." ) && ! ( x . Name == ".." ) ) )
151+ var files = ftpClient . ListDirectory ( basePath ) ;
152+ if ( recursive )
150153 {
151- var subPath = System . IO . Path . Combine ( folder . Name ) . Replace ( "\\ " , "/" ) ;
152- ret_val . AddRange ( GetFiles ( minimumLastWriteTime , pattern , subPath , recursive ) ) ;
154+ foreach ( var folder in files . Where ( x => x . IsDirectory && ! ( x . Name == "." ) && ! ( x . Name == ".." ) ) )
155+ {
156+ var subPath = System . IO . Path . Combine ( folder . Name ) . Replace ( "\\ " , "/" ) ;
157+ ret_val . AddRange ( GetFiles ( minimumLastWriteTime , pattern , subPath , recursive ) ) ;
158+ }
153159 }
160+ ret_val . AddRange ( files . Where ( x => MatchesPattern ( x . Name , pattern ) ) . Where ( x => x . LastWriteTime >= minimumLastWriteTime && ! x . IsDirectory ) . Select ( x =>
161+ new FileInfo2 ( $ "{ basePath . Substring ( AccessPath . Length + 1 ) } /{ x . Name } ", exists : true ) { LastWriteTime = x . LastWriteTime , Length = x . Length } ) . ToList ( ) ) ;
162+ }
163+ catch ( Exception exc )
164+ {
165+ logger ? . LogError ( exc , "exception in GetFiles for path {A}, basePath {B}" , path , basePath ) ;
154166 }
155- ret_val . AddRange ( files . Where ( x => MatchesPattern ( x . Name , pattern ) ) . Where ( x => x . LastWriteTime >= minimumLastWriteTime && ! x . IsDirectory ) . Select ( x =>
156- new FileInfo2 ( $ "{ basePath . Substring ( AccessPath . Length + 1 ) } /{ x . Name } ", exists : true ) { LastWriteTime = x . LastWriteTime , Length = x . Length } ) . ToList ( ) ) ;
157167 }
158168 return ret_val ;
159169 }
0 commit comments