@ -65,9 +65,19 @@ namespace ZeroLevel.Services.FileSystem
private void CheckSourceFolder ( )
private void CheckSourceFolder ( )
{
{
string [ ] files = null ;
try
try
{
{
var files = GetFilesFromSource ( ) ;
files = GetFilesFromSource ( ) ;
}
catch ( Exception ex )
{
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Failed to process input directory '{_sourceFolder}'" ) ;
}
if ( files = = null | | files . Length = = 0 )
{
return ;
}
OnStartMovingFilesToTemporary ? . Invoke ( files . Length ) ;
OnStartMovingFilesToTemporary ? . Invoke ( files . Length ) ;
foreach ( var file in files )
foreach ( var file in files )
{
{
@ -82,6 +92,11 @@ namespace ZeroLevel.Services.FileSystem
try
try
{
{
tempFile = MoveToTemporary ( file ) ;
tempFile = MoveToTemporary ( file ) ;
if ( string . IsNullOrWhiteSpace ( tempFile ) )
{
Log . SystemWarning ( $"[PeriodicFileSystemWatcher.CheckSourceFolder] Failed to move file '{file}' to temporary directory '{_temporaryFolder}'. Without system error!" ) ;
continue ;
}
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
@ -111,16 +126,8 @@ namespace ZeroLevel.Services.FileSystem
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Fault proceed file {file}" ) ;
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Fault proceed file {file}" ) ;
}
}
}
}
}
catch ( Exception ex )
{
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Failed to process input directory '{_sourceFolder}'" ) ;
}
finally
{
OnCompleteMovingFilesToTemporary ? . Invoke ( ) ;
OnCompleteMovingFilesToTemporary ? . Invoke ( ) ;
}
}
}
/// <summary>
/// <summary>
/// Moving a file to a temporary directory
/// Moving a file to a temporary directory
@ -137,9 +144,13 @@ namespace ZeroLevel.Services.FileSystem
tempFile = TrySolveCollision ( tempFile ) ;
tempFile = TrySolveCollision ( tempFile ) ;
}
}
File . Copy ( from , tempFile , false ) ;
File . Copy ( from , tempFile , false ) ;
if ( File . Exists ( tempFile ) )
{
File . Delete ( from ) ;
File . Delete ( from ) ;
return tempFile ;
return tempFile ;
}
}
return null ;
}
/// <summary>
/// <summary>
/// Resolving collisions in filenames in the temporary directory
/// Resolving collisions in filenames in the temporary directory
@ -177,19 +188,22 @@ namespace ZeroLevel.Services.FileSystem
/// </summary>
/// </summary>
private string [ ] GetFilesFromSource ( )
private string [ ] GetFilesFromSource ( )
{
{
string [ ] files ;
if ( _extensions . Count > 0 )
if ( _extensions . Count > 0 )
{
{
string [ ] files = Directory . GetFiles ( _sourceFolder , "*.*" , _useSubdirectories ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly )
files = Directory . GetFiles ( _sourceFolder , "*.*" , _useSubdirectories ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly )
. Where ( f = > _extensions . Contains ( Path . GetExtension ( f ) . ToLowerInvariant ( ) ) ) . ToArray ( ) ;
? . Where ( f = > _extensions . Contains ( Path . GetExtension ( f ) . ToLowerInvariant ( ) ) )
Array . Sort < string > ( files , FileNameSortCompare ) ;
? . ToArray ( ) ;
return files ;
}
}
else
else
{
{
string [ ] files = Directory . GetFiles ( _sourceFolder , "*.*" , _useSubdirectories ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ) ;
files = Directory . GetFiles ( _sourceFolder , "*.*" , _useSubdirectories ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ) ;
}
if ( files ! = null )
{
Array . Sort < string > ( files , FileNameSortCompare ) ;
Array . Sort < string > ( files , FileNameSortCompare ) ;
return files ;
}
}
return files ;
}
}
/// <summary>
/// <summary>