general clean up, add file contents to getdocumentbyx methods

This commit is contained in:
Jonathan Jenne 2019-04-10 16:15:00 +02:00
parent dee5d33bb9
commit 696a8ddbf6
12 changed files with 196 additions and 44 deletions

View File

@ -8,10 +8,10 @@ Public Class JobRunner
Private WithEvents _workerThread As BackgroundWorker Private WithEvents _workerThread As BackgroundWorker
Private WithEvents _workerTimer As Timer Private WithEvents _workerTimer As Timer
Private _interval As Long Private ReadOnly _interval As Long
Private _logConfig As LogConfig Private ReadOnly _logConfig As LogConfig
Private _logger As Logger Private ReadOnly _logger As Logger
Private _firebird As Firebird Private ReadOnly _firebird As Firebird
Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Interval As Long) Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Interval As Long)
_logConfig = LogConfig _logConfig = LogConfig
@ -21,7 +21,7 @@ Public Class JobRunner
_workerTimer = New Timer() _workerTimer = New Timer()
_workerThread = New BackgroundWorker() With { _workerThread = New BackgroundWorker() With {
.WorkerReportsProgress = False, .WorkerReportsProgress = True,
.WorkerSupportsCancellation = True .WorkerSupportsCancellation = True
} }
End Sub End Sub
@ -34,6 +34,7 @@ Public Class JobRunner
Public Sub [Stop]() Public Sub [Stop]()
Try Try
_logger.Debug("Stopping Background worker...")
If _workerThread.IsBusy Then If _workerThread.IsBusy Then
_workerThread.CancelAsync() _workerThread.CancelAsync()
_logger.Debug("Background Worker cancelled.") _logger.Debug("Background Worker cancelled.")
@ -49,35 +50,32 @@ Public Class JobRunner
If Not _workerThread.IsBusy Then If Not _workerThread.IsBusy Then
_workerThread.RunWorkerAsync() _workerThread.RunWorkerAsync()
Else Else
_logger.Warn("Background Worker is busy") _logger.Warn("Background Worker is busy. Waiting for next interval.")
End If End If
End Sub End Sub
Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork
Try _logger.Debug("Background worker running..")
Dim args As WorkerArgs = e.Argument
_logger.Debug("Background worker running..") Dim args As WorkerArgs = e.Argument
Dim oJob As New ADSyncJob(_logConfig, _firebird)
Dim oArgs As New ADSyncArgs() With {
.Enabled = My.Settings.JOB_ADSYNC_ENABLED,
.Interval = My.Settings.JOB_ADSYNC_INTERVAL,
.RootPath = My.Settings.JOB_ADSYNC_ROOT_PATH
}
' TODO: WORK If oJob.ShouldStart(oArgs) Then
oJob.Start(oArgs)
Dim oJob As New ADSyncJob(_logConfig, _firebird) End If
Dim oArgs As New ADSyncArgs() With {
.Enabled = My.Settings.JOB_ADSYNC_ENABLED,
.Interval = My.Settings.JOB_ADSYNC_INTERVAL,
.RootPath = My.Settings.JOB_ADSYNC_ROOT_PATH
}
If oJob.ShouldStart(oArgs) Then
oJob.Start(oArgs)
End If
Catch ex As Exception
_logger.Warn("Background worker failed!")
_logger.Error(ex)
End Try
End Sub End Sub
Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted
_logger.Debug("Background worker completed!") If e.Error Is Nothing Then
_logger.Debug("Background worker completed!")
Else
_logger.Warn("Background worker failed!")
_logger.Error(e.Error)
End If
End Sub End Sub
End Class End Class

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup> </startup>
</configuration> <system.data>
<DbProviderFactories>
<remove invariant="FirebirdSql.Data.FirebirdClient" />
<add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
</DbProviderFactories>
</system.data></configuration>

View File

@ -5,7 +5,6 @@ Imports System.Linq
Imports System.Text.RegularExpressions Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Database Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Jobs
Imports DigitalData.Modules.Jobs.Exceptions Imports DigitalData.Modules.Jobs.Exceptions
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports FirebirdSql.Data.FirebirdClient Imports FirebirdSql.Data.FirebirdClient

View File

@ -101,7 +101,9 @@
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="FirebirdSql.Data.FirebirdClient, Version=6.4.0.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" /> <Reference Include="FirebirdSql.Data.FirebirdClient, Version=6.4.0.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c, processorArchitecture=MSIL">
<HintPath>..\packages\FirebirdSql.Data.FirebirdClient.6.4.0\lib\net452\FirebirdSql.Data.FirebirdClient.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.5.11\lib\net45\NLog.dll</HintPath> <HintPath>..\packages\NLog.4.5.11\lib\net45\NLog.dll</HintPath>

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="FirebirdSql.Data.FirebirdClient" version="6.4.0" targetFramework="net461" />
<package id="NLog" version="4.5.11" targetFramework="net461" /> <package id="NLog" version="4.5.11" targetFramework="net461" />
</packages> </packages>

View File

@ -1,20 +1,148 @@
Imports Independentsoft.Email Imports Independentsoft.Email
Imports Independentsoft.Email.Pop3 Imports Independentsoft.Email.Pop3
Imports Independentsoft.Email.Smtp
Imports Independentsoft.Email.Mime Imports Independentsoft.Email.Mime
Imports Independentsoft.Email.Imap Imports Independentsoft.Email.Imap
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports System.Net.Mail Imports System.Net.Mail
Imports System.Net Imports System.Net
Imports System.Reflection
Imports System.IO
Public Class Email Public Class Email
Private _logger As Logging.Logger Private ReadOnly _logger As Logging.Logger
Private _logConfig As LogConfig Private ReadOnly _logConfig As LogConfig
Public Sub New(LogConfig As LogConfig) Public Sub New(LogConfig As LogConfig)
_logger = LogConfig.GetLogger() _logger = LogConfig.GetLogger()
_logConfig = LogConfig _logConfig = LogConfig
End Sub End Sub
''' <summary>
''' Tests connection to a given IMAP Server by connecting and doing a simple message query.
''' </summary>
''' <param name="Server">IP-Address or Domainname of Server</param>
''' <param name="Port">IMAP-Port</param>
''' <param name="Username">IMAP-Username</param>
''' <param name="Password">IMAP-Password</param>
''' <param name="Folder">The folder to fetch messages from. Defaults to `Inbox`</param>
''' <returns>True if connection and query were successful. False otherwise.</returns>
Public Function TestIMAPLogin(Server As String, Port As Integer, Username As String, Password As String, Optional Folder As String = "Inbox") As Boolean
_logger.Debug("Testing Login to Server {0}:{1} with user {2}", Server, Port, Username)
Try
_logger.Debug("Connecting...")
Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True)
If Not oClient.Authed Then
_logger.Warn("Connected to server but authentication failed.")
Return False
End If
_logger.Debug("Connection successful")
_logger.Debug("Fetching MessageIds..")
Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, Folder)
_logger.Debug("Found {0} messages", oMessageIds.Count)
_logger.Debug("Fetching messages...")
Dim oMessages As IEnumerable(Of MailMessage) = oClient.GetMessages(oMessageIds, False, Folder)
_logger.Debug("Messages fetched")
oClient.Dispose()
Return True
End Using
Catch ex As Exception
_logger.Error(ex)
Return False
End Try
End Function
''' <summary>
''' Connects to an IMAP Server with the given credentials and
''' fetches emails from the given folder.
''' Results can be filtered with `SearchCondition`
''' </summary>
''' <param name="Server">IP-Address or Domainname of Server</param>
''' <param name="Port">IMAP-Port</param>
''' <param name="Username">IMAP-Username</param>
''' <param name="Password">IMAP-Password</param>
''' <param name="Folder">The folder to fetch messages from</param>
''' <param name="SearchCondition">Filter the search command. Defaults to `All`</param>
''' <returns>A list of Independentsoft.Email.Mime.Message objects</returns>
Public Function FetchIMAPMessages(Server As String, Port As Integer, Username As String, Password As String, Folder As String, Optional SearchCondition As S22.Imap.SearchCondition = S22.Imap.SearchCondition.All) As List(Of Message)
Dim oMessages As New List(Of Message)
_logger.Debug("Connecting to Server {0}:{1} with user {2}", Server, Port, Username)
Try
_logger.Debug("Connecting...")
Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True)
If Not oClient.Authed Then
_logger.Warn("Connected to server but authentication failed.")
Return Nothing
End If
_logger.Debug("Connection successful")
_logger.Debug("Fetching MessageIds..")
Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, Folder)
_logger.Debug("Found {0} messages", oMessageIds.Count)
_logger.Debug("Fetching messages...")
' Since this needs to return a list of IndependentSoft Message objects,
' we 'convert' the .NET MailMessage objects that are fetched from the server
' by writing them temporarily to disk as an eml file and then reading them back into a Message object.
' This approach uses an unintended use of internal .NET APIs and may break in the future.
For Each oMessageId As UInteger In oMessageIds
Dim oMessage = oClient.GetMessage(oMessageId, False, Folder)
Dim oTempPath = Path.GetTempFileName()
Dim oResult = WriteMessageToFile(oMessage, oTempPath)
Dim oMsg As New Message(oTempPath)
oMessages.Add(oMsg)
Try
File.Delete(oTempPath)
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Temp file could not be deleted")
End Try
Next
_logger.Debug("{0} Messages fetched", oMessages.Count)
End Using
Return oMessages
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
''' <summary>
''' Uses a private API from MailWriter to write a MailMessage to disk.
''' May break in future versions of .NET
''' </summary>
Public Function WriteMessageToFile(Message As MailMessage, Filename As String) As Boolean
Dim oAssembly As Assembly = GetType(Mail.SmtpClient).Assembly
Dim oMailWriterType As Type = oAssembly.[GetType]("System.Net.Mail.MailWriter")
Try
Using oStream As New FileStream(Filename, FileMode.Create)
Dim oMailWriterConstructor As ConstructorInfo = oMailWriterType.GetConstructor(
BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Type() {GetType(Stream)}, Nothing
)
Dim oMailWriter As Object = oMailWriterConstructor.Invoke(New Object() {oStream})
Dim sendMethod As MethodInfo = GetType(MailMessage).GetMethod("Send", BindingFlags.Instance Or BindingFlags.NonPublic)
sendMethod.Invoke(Message, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, {oMailWriter, True, True}, Nothing)
End Using
Return True
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function IMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String) Public Function IMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
Try Try
Dim oMAIL_LIST As New ArrayList() Dim oMAIL_LIST As New ArrayList()

View File

@ -53,6 +53,9 @@
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.5.11\lib\net45\NLog.dll</HintPath> <HintPath>..\packages\NLog.4.5.11\lib\net45\NLog.dll</HintPath>
</Reference> </Reference>
<Reference Include="S22.Imap, Version=3.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\S22.Imap.3.6.0.0\lib\net40\S22.Imap.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="NLog" version="4.5.11" targetFramework="net461" /> <package id="NLog" version="4.5.11" targetFramework="net461" />
<package id="S22.Imap" version="3.6.0.0" targetFramework="net461" />
</packages> </packages>

View File

@ -47,7 +47,7 @@ Imports DigitalData.Modules.Logging
''' </summary> ''' </summary>
Public Class Firebird Public Class Firebird
Private _Logger As Logger Private _Logger As Logger
Private _MyLogger As LogConfig Private _LogConfig As LogConfig
Private _connectionServer As String Private _connectionServer As String
Private _connectionDatabase As String Private _connectionDatabase As String
Private _connectionUsername As String Private _connectionUsername As String
@ -79,15 +79,16 @@ Public Class Firebird
''' <summary> ''' <summary>
''' '''
''' </summary> ''' </summary>
''' <param name="Datasource"></param> ''' <param name="LogConfig">The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class</param>
''' <param name="Database"></param> ''' <param name="Datasource">The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03</param>
''' <param name="User"></param> ''' <param name="Database">The location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB`</param>
''' <param name="Password"></param> ''' <param name="User">The user name to connect as</param>
''' <param name="Password">The user's password</param>
''' <exception cref="Exceptions.DatabaseException"></exception> ''' <exception cref="Exceptions.DatabaseException"></exception>
Public Sub New(LogConfig As LogConfig, Datasource As String, Database As String, User As String, Password As String) Public Sub New(LogConfig As LogConfig, Datasource As String, Database As String, User As String, Password As String)
Try Try
_MyLogger = LogConfig _LogConfig = LogConfig
_Logger = _MyLogger.GetLogger() _Logger = _LogConfig.GetLogger()
Dim oConnectionString = GetConnectionString(Datasource, Database, User, Password) Dim oConnectionString = GetConnectionString(Datasource, Database, User, Password)
_connectionServer = Datasource _connectionServer = Datasource

View File

@ -254,7 +254,13 @@ Public Class EDMService
oRow.Item("ORIGINAL_FILENAME") oRow.Item("ORIGINAL_FILENAME")
) )
Return New DocumentResult(oDocument) TestFileExists(oDocument.ContainerId)
Dim oContainerPath = GetContainerPath(oDocument.ContainerId)
Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath)
Dim oContents As Byte() = oContainer.GetFile().Contents
Return New DocumentResult(oDocument, oContents)
Catch ex As Exception Catch ex As Exception
Return New DocumentResult(ex.Message) Return New DocumentResult(ex.Message)
End Try End Try
@ -276,7 +282,13 @@ Public Class EDMService
oRow.Item("ORIGINAL_FILENAME") oRow.Item("ORIGINAL_FILENAME")
) )
Return New DocumentResult(oDocument) TestFileExists(oDocument.ContainerId)
Dim oContainerPath = GetContainerPath(oDocument.ContainerId)
Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath)
Dim oContents As Byte() = oContainer.GetFile().Contents
Return New DocumentResult(oDocument, oContents)
Catch ex As Exception Catch ex As Exception
Return New DocumentResult(ex.Message) Return New DocumentResult(ex.Message)
End Try End Try

View File

@ -38,6 +38,8 @@ Interface IEDMService
<OperationContract> <OperationContract>
Function DeleteFile(DocObject As DocumentObject) As Boolean Function DeleteFile(DocObject As DocumentObject) As Boolean
#End Region #End Region
#Region "Utils" #Region "Utils"

View File

@ -3,7 +3,7 @@
<Serializable> <Serializable>
<DataContract> <DataContract>
<KnownType(GetType(DBNull))> <KnownType(GetType(DBNull))>
Public Class BaseResult Public MustInherit Class BaseResult
<DataMember> <DataMember>
Public Property OK As Boolean Public Property OK As Boolean
<DataMember> <DataMember>