Zooflow: Massive Clean up
This commit is contained in:
@@ -1,34 +1,47 @@
|
||||
Option Explicit On
|
||||
|
||||
Imports System.IO
|
||||
Imports System.Text.RegularExpressions
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports Independentsoft
|
||||
Imports DigitalData.Modules.Base
|
||||
Imports DigitalData.Modules.Language
|
||||
Imports DigitalData.Modules.Filesystem
|
||||
Imports DigitalData.Modules.Messaging
|
||||
Imports Limilabs.Mail
|
||||
|
||||
Public Class ClassFilehandle
|
||||
Inherits Base.BaseClass
|
||||
Inherits BaseClass
|
||||
|
||||
Private FileEx As File
|
||||
Private Email As Email2
|
||||
Private UserFiles As ClassUserFiles
|
||||
|
||||
Private TempFiles As New List(Of String)
|
||||
|
||||
Public Sub New(pLogConfig As LogConfig)
|
||||
MyBase.New(pLogConfig)
|
||||
Email = New Email2(pLogConfig)
|
||||
FileEx = New File(pLogConfig)
|
||||
UserFiles = New ClassUserFiles(pLogConfig)
|
||||
End Sub
|
||||
|
||||
Public Sub ClearTempFiles()
|
||||
For Each oFile In TempFiles
|
||||
Try
|
||||
IO.File.Delete(oFile)
|
||||
Logger.Debug("Temp file [{0}] was deleted.", oFile)
|
||||
Catch ex As Exception
|
||||
Logger.Warn("Temp file [{0}] could not be deleted", oFile)
|
||||
Logger.Error(ex)
|
||||
End Try
|
||||
Next
|
||||
Email.Clear_TempFiles()
|
||||
TempFiles.Clear()
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Diese Funktion entfernt alle Zeichen aus dem übergebenen String
|
||||
''' die in Dateinamen nicht erlaubt sind.
|
||||
''' </summary>
|
||||
''' <param name="Input">Der zu prüfende String</param>
|
||||
''' <returns>String ohne nichterlaubte Zeichen</returns>
|
||||
Public Function InvalidCharacters(Input As String) As String
|
||||
Dim replacement = ""
|
||||
'Return System.Text.RegularExpressions.Regex.Replace(Input, "[\\/:*?""<>|\r\n]", "", System.Text.RegularExpressions.RegexOptions.Singleline)
|
||||
Dim regexSearch = New String(Path.GetInvalidFileNameChars()) & New String(Path.GetInvalidPathChars())
|
||||
Dim r = New Regex(String.Format("[{0}]", Regex.Escape(regexSearch)))
|
||||
Return r.Replace(Input, replacement)
|
||||
End Function
|
||||
Public Function CheckDuplicateFiles(Filepath As String, ModuleTitle As String)
|
||||
Dim oFileInfo As New FileInfo(Filepath)
|
||||
Dim oFileInfo As New IO.FileInfo(Filepath)
|
||||
Dim oFilename As String = oFileInfo.Name
|
||||
Dim oFileExists As Date = ClassHelpers.FileExistsinDropTable(Filepath)
|
||||
Dim oFileExists As Date = UserFiles.FileExistsinDropTable(Filepath)
|
||||
|
||||
If oFileExists.Equals(Date.MinValue) Then
|
||||
Return True
|
||||
@@ -52,31 +65,62 @@ Public Class ClassFilehandle
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Public Function Decide_FileHandle(filename As String, handletype As String) As Boolean
|
||||
Public Function Decide_FileHandle(pFilepath As String, pHandletype As String) As Boolean
|
||||
Try
|
||||
If filename.EndsWith(".msg") Then
|
||||
Dim oTempFilePath = pFilepath
|
||||
|
||||
Dim oInboxRegex As New Regex("\.INBOX\d+$")
|
||||
|
||||
If oInboxRegex.IsMatch(oTempFilePath) Then
|
||||
Logger.Info("Renaming INBOX file to EML")
|
||||
|
||||
Try
|
||||
Dim oInfo As New IO.FileInfo(oTempFilePath)
|
||||
Logger.Info("Old Name: {0}", oInfo.Name)
|
||||
Dim oNewName = $"{oInfo.Name}.eml"
|
||||
Logger.Info("New Name: {0}", oNewName)
|
||||
Dim oTempDirectory = IO.Path.GetTempPath()
|
||||
Dim oNewPath = IO.Path.Combine(oTempDirectory, oNewName)
|
||||
|
||||
IO.File.Copy(oInfo.FullName, oNewPath)
|
||||
|
||||
TempFiles.Add(oNewPath)
|
||||
|
||||
oTempFilePath = oNewPath
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
End Try
|
||||
End If
|
||||
|
||||
If oTempFilePath.ToUpper.EndsWith(".MSG") Or oTempFilePath.ToUpper.EndsWith(".EML") Then
|
||||
My.Application.Globix.CurrMessageID = ""
|
||||
Dim _msg As New Msg.Message(filename)
|
||||
If _msg.Attachments.Count > 0 Then
|
||||
Dim result As DialogResult
|
||||
Dim oMail As IMail = Email.Load_Email(oTempFilePath)
|
||||
If oMail.Attachments.Count > 0 Then
|
||||
Dim oTitle As String
|
||||
Dim oMessage As String
|
||||
|
||||
If My.Application.User.Language = "de-DE" Then
|
||||
result = MessageBox.Show(New Form With {.TopMost = True}, "Achtung: Die Email enthält Anhänge!" & vbNewLine & "Wollen Sie die Anhänge separat indexieren und herauslösen?", "Nachfrage zur Indexierung:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
|
||||
oTitle = "Nachfrage zur Indexierung:"
|
||||
oMessage = "Achtung: Die Email enthält Anhänge!" & vbNewLine & "Wollen Sie die Anhänge separat indexieren und herauslösen?"
|
||||
Else
|
||||
result = MessageBox.Show(New Form With {.TopMost = True}, "Attention: This Email contains Attachments!" & vbNewLine & "Do you want to extract the attachments and index them seperately?", "Question about Indexing:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
|
||||
oTitle = "Question about Indexing:"
|
||||
oMessage = "Attention: This Email contains Attachments!" & vbNewLine & "Do you want to extract the attachments and index them seperately?"
|
||||
End If
|
||||
Dim oResult As DialogResult
|
||||
|
||||
If result = MsgBoxResult.Yes Then
|
||||
If handletype.StartsWith("|FW") Then
|
||||
Return Email_Decay(filename, True)
|
||||
Else
|
||||
Return Email_Decay(filename)
|
||||
End If
|
||||
' Weird hack to force messagebox to be topmost
|
||||
' https://stackoverflow.com/questions/1220882/keep-messagebox-show-on-top-of-other-application-using-c-sharp
|
||||
oResult = MessageBox.Show(oMessage, oTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
|
||||
|
||||
|
||||
If oResult = MsgBoxResult.Yes Then
|
||||
Dim oIsFolderWatch = pHandletype.StartsWith("|FW")
|
||||
Return Save_EmailAndAttachmentsToDisk(oTempFilePath, oIsFolderWatch)
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
|
||||
If filename.ToUpper.EndsWith(".LNK") Then
|
||||
If oTempFilePath.ToUpper.EndsWith(".LNK") Then
|
||||
If My.Application.User.Language = "de-DE" Then
|
||||
MsgBox("Verknüpfungen können nicht abgelegt werden!", MsgBoxStyle.Critical, "Global Indexer")
|
||||
Else
|
||||
@@ -85,164 +129,63 @@ Public Class ClassFilehandle
|
||||
Return False
|
||||
End If
|
||||
|
||||
Return Insert_GI_File(filename, handletype)
|
||||
Return UserFiles.Insert_GI_File(oTempFilePath, pHandletype)
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
MsgBox("Unexpected Error in Decide_FileHandle: " & ex.Message, MsgBoxStyle.Critical)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
Private Function Email_Decay(msgname As String, Optional FW As Boolean = False) As Boolean
|
||||
|
||||
Private Function Save_EmailAndAttachmentsToDisk(pEmailFilePath As String, Optional pFolderWatch As Boolean = False) As Boolean
|
||||
Try
|
||||
Dim msgonly As String = "|MSGONLY|"
|
||||
Dim ATT_EXTR As String = "|ATTMNTEXTRACTED|"
|
||||
If FW = True Then
|
||||
msgonly = "|FW_MSGONLY|"
|
||||
ATT_EXTR = "|FW_ATTMNTEXTRACTED|"
|
||||
Dim oMessageOnlyMarker As String = "|MSGONLY|"
|
||||
Dim oExtractedAttachmentMarker As String = "|ATTMNTEXTRACTED|"
|
||||
If pFolderWatch = True Then
|
||||
oMessageOnlyMarker = "|FW_MSGONLY|"
|
||||
oExtractedAttachmentMarker = "|FW_ATTMNTEXTRACTED|"
|
||||
End If
|
||||
Dim erfolgreich As Boolean = False
|
||||
Dim msg As New Msg.Message(msgname)
|
||||
Dim oSuccess As Boolean = False
|
||||
|
||||
If Not msg.InternetMessageId Is Nothing Then
|
||||
My.Application.Globix.CurrMessageID = msg.InternetMessageId
|
||||
Logger.Info("Converting file to Eml if needed: [{0}]", pEmailFilePath)
|
||||
Dim oEmail As IMail = Email.Load_Email(pEmailFilePath)
|
||||
|
||||
If oEmail.MessageID IsNot Nothing Then
|
||||
My.Application.Globix.CurrMessageID = oEmail.MessageID
|
||||
Else
|
||||
Logger.Info(">> Email_Decay: Es konnte keine Message-ID gelesen werden. Eine GUID wird erzeugt!")
|
||||
Dim sGUID As String
|
||||
sGUID = System.Guid.NewGuid.ToString()
|
||||
My.Application.Globix.CurrMessageID = sGUID
|
||||
Logger.Info("Es konnte keine Message-ID gelesen werden. Eine GUID wird erzeugt!")
|
||||
My.Application.Globix.CurrMessageID = Guid.NewGuid.ToString()
|
||||
End If
|
||||
|
||||
'Nur die MSGDatei ablegen
|
||||
Dim tempfile As String = Path.Combine(Path.GetTempPath, Path.GetFileNameWithoutExtension(msgname) & "_excl_att.msg")
|
||||
Dim oEmailFilePathWithoutAttachments = Email.Remove_AttachmentsFromEmail(pEmailFilePath, "_excl_attachments")
|
||||
|
||||
If File.Exists(tempfile) Then
|
||||
File.Delete(tempfile)
|
||||
End If
|
||||
Dim _msgEXAtt As New Msg.Message(msgname)
|
||||
_msgEXAtt.Attachments.Clear()
|
||||
_msgEXAtt.Save(tempfile)
|
||||
'Datei in Array zum Templöschen speichern
|
||||
My.Application.Globix.TEMP_FILES.Add(tempfile)
|
||||
TempFiles.Add(oEmailFilePathWithoutAttachments)
|
||||
|
||||
If Insert_GI_File(tempfile, msgonly) = True Then
|
||||
erfolgreich = True
|
||||
'Hier nun die Anhänge herauslösen
|
||||
Dim _msg As New Msg.Message(msgname)
|
||||
Dim i1 As Integer = 1
|
||||
If UserFiles.Insert_GI_File(oEmailFilePathWithoutAttachments, oMessageOnlyMarker) = True Then
|
||||
oSuccess = True
|
||||
|
||||
Logger.Info(">> Anzahl der Attachments: " & _msg.Attachments.Count)
|
||||
For Each attachment As Independentsoft.Msg.Attachment In _msg.Attachments
|
||||
If erfolgreich = False Then
|
||||
Dim oAttachments As List(Of String) = Email.Save_AttachmentsToDisk(pEmailFilePath)
|
||||
|
||||
Logger.Debug("Saved [{0}] attachments to disk.", oAttachments.Count)
|
||||
|
||||
For Each oAttachment In oAttachments
|
||||
TempFiles.Add(oAttachment)
|
||||
|
||||
Logger.Debug("Saved attachment [{0}].", oAttachment)
|
||||
oSuccess = UserFiles.Insert_GI_File(oAttachment, oExtractedAttachmentMarker)
|
||||
|
||||
If oSuccess = False Then
|
||||
Logger.Warn("Saving attachment to disk failed: [{0}]", oAttachment)
|
||||
Exit For
|
||||
End If
|
||||
Dim attachment_name As String
|
||||
If attachment.LongFileName Is Nothing Then
|
||||
attachment_name = attachment.DisplayName
|
||||
Else
|
||||
attachment_name = attachment.LongFileName
|
||||
End If
|
||||
If attachment.EmbeddedMessage IsNot Nothing Then
|
||||
attachment_name = InvalidCharacters(attachment_name)
|
||||
tempfile = Path.Combine(Path.GetTempPath, attachment_name & ".msg")
|
||||
tempfile = CType(Versionierung_Datei(tempfile), String)
|
||||
|
||||
If tempfile <> String.Empty Then
|
||||
Dim oMessage = attachment.EmbeddedMessage
|
||||
oMessage.Save(tempfile)
|
||||
My.Application.Globix.TEMP_FILES.Add(tempfile)
|
||||
Logger.Info("Attachment (" & i1 & "):" & tempfile)
|
||||
erfolgreich = Insert_GI_File(tempfile, ATT_EXTR)
|
||||
i1 += 1
|
||||
End If
|
||||
ElseIf Not attachment_name.Contains("inline") Then
|
||||
'Sonderzeichen entfernen
|
||||
attachment_name = InvalidCharacters(attachment_name)
|
||||
tempfile = Path.Combine(Path.GetTempPath, attachment_name)
|
||||
tempfile = Versionierung_Datei(tempfile)
|
||||
If tempfile <> "" Then
|
||||
attachment.Save(tempfile)
|
||||
'Datei in Array zum Templöschen speichern
|
||||
My.Application.Globix.TEMP_FILES.Add(tempfile)
|
||||
Logger.Info("Attachment (" & i1 & "):" & tempfile)
|
||||
'nun der Insert des Anhanges
|
||||
erfolgreich = Insert_GI_File(tempfile, ATT_EXTR)
|
||||
i1 += 1
|
||||
End If
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
Return erfolgreich
|
||||
|
||||
Return oSuccess
|
||||
Catch ex As Exception
|
||||
Logger.Warn("Saving email to disk failed (Email_Decay)")
|
||||
Logger.Error(ex)
|
||||
MsgBox("Error in Email_Decay: " & ex.Message, MsgBoxStyle.Critical)
|
||||
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function Insert_GI_File(filename As String, handleType As String) As Boolean
|
||||
Try
|
||||
filename = filename.Replace("'", "''")
|
||||
|
||||
Dim filename_only As String = Path.GetFileName(filename)
|
||||
Dim ins As String = "INSERT INTO TBGI_FILES_USER (FILENAME2WORK, USER@WORK,HANDLE_TYPE,FILENAME_ONLY) VALUES ('" & filename & "','" & Environment.UserName & "','" & handleType & "','" & filename_only & "')"
|
||||
Return My.DatabaseECM.ExecuteNonQuery(ins)
|
||||
|
||||
Catch ex As Exception
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
Public Function IsFileInUse(ByVal fullFilePath As String) As Boolean
|
||||
' Gibt zurück, ob die übergebene Datei momentan exklusiv zu haben ist.
|
||||
' Prüft, ob die angegeben Datei aktuell durch eine
|
||||
' andere Anwendung in Benutzung ist
|
||||
Dim ff As Integer = FreeFile()
|
||||
If System.IO.File.Exists(fullFilePath) Then
|
||||
Try
|
||||
' Versuchen, die Datei mit *exklusiven* Lese- und
|
||||
' Schreibrechten zu öffnen
|
||||
FileOpen(ff, fullFilePath, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
|
||||
Catch ex As Exception
|
||||
' Ist ein Fehler aufgetreten, so wird nach außen hin generell
|
||||
' davon ausgegangen, dass die Datei in Benutzung ist (obwohl
|
||||
' auch andere Ursachen, etwa Rechteprobleme, möglich sind).
|
||||
Logger.Info(">> FileInUse Message: " & ex.Message)
|
||||
Return True
|
||||
Finally
|
||||
' Die eventuell geöffnete Datei schließen
|
||||
FileClose(ff)
|
||||
End Try
|
||||
Return False
|
||||
Else
|
||||
Return False
|
||||
End If
|
||||
End Function
|
||||
Public Function Versionierung_Datei(Dateiname As String) As String
|
||||
Dim extension As String
|
||||
Dim _NewFileString As String = ""
|
||||
Try
|
||||
Dim version As Integer = 1
|
||||
|
||||
Dim Stammname As String = Path.GetDirectoryName(Dateiname) & "\" & Path.GetFileNameWithoutExtension(Dateiname)
|
||||
extension = Path.GetExtension(Dateiname)
|
||||
|
||||
Dim _neuername As String = Stammname
|
||||
'Dim MoveFilename As String = DATEINAME.Replace(element.Value, "")
|
||||
'Überprüfen ob File existiert
|
||||
If File.Exists(_neuername & extension) = False Then
|
||||
_NewFileString = _neuername
|
||||
Else
|
||||
Do While File.Exists(_neuername & extension)
|
||||
version = version + 1
|
||||
_neuername = Stammname & "~" & version
|
||||
_NewFileString = _neuername
|
||||
Loop
|
||||
End If
|
||||
Return _NewFileString & extension
|
||||
Catch ex As Exception
|
||||
Logger.Info(" - Error in versioning file - error: " & vbNewLine & ex.Message)
|
||||
Logger.Error(ex)
|
||||
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in versioning file:")
|
||||
Return ""
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
|
||||
Reference in New Issue
Block a user