@ -65,61 +65,68 @@ namespace ZeroLevel.Services.FileSystem
private void CheckSourceFolder ( )
private void CheckSourceFolder ( )
{
{
string [ ] files = null ;
try
try
{
{
var files = GetFilesFromSource ( ) ;
files = GetFilesFromSource ( ) ;
OnStartMovingFilesToTemporary ? . Invoke ( files . Length ) ;
}
foreach ( var file in files )
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 ) ;
foreach ( var file in files )
{
try
{
{
Log . Debug ( $"[PeriodicFileSystemWatcher.CheckSourceFolder] Find new file {file}" ) ;
if ( FSUtils . IsFileLocked ( new FileInfo ( file ) ) )
{
continue ;
}
string tempFile ;
try
try
{
{
Log . Debug ( $"[PeriodicFileSystemWatcher.CheckSourceFolder] Find new file {file}" ) ;
tempFile = MoveToTemporary ( file ) ;
if ( FSUtils . IsFileLocked ( new FileInfo ( file ) ) )
if ( string . IsNullOrWhiteSpace ( tempFile ) )
{
continue ;
}
string tempFile ;
try
{
tempFile = MoveToTemporary ( file ) ;
}
catch ( Exception ex )
{
{
Log . System Error( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Failed to attempt to move file '{file}' to temporary directory '{_temporaryFolder}'") ;
Log . SystemWarning ( $"[PeriodicFileSystemWatcher.CheckSourceFolder] Failed to move file '{file}' to temporary directory '{_temporaryFolder}'. Without system error!" ) ;
continue ;
continue ;
}
}
finally
{
OnMovingFileToTemporary ? . Invoke ( ) ;
}
Log . Debug ( $"[PeriodicFileSystemWatcher.CheckSourceFolder] Handle file {file}" ) ;
try
{
_callback ( new FileMeta ( Path . GetFileName ( file ) , tempFile ) ) ;
}
catch ( Exception ex )
{
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Fault callback for file '{tempFile}'" ) ;
}
if ( _autoRemoveTempFileAfterCallback )
{
File . Delete ( tempFile ) ;
}
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Fault proceed file {file}" ) ;
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Failed to attempt to move file '{file}' to temporary directory '{_temporaryFolder}'" ) ;
continue ;
}
finally
{
OnMovingFileToTemporary ? . Invoke ( ) ;
}
Log . Debug ( $"[PeriodicFileSystemWatcher.CheckSourceFolder] Handle file {file}" ) ;
try
{
_callback ( new FileMeta ( Path . GetFileName ( file ) , tempFile ) ) ;
}
catch ( Exception ex )
{
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Fault callback for file '{tempFile}'" ) ;
}
if ( _autoRemoveTempFileAfterCallback )
{
File . Delete ( tempFile ) ;
}
}
}
}
catch ( Exception ex )
{
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Fault proceed file {file}" ) ;
}
}
}
catch ( Exception ex )
OnCompleteMovingFilesToTemporary ? . Invoke ( ) ;
{
Log . SystemError ( ex , $"[PeriodicFileSystemWatcher.CheckSourceFolder] Failed to process input directory '{_sourceFolder}'" ) ;
}
finally
{
OnCompleteMovingFilesToTemporary ? . Invoke ( ) ;
}
}
}
/// <summary>
/// <summary>
@ -137,8 +144,12 @@ namespace ZeroLevel.Services.FileSystem
tempFile = TrySolveCollision ( tempFile ) ;
tempFile = TrySolveCollision ( tempFile ) ;
}
}
File . Copy ( from , tempFile , false ) ;
File . Copy ( from , tempFile , false ) ;
File . Delete ( from ) ;
if ( File . Exists ( tempFile ) )
return tempFile ;
{
File . Delete ( from ) ;
return tempFile ;
}
return null ;
}
}
/// <summary>
/// <summary>
@ -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>