FileFlow/Global_Indexer/ClassFolderWatcher.vb

223 lines
9.6 KiB
VB.net

Imports System.IO
Imports Independentsoft
Imports System.Threading
Public Class ClassFolderWatcher
Public Shared FolderWatcher As FileSystemWatcher
Public Shared FolderWatcher_SCAN As FileSystemWatcher
Public Shared Function Restart_FolderWatch()
Try
If FolderWatcher.EnableRaisingEvents = True Then
'Gestartet also Stoppen
FolderWatcher.EnableRaisingEvents = False
FW_started = False
'FolderWatch neu instanzieren
FolderWatcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
LOGGER.Info(" >> FolderWatch neu instanziert")
FolderWatcher.IncludeSubdirectories = False
FolderWatcher.EnableRaisingEvents = True
AddHandler FolderWatcher.Created, AddressOf OnCreated
FW_started = True
'SaveConfigValue("FW_started", "True")
CONFIG.Config.FolderWatchStarted = True
CONFIG.Save()
End If
Catch ex As Exception
LOGGER.Info($"Error in Restart_FolderWatch: {ex.Message}")
LOGGER.Error(ex)
End Try
End Function
Public Shared Function Restart_FolderWatchSCAN()
Try
If FolderWatcher_SCAN.EnableRaisingEvents = True Then
'Gestartet also Stoppen
FolderWatcher_SCAN.EnableRaisingEvents = False
'FolderWatch neu instanzieren
FolderWatcher_SCAN = New System.IO.FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
LOGGER.Info(" >> FolderWatchScan neu instanziert")
FolderWatcher_SCAN.IncludeSubdirectories = False
FolderWatcher_SCAN.EnableRaisingEvents = True
AddHandler FolderWatcher_SCAN.Created, AddressOf OnCreated
'SaveConfigValue("FWSCAN_started", "True")
CONFIG.Config.FolderWatchScanStarted = True
CONFIG.Save()
End If
Catch ex As Exception
LOGGER.Info($"Error in Restart_FolderWatchSCAN: {ex.Message}")
LOGGER.Error(ex)
End Try
End Function
Public Shared Function StartStop_FolderWatch()
Try
If CURRENT_FOLDERWATCH = "" Then
'MsgBox("Bitte definieren Sie einen Überwachungsordner:", MsgBoxStyle.Exclamation)
Return False
End If
If FolderWatcher Is Nothing Then
FolderWatcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
LOGGER.Info(" >> FolderWatch Gestartet")
FolderWatcher.IncludeSubdirectories = False
FolderWatcher.EnableRaisingEvents = True
AddHandler FolderWatcher.Created, AddressOf OnCreated
FW_started = True
'SaveConfigValue("FW_started", "True")
CONFIG.Config.FolderWatchStarted = True
CONFIG.Save()
Return 1
End If
If FolderWatcher.EnableRaisingEvents = False Then
' Dim watcher As New FileSystemWatcher()
' watcher.Path = CURRENT_FOLDERWATCH
FolderWatcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
LOGGER.Info(" >> FolderWatch Gestartet")
FolderWatcher.IncludeSubdirectories = False
FolderWatcher.EnableRaisingEvents = True
AddHandler FolderWatcher.Created, AddressOf OnCreated
FW_started = True
'SaveConfigValue("FW_started", "True")
CONFIG.Config.FolderWatchStarted = True
CONFIG.Save()
Return 1
Else
'Gestartet also Stoppen
FolderWatcher.EnableRaisingEvents = False
FW_started = False
LOGGER.Info(" >> FolderWatch gestoppt")
'SaveConfigValue("FW_started", "False")
CONFIG.Config.FolderWatchStarted = False
CONFIG.Save()
Return 0
End If
'If watcher.EnableRaisingEvents = False Then
' watcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
' LOGGER.Info(" - vFolderWatch.Gestartet")
' watcher.IncludeSubdirectories = False
' watcher.EnableRaisingEvents = True
' AddHandler watcher.Created, AddressOf OnCreated
' Return 1
'Else
' 'Gestartet also Stoppen
' watcher.EnableRaisingEvents = False
' Return 0
'End If
Catch ex As Exception
MsgBox("Error in StartStop_FolderWatch:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return 99
End Try
End Function
Public Shared Function StartStop_FolderWatchSCAN()
Try
If CURRENT_SCAN_FOLDERWATCH = "" Then
If FolderWatcher.EnableRaisingEvents = True Then
Stop_FWSCAN()
Return 0
Else
If USER_LANGUAGE = "de-DE" Then
MsgBox("Bitte definieren Sie einen Überwachungsordner für Scan-Eingänge:", MsgBoxStyle.Exclamation)
Else
MsgBox("Please define a watchfolder for Scanning:", MsgBoxStyle.Exclamation)
End If
Return False
End If
End If
If FolderWatcher_SCAN Is Nothing Then
FolderWatcher_SCAN = New FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
LOGGER.Info(" >> FolderWatch Scan Gestartet")
FolderWatcher_SCAN.IncludeSubdirectories = False
FolderWatcher_SCAN.EnableRaisingEvents = True
AddHandler FolderWatcher_SCAN.Created, AddressOf OnCreated
CONFIG.Config.FolderWatchScanStarted = True
CONFIG.Save()
Return 1
End If
If FolderWatcher_SCAN.EnableRaisingEvents = False Then
' Dim watcher As New FileSystemWatcher()
' watcher.Path = CURRENT_FOLDERWATCH
FolderWatcher_SCAN = New FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
LOGGER.Info(" >> FolderWatch Scan Gestartet")
FolderWatcher_SCAN.IncludeSubdirectories = False
FolderWatcher_SCAN.EnableRaisingEvents = True
AddHandler FolderWatcher_SCAN.Created, AddressOf OnCreated
CONFIG.Config.FolderWatchScanStarted = True
CONFIG.Save()
Return 1
Else
'Gestartet also Stoppen
FolderWatcher_SCAN.EnableRaisingEvents = False
LOGGER.Info(" >> FolderWatch Scan gestoppt")
'SaveConfigValue("FWSCAN_started", "False")
CONFIG.Config.FolderWatchScanStarted = False
CONFIG.Save()
Return 0
End If
Catch ex As Exception
MsgBox("Error in StartStop_FolderWatchSCAN:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return 99
End Try
End Function
Public Shared Function Stop_FWSCAN()
If FolderWatcher.EnableRaisingEvents = True Then
'Gestartet also Stoppen
FolderWatcher.EnableRaisingEvents = False
FW_started = False
LOGGER.Info(" >> FolderWatch gestoppt")
'SaveConfigValue("FW_started", "False")
CONFIG.Config.FolderWatchStarted = True
CONFIG.Save()
Return True
Else
Return False
End If
End Function
Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
If ClassDatabase.DatabaseConnectionTimeout = True Then
LOGGER.Info(">> File handling aborted because of database timeout error!")
Exit Sub
End If
Try
For Each row As DataRow In DTEXCLUDE_FILES.Rows
Dim content As String = row.Item(0).ToString.ToLower
If e.FullPath.ToLower.Contains(content) Then
Exit Sub
End If
Next
Dim oHandleType As String
If e.FullPath.ToLower.EndsWith(".msg") Then
oHandleType = "|FW_OUTLOOK_MESSAGE|"
Else
oHandleType = "|FW_SIMPLEINDEXER|"
End If
'Die Datei übergeben
LOGGER.Info(">> OnCreated-File:" & e.FullPath)
Dim oFileExists As Date = ClassIndexFunctions.FileExistsinDropTable(e.FullPath)
If IsNothing(oFileExists) Then
ClassFilehandle.Decide_FileHandle(e.FullPath, oHandleType)
Else
Dim oResult As DialogResult
Dim oDate As String = oFileExists.ToString("d")
If USER_LANGUAGE = "de-DE" Then
oResult = MsgBox($"Die Datei wurde bereits am [{oDate}] verarbeitet. Wollen Sie die gleiche Datei noch einmal verarbeiten?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, "GLOBIX - FolderWatch")
Else
oResult = MsgBox($"This file has already been processed at [{oDate}]. Do you want to process the same file again?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, "GLOBIX - FolderWatch")
End If
If oResult = DialogResult.Yes Then
ClassFilehandle.Decide_FileHandle(e.FullPath, oHandleType)
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in folder_watch_Created")
End Try
End Sub
End Class