11using FileSyncLibNet . AccessProviders ;
22using FileSyncLibNet . Commons ;
3+ using FileSyncLibNet . FileCleanJob ;
34using FileSyncLibNet . FileSyncJob ;
45using Microsoft . Extensions . Logging ;
56using System ;
@@ -14,32 +15,77 @@ internal class AbstractProvider : ProviderBase
1415
1516 public AbstractProvider ( IFileJobOptions jobOptions ) : base ( jobOptions )
1617 {
17- if ( ! ( JobOptions is IFileSyncJobOptions syncJobOptions ) )
18- throw new ArgumentException ( "this instance has no information about syncing files, it has type " + JobOptions . GetType ( ) . ToString ( ) ) ;
19- if ( syncJobOptions . SourcePath . StartsWith ( "scp:" ) )
18+ if ( ! ( JobOptions is IFileCleanJobOptions ) && ! ( JobOptions is IFileSyncJobOptions ) )
2019 {
21- SourceAccess = new ScpAccessProvider ( syncJobOptions . Credentials , jobOptions . Logger , stateFilename : null ) ;
20+ throw new ArgumentException ( "this instance has no information about syncing files or cleaning files, it has type " + JobOptions . GetType ( ) . ToString ( ) ) ;
2221 }
23- else
22+ string stateFilename = null ;
23+ if ( JobOptions is IFileSyncJobOptions syncJobOptions )
2424 {
25- SourceAccess = new FileIoAccessProvider ( jobOptions . Logger , stateFilename : null ) ;
25+ if ( syncJobOptions . SourcePath . StartsWith ( "scp:" ) )
26+ {
27+ SourceAccess = new ScpAccessProvider ( syncJobOptions . Credentials , JobOptions . Logger , stateFilename : null ) ;
28+ }
29+ else
30+ {
31+ SourceAccess = new FileIoAccessProvider ( JobOptions . Logger , stateFilename : null ) ;
32+ }
33+ SourceAccess . UpdateAccessPath ( syncJobOptions . SourcePath ) ;
34+ stateFilename = null ;
2635 }
27- SourceAccess . UpdateAccessPath ( syncJobOptions . SourcePath ) ;
28- string stateFilename = syncJobOptions . RememberRemoteState ? syncJobOptions . GetHashedName ( ) : null ;
29- if ( syncJobOptions . DestinationPath . StartsWith ( "scp:" ) )
36+ if ( JobOptions . DestinationPath . StartsWith ( "scp:" ) )
3037 {
31- DestinationAccess = new ScpAccessProvider ( syncJobOptions . Credentials , jobOptions . Logger , stateFilename ) ;
38+ DestinationAccess = new ScpAccessProvider ( JobOptions . Credentials , JobOptions . Logger , stateFilename ) ;
3239 }
3340 else
3441 {
35- DestinationAccess = new FileIoAccessProvider ( jobOptions . Logger , stateFilename ) ;
42+ DestinationAccess = new FileIoAccessProvider ( JobOptions . Logger , stateFilename ) ;
3643 }
3744
3845 }
3946
4047 public override void DeleteFiles ( )
4148 {
42- throw new NotImplementedException ( ) ;
49+ //Use from file io provider and adapt to use access provider
50+
51+ if ( ! ( JobOptions is IFileCleanJobOptions jobOptions ) )
52+ throw new ArgumentException ( "this instance has no information about deleting files, it has type " + JobOptions . GetType ( ) . ToString ( ) ) ;
53+ try
54+ {
55+ string formattedDestinationPath = string . Format ( jobOptions . DestinationPath , DateTime . Now ) ;
56+ DestinationAccess . UpdateAccessPath ( formattedDestinationPath ) ;
57+ }
58+ catch ( Exception ex ) { logger ? . LogError ( ex , "exception formatting destination accesspath with DateTime.Now as 0 arg {A}" , jobOptions . DestinationPath ) ; }
59+
60+ try
61+ {
62+ var minimumLastWriteTime = DateTimeOffset . Now - jobOptions . MaxAge ;
63+ var sourceFiles = DestinationAccess . GetFiles ( minimumLastWriteTime : minimumLastWriteTime . DateTime ,
64+ pattern : JobOptions . SearchPattern ,
65+ recursive : JobOptions . Recursive ,
66+ subfolders : JobOptions . Subfolders ,
67+ olderFiles : true ) ;
68+ int fileCount = 0 ;
69+ foreach ( var fileToDelete in sourceFiles )
70+ {
71+ logger . LogDebug ( "deleting file {A}" , fileToDelete . Name ) ;
72+
73+ try
74+ {
75+ DestinationAccess . Delete ( fileToDelete ) ;
76+
77+ fileCount ++ ;
78+ }
79+ catch ( Exception ex ) { logger . LogError ( ex , "exception deleting file {A}" , fileToDelete . Name ) ; }
80+ logger . LogDebug ( "deleted {A} files" , fileCount ) ;
81+ }
82+
83+
84+ }
85+ catch ( Exception exc )
86+ {
87+ logger . LogError ( exc , "TimerCleanup_Tick threw an exception" ) ;
88+ }
4389 }
4490
4591 public override void SyncSourceToDest ( )
0 commit comments