From a9210725564dee088a03f0b0d1c8ca661d62c9bb Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 25 Jan 2022 13:24:28 +0100 Subject: [PATCH] fix file access error and illegal path error --- Global_Indexer/ClassFileDrop.vb | 95 ++++++++++++++++------------ Global_Indexer/ClassFilehandle.vb | 33 ++++++---- Global_Indexer/Global_Indexer.vbproj | 2 +- Global_Indexer/frmIndex.vb | 24 +------ Global_Indexer/frmStart.vb | 16 +++++ Global_Indexer/packages.config | 2 +- 6 files changed, 92 insertions(+), 80 deletions(-) diff --git a/Global_Indexer/ClassFileDrop.vb b/Global_Indexer/ClassFileDrop.vb index f48dc73..a1bc217 100644 --- a/Global_Indexer/ClassFileDrop.vb +++ b/Global_Indexer/ClassFileDrop.vb @@ -41,20 +41,26 @@ Public Class ClassFileDrop '// '// set up to obtain the aryFileGroupDescriptor '// and extract the file name - Dim oStream As IO.Stream = CType(e.Data.GetData("FileGroupDescriptor"), IO.Stream) - Dim aryFileGroupDescriptor(512) As Byte ' = new byte[512] - oStream.Read(aryFileGroupDescriptor, 0, 512) - '// used to build the stbFileName from the aryFileGroupDescriptor block Dim stbFileName As System.Text.StringBuilder = New System.Text.StringBuilder("") - '// this trick gets the stbFileName of the passed attached file - Dim intCnt As Integer = 76 - Do While aryFileGroupDescriptor(intCnt) <> 0 - stbFileName.Append(Convert.ToChar(aryFileGroupDescriptor(intCnt), System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"))) - intCnt += 1 - Loop - oStream.Close() + + Using oStream As IO.Stream = CType(e.Data.GetData("FileGroupDescriptor"), IO.Stream) + Dim aryFileGroupDescriptor(512) As Byte ' = new byte[512] + oStream.Read(aryFileGroupDescriptor, 0, 512) + '// used to build the stbFileName from the aryFileGroupDescriptor block + + '// this trick gets the stbFileName of the passed attached file + Dim intCnt As Integer = 76 + Do While aryFileGroupDescriptor(intCnt) <> 0 + stbFileName.Append(Convert.ToChar(aryFileGroupDescriptor(intCnt), System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"))) + intCnt += 1 + Loop + oStream.Close() + End Using + 'Sonderzeichen entfernen - Dim oTempFileName = DigitalData.Modules.Language.Utils.RemoveInvalidCharacters(stbFileName.ToString) + Dim oTempFileName = DigitalData.Modules.Language.Utils.ConvertTextToSlug(stbFileName.ToString) + LOGGER.Info("Slug for Filename: [{0}]", oTempFileName) + Dim oAttachments = e.Data.GetDataPresent("FileContents") Dim strOutFile As String = Path.Combine(Path.GetTempPath(), oTempFileName) '// create the full-path name @@ -64,35 +70,37 @@ Public Class ClassFileDrop '// data for the attached file and copy it to disk so we work on it. '// '// get the actual raw file into memory - Dim oMemoryStreamInput As IO.MemoryStream = CType(e.Data.GetData("FileContents", True), IO.MemoryStream) 'This returns nothing for an Email - If oMemoryStreamInput Is Nothing = False Then - '// allocate enough bytes to hold the raw date - Dim aryFileBytes(CType(oMemoryStreamInput.Length, Int32)) As Byte - '// set starting position at first byte and read in the raw data - oMemoryStreamInput.Position = 0 - oMemoryStreamInput.Read(aryFileBytes, 0, CType(oMemoryStreamInput.Length, Int32)) - '// create a file and save the raw zip file to it - Dim fsOutput As IO.FileStream = New IO.FileStream(strOutFile, IO.FileMode.Create) '; - fsOutput.Write(aryFileBytes, 0, aryFileBytes.Length) - fsOutput.Close() ' // close the file - Dim resultVersion = ClassFilehandle.Versionierung_Datei(strOutFile) - If resultVersion <> "" Then - strOutFile = resultVersion - End If - Dim finTemp As IO.FileInfo = New IO.FileInfo(strOutFile) - '// always good to make sure we actually created the file - If (finTemp.Exists = True) Then - LOGGER.Info("Drop an Attachment - File: " & strOutFile) - FilesDropped.Add("|OUTLOOK_ATTACHMENT|" & strOutFile) - - 'ReDim Preserve FilesDropped(0) - 'FilesDropped(0) = "|OUTLOOK_ATTACHMENT|" & strOutFile - - Return True - Else - LOGGER.Info("Attachment File from Outlook could not be created") + 'Dim oMemoryStreamInput As IO.MemoryStream = CType(e.Data.GetData("FileContents", True), IO.MemoryStream) 'This returns nothing for an Email + Using oStream As MemoryStream = CType(e.Data.GetData("FileContents", True), IO.MemoryStream) + If oStream Is Nothing = False Then + '// allocate enough bytes to hold the raw date + Dim aryFileBytes(CType(oStream.Length, Int32)) As Byte + '// set starting position at first byte and read in the raw data + oStream.Position = 0 + oStream.Read(aryFileBytes, 0, CType(oStream.Length, Int32)) + '// create a file and save the raw zip file to it + Dim fsOutput As IO.FileStream = New IO.FileStream(strOutFile, IO.FileMode.Create) '; + fsOutput.Write(aryFileBytes, 0, aryFileBytes.Length) + fsOutput.Close() ' // close the file + Dim resultVersion = ClassFilehandle.Versionierung_Datei(strOutFile) + If resultVersion <> "" Then + strOutFile = resultVersion + End If + Dim finTemp As IO.FileInfo = New IO.FileInfo(strOutFile) + '// always good to make sure we actually created the file + If (finTemp.Exists = True) Then + LOGGER.Info("Drop an Attachment - File: " & strOutFile) + FilesDropped.Add("|OUTLOOK_ATTACHMENT|" & strOutFile) + + 'ReDim Preserve FilesDropped(0) + 'FilesDropped(0) = "|OUTLOOK_ATTACHMENT|" & strOutFile + + Return True + Else + LOGGER.Info("Attachment File from Outlook could not be created") + End If End If - End If + End Using End If If e.Data.GetDataPresent("FileGroupDescriptor") Then Dim oApp As Outlook.Application @@ -115,9 +123,11 @@ Public Class ClassFileDrop End If 'Sonderzeichen entfernen - subj = DigitalData.Modules.Language.Utils.RemoveInvalidCharacters(subj) + Dim oSubjectSlug = DigitalData.Modules.Language.Utils.ConvertTextToSlug(subj) + LOGGER.Info("Subject Slug for Filename: [{0}]", oSubjectSlug) + 'hardcode a destination path for testing - Dim oFilename As String = IO.Path.Combine(Path.GetTempPath, subj + ".msg") + Dim oFilename As String = IO.Path.Combine(Path.GetTempPath, oSubjectSlug + ".msg") oFilename = oFilename.Replace("?", "") oFilename = oFilename.Replace("!", "") @@ -146,6 +156,7 @@ Public Class ClassFileDrop End If Catch ex As Exception + LOGGER.Error(ex) MsgBox("Error in Drop-File" & vbNewLine & ex.Message, MsgBoxStyle.Critical) Return False End Try diff --git a/Global_Indexer/ClassFilehandle.vb b/Global_Indexer/ClassFilehandle.vb index 4394da3..893441b 100644 --- a/Global_Indexer/ClassFilehandle.vb +++ b/Global_Indexer/ClassFilehandle.vb @@ -6,21 +6,26 @@ Imports DigitalData.Modules.Language Imports Limilabs.Mail Public Class ClassFilehandle - Public Shared Function Decide_FileHandle(pFilepath As String, pHandletype As String) + Public Shared Function Decide_FileHandle(pFilepath As String, pHandletype As String) As Boolean Try - 'TODO: Before doing anything, clean the filename - Dim oFilename = IO.Path.GetFileName(pFilepath) - Dim oCleanFileName = Utils.RemoveInvalidCharacters(oFilename) - Dim oTempDirectory = IO.Path.GetTempPath() - Dim oTempFilePath = IO.Path.Combine(oTempDirectory, oCleanFileName) - - Try - TEMP_FILES.Add(oTempFilePath) - IO.File.Copy(pFilepath, oTempFilePath, True) - Catch ex As Exception - LOGGER.Error(ex) - Throw ex - End Try + ''TODO: Before doing anything, clean the filename + 'Dim oFilename = IO.Path.GetFileName(pFilepath) + 'Dim oCleanFileName = Utils.RemoveInvalidCharacters(oFilename) + 'Dim oTempDirectory = IO.Path.GetTempPath() + 'Dim oTempFilePath = IO.Path.Combine(oTempDirectory, oCleanFileName) + + 'Try + ' TEMP_FILES.Add(oTempFilePath) + ' LOGGER.Debug("Copying file") + ' LOGGER.Debug(pFilepath) + ' LOGGER.Debug(oTempFilePath) + ' IO.File.Copy(pFilepath, oTempFilePath, True) + 'Catch ex As Exception + ' LOGGER.Error(ex) + ' Throw ex + 'End Try + + Dim oTempFilePath = pFilepath If oTempFilePath.ToUpper.EndsWith(".MSG") Or oTempFilePath.ToUpper.EndsWith(".EML") Then CURRENT_MESSAGEID = "" diff --git a/Global_Indexer/Global_Indexer.vbproj b/Global_Indexer/Global_Indexer.vbproj index 86a50c5..9a67d65 100644 --- a/Global_Indexer/Global_Indexer.vbproj +++ b/Global_Indexer/Global_Indexer.vbproj @@ -210,7 +210,7 @@ - ..\packages\NLog.4.7.10\lib\net45\NLog.dll + ..\packages\NLog.4.7.13\lib\net45\NLog.dll P:\Visual Studio Projekte\Bibliotheken\Oracle.ManagedDataAccess.dll diff --git a/Global_Indexer/frmIndex.vb b/Global_Indexer/frmIndex.vb index c3cd09b..281b8be 100644 --- a/Global_Indexer/frmIndex.vb +++ b/Global_Indexer/frmIndex.vb @@ -713,7 +713,7 @@ Public Class frmIndex End Try End Function - Private Function Write_Indizes() + Private Function Write_Indizes() As Boolean Try _Logger.NewBlock("Write_Indizes") _Logger.Info("Indexing file [{0}]", CURRENT_NEWFILENAME) @@ -906,7 +906,7 @@ Public Class frmIndex Dim oSubject As String = oMail.Subject Dim oDateIn As Date = oMail.Date - CURRENT_MESSAGEID = oMessageFrom + CURRENT_MESSAGEID = oMessageId CURRENT_MESSAGEDATE = oDateIn CURRENT_MESSAGESUBJECT = oSubject @@ -2785,33 +2785,13 @@ Public Class frmIndex Me.Close() End If End If - - ' Clear all temp files after indexing - Clear_Tempfiles() - EMAIL.Clear_TempFiles() - Catch ex As Exception MsgBox("Uncaught error while indexing: " & vbNewLine & ex.Message, MsgBoxStyle.Critical, Text) - - ' Clear all temp files in case of an error - Clear_Tempfiles() - EMAIL.Clear_TempFiles() Finally Me.Cursor = Cursors.Default End Try End Sub - Private Sub Clear_Tempfiles() - 'TempDateien löschen - For Each oFile In TEMP_FILES - Try - System.IO.File.Delete(oFile) - Catch ex As Exception - LOGGER.Error(ex) - End Try - Next - End Sub - Private Sub checkItemPreview_CheckedChanged(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles checkItemPreview.CheckedChanged SetFilePreview(checkItemPreview.Checked) CONFIG.Config.FilePreview = checkItemPreview.Checked diff --git a/Global_Indexer/frmStart.vb b/Global_Indexer/frmStart.vb index f855727..69f06a7 100644 --- a/Global_Indexer/frmStart.vb +++ b/Global_Indexer/frmStart.vb @@ -136,9 +136,25 @@ Public Class frmStart Catch ex As Exception LOGGER.Error(ex) MsgBox("Unexpected Error in Check_Dropped_Files:" & vbNewLine & ex.Message, MsgBoxStyle.Critical) + + Finally + ' Clear all temp files after indexing + Clear_Tempfiles() + EMAIL.Clear_TempFiles() End Try End Sub + Private Sub Clear_Tempfiles() + 'TempDateien löschen + For Each oFile In TEMP_FILES + Try + System.IO.File.Delete(oFile) + Catch ex As Exception + LOGGER.Error(ex) + End Try + Next + End Sub + Sub Open_IndexDialog() Try Hide() diff --git a/Global_Indexer/packages.config b/Global_Indexer/packages.config index 63f3075..f467140 100644 --- a/Global_Indexer/packages.config +++ b/Global_Indexer/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file