@@ -15,10 +15,14 @@ internal class ScpAccessProvider : IAccessProvider
1515 public string AccessPathUri { get ; private set ; }
1616 NetworkCredential Credentials { get ; }
1717 SftpClient ftpClient ;
18+ private readonly RemoteState remoteState ;
1819 public string AccessPath { get ; private set ; }
20+
1921 private readonly ILogger logger ;
20- public ScpAccessProvider ( NetworkCredential credentials , ILogger logger )
22+ public ScpAccessProvider ( NetworkCredential credentials , ILogger logger , string stateFilename )
2123 {
24+ if ( ! string . IsNullOrEmpty ( stateFilename ) )
25+ remoteState = new RemoteState ( stateFilename ) ;
2226 this . logger = logger ;
2327 Credentials = credentials ;
2428 }
@@ -101,8 +105,11 @@ public void CreateDirectory(string path)
101105
102106 public FileInfo2 GetFileInfo ( string path )
103107 {
104- EnsureConnected ( ) ;
105108 var realFilename = System . IO . Path . Combine ( AccessPath , path ) . Replace ( "\\ " , "/" ) ;
109+ if ( remoteState != null )
110+ return remoteState . GetFileInfo ( realFilename ) ;
111+
112+ EnsureConnected ( ) ;
106113 if ( ftpClient . Exists ( realFilename ) )
107114 {
108115 var fi = ftpClient . ListDirectory ( realFilename ) ;
@@ -149,6 +156,7 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
149156 try
150157 {
151158 var files = ftpClient . ListDirectory ( basePath ) ;
159+ var sepChar = "/" ;
152160 if ( recursive )
153161 {
154162 foreach ( var folder in files . Where ( x => x . IsDirectory && ! ( x . Name == "." ) && ! ( x . Name == ".." ) ) )
@@ -158,7 +166,7 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
158166 }
159167 }
160168 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 ( ) ) ;
169+ new FileInfo2 ( $ "{ ( AccessPath . Length + 1 < basePath . Length ? ( basePath . Substring ( AccessPath . Length + 1 ) ) + sepChar : string . Empty ) } { x . Name } ", exists : true ) { LastWriteTime = x . LastWriteTime , Length = x . Length } ) . ToList ( ) ) ;
162170 }
163171 catch ( Exception exc )
164172 {
@@ -188,13 +196,15 @@ public void WriteFile(FileInfo2 file, Stream content)
188196
189197 content . CopyTo ( stream ) ;
190198 }
199+ remoteState ? . SetFileInfo ( filePath , file ) ;
191200 }
192201
193202 public void Delete ( FileInfo2 fileInfo )
194203 {
195204 EnsureConnected ( ) ;
196205 var filePath = System . IO . Path . Combine ( AccessPath , fileInfo . Name ) . Replace ( "\\ " , "/" ) ;
197206 ftpClient . Delete ( filePath ) ;
207+ remoteState ? . RemoveFileInfo ( filePath ) ;
198208 }
199209 }
200210}
0 commit comments