11-08-2023

This commit is contained in:
Jonathan Jenne 2023-08-11 09:45:34 +02:00
parent 3e41e5b469
commit 2579d7abdd
7 changed files with 74 additions and 38 deletions

View File

@ -11,6 +11,11 @@
'''
''' - {0} for ExtDocId
''' - {1} for Filename
'''
''' These placeholders are optional and will be replaced if they are found in the query
''' - [String1]
''' - for Sharepoint Path when Sharepoint Module is used
''' - not used in slt Module
''' </summary>
Public Property SQLQueryExport As String = ""
''' <summary>
@ -31,7 +36,7 @@
''' <summary>
''' The connection string of the sharepoint database. Should contain the tables AllDocs and AllDocStreams
''' </summary>
Public Property SharepointConnectionString As String = ""
Public Property SharepointDatabase As String = ""
End Class
Public Class sltConfig

View File

@ -97,7 +97,9 @@
<Compile Include="Config.vb" />
<Compile Include="Modules\BaseModule.vb" />
<Compile Include="Modules\ISync.vb" />
<Compile Include="Modules\Sharepoint\Constants.vb" />
<Compile Include="Modules\Sharepoint\Entities\SharepointDocument.vb" />
<Compile Include="Modules\Sharepoint\SharepointException.vb" />
<Compile Include="Modules\Sharepoint\SharepointSync.vb" />
<Compile Include="Modules\slt\Constants.vb" />
<Compile Include="Modules\slt\Entities\sltDocument.vb" />

View File

@ -57,6 +57,13 @@ Public MustInherit Class BaseModule
End If
End Sub
Friend Function ConvertFilenameToSlug(pFileName As String) As String
Dim oName = Path.GetFileNameWithoutExtension(pFileName)
Dim oExtension = Path.GetExtension(pFileName)
Return StringEx.ConvertTextToSlug(oName) & oExtension
End Function
Friend Function CopyFileToOutputPath(pFileContents As Byte(), pFilePath As String) As Boolean
Try
Using oStream As New MemoryStream(pFileContents)

View File

@ -0,0 +1,7 @@
Namespace Sharepoint
Public Class Constants
Public Enum ErrorType
GetDocumentError
End Enum
End Class
End Namespace

View File

@ -0,0 +1,12 @@
Namespace Sharepoint
Public Class SharepointException
Inherits ApplicationException
Public ReadOnly ErrorType As Constants.ErrorType
Public Sub New(pErrorType As Constants.ErrorType, pMessage As String)
MyBase.New(pMessage)
ErrorType = pErrorType
End Sub
End Class
End Namespace

View File

@ -10,13 +10,11 @@ Namespace Sharepoint
Inherits BaseModule
Implements ISync
Private Const STRING1_PLACEHOLDER = "[String1]"
Public Overrides Property Name As String = "Sharepoint Sync"
Public Overrides Property IsLoggedIn As Boolean
Private ReadOnly AllowedExtensions As New List(Of String) From {
"xls", "xlsx", "pdf", "dxf", "dwg", "doc", "docx", "ppt", "pptx", "jpg", "bmp", "msg", "eml", "png"
}
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pConfig As Config)
MyBase.New(pLogConfig, pDatabase, pConfig)
End Sub
@ -37,31 +35,33 @@ Namespace Sharepoint
Try
Logger.Debug("Fetching document from Database..")
Dim oDocument = Await GetDocumentContent(oDocId)
If oDocument Is Nothing Then
Throw New SharepointException(Constants.ErrorType.GetDocumentError, "Document was not found!")
End If
Logger.Debug("Document fetched!")
AddInfoEntry("Document: [{0}]", oDocument.Name)
Logger.Info("ExtDocId: [{0}]", oDocument.Id)
' TODO: split path and build final sub path
' Beispiel Pfad:
' Netze/Betrieb/Baumassnahmen/Zählerwechsel 2012/Wasserzähler und Ausbauscheine/2012-06-25
Logger.Debug("Original Document Path: [{0}]", oDocument.Path)
Dim oSplitPaths = oDocument.Path.Split("/"c).
Select(Function(folder) StringEx.ConvertTextToSlug(folder)).
ToArray()
Dim oDocumentPath = String.Join("\", oSplitPaths)
Logger.Debug("Normalized Document Path: [{0}]", oDocument.Path)
Dim oDirectoryPath = Path.Combine(Config.OutputDirectory, oDocument.Path.Replace("/", "\"))
Dim oFilePath = Path.Combine(oDirectoryPath, oDocument.Name)
Logger.Debug("Final Document Path: [{0}]", oFilePath)
Dim oFileName = ConvertFilenameToSlug(oDocument.Name)
Dim oSubPath = FileEx.CreateDateDirectory(Config.OutputDirectory)
If oSubPath Is Nothing Then
Throw New ApplicationException("Output sub path could not be created!")
End If
Dim oFilePath = Path.Combine(oSubPath, oFileName)
Logger.Debug("Subdirectory [{0}] created.", oSubPath)
If CopyFileToOutputPath(oDocument.Data, oFilePath) = False Then
Throw New ApplicationException("File could not be created in output path!")
End If
Dim oSQL = String.Format(Config.SQLQueryExport, oDocument.Id, oDocument.Name)
Dim oSQL = String.Format(Config.SQLQueryExport, oDocument.Id, oFileName)
If oSQL.Contains(STRING1_PLACEHOLDER) Then
oSQL.Replace(STRING1_PLACEHOLDER, oDocument.Path)
End If
Await Database.ExecuteNonQueryAsync(oSQL)
Catch ex As Exception
@ -79,6 +79,8 @@ Namespace Sharepoint
End Try
End Function
Public Overrides Function Cleanup() As Task
Return Task.CompletedTask
End Function
@ -86,21 +88,24 @@ Namespace Sharepoint
Public Overrides Function TestConfigIsComplete() As Boolean
Dim oComplete = TestConfigIsCompleteBase()
If Config.SharepointConfiguration.SharepointDatabase = String.Empty Then
AddWarnEntry("Configuration for 'SharepointDatabase' is empty.")
oComplete = False
End If
Return oComplete
End Function
Private Async Function GetDocumentContent(pDocumentId As String) As Task(Of Entities.SharepointDocument)
Try
Dim oExtensionList = AllowedExtensions.
Select(Function(ext) $"'{ext}'").
ToList()
Dim oSql As String = $"SELECT T.*, T2.Content, T2.InternalVersion
FROM [AllDocs] T
INNER JOIN [AllDocStreams] T2
Dim oSql As String = $"SELECT T.LeafName, T.DirName, T2.Content
FROM {Config.SharepointConfiguration.SharepointDatabase}.[AllDocs] T
INNER JOIN {Config.SharepointConfiguration.SharepointDatabase}.[AllDocStreams] T2
ON T.Id = T2.Id AND T.InternalVersion = T2.InternalVersion AND T.SiteId = T2.SiteId
WHERE T.Extension in ({String.Join(",", oExtensionList)})
T.Id = '{pDocumentId}'"
Dim oTable As DataTable = Database.GetDatatableWithConnection(oSql, Config.SharepointConfiguration.SharepointConnectionString)
WHERE T.Id = '{pDocumentId}'"
Dim oTable As DataTable = Database.GetDatatable(oSql)
If oTable Is Nothing OrElse oTable.Rows.Count = 0 Then
Logger.Warn("Document with Id [{0}] was not found in SharePoint Database!", pDocumentId)
@ -126,7 +131,5 @@ Namespace Sharepoint
Return Nothing
End Try
End Function
End Class
End Namespace

View File

@ -16,7 +16,7 @@ Namespace slt
Inherits BaseModule
Implements ISync
Private ReadOnly FileEx As FileEx
Private ReadOnly MimeEx As MimeEx
Public Overrides Property Name As String = "slt Sync"
Public Overrides Property IsLoggedIn As Boolean = False
@ -25,7 +25,7 @@ Namespace slt
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pConfig As Config)
MyBase.New(pLogConfig, pDatabase, pConfig)
FileEx = New FileEx()
MimeEx = New MimeEx(pLogConfig)
End Sub
Public Overrides Async Function Run() As Threading.Tasks.Task Implements ISync.Run
@ -123,7 +123,7 @@ Namespace slt
pMimetype = "application/vnd.ms-outlook"
End If
Dim oExtension = FileEx.GetExtension(pMimetype)
Dim oExtension = MimeEx.GetExtension(pMimetype)
Return StringEx.ConvertTextToSlug(pFilename) & oExtension
Catch ex As Exception
@ -166,10 +166,10 @@ Namespace slt
Dim oUrl = "/slt/External/System/Authentication/Json/Login"
Dim oParams = New Dictionary(Of String, String) From {
{"systemid", pSystemId},
{"user", Config.sltConfiguration.Username},
{"password", Config.sltConfiguration.Password}
}
{"systemid", pSystemId},
{"user", Config.sltConfiguration.Username},
{"password", Config.sltConfiguration.Password}
}
Logger.Debug("Username: [{0}]", Config.sltConfiguration.Username)
Logger.Debug("SystemId: [{0}]", pSystemId)