09-08-2023

This commit is contained in:
Jonathan Jenne
2023-08-09 09:10:03 +02:00
parent c7f4885078
commit 3e41e5b469
44 changed files with 1024 additions and 638 deletions

View File

@@ -0,0 +1,132 @@
Imports System.ComponentModel
Imports System.IO
Imports System.Threading.Tasks
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Namespace Sharepoint
Public Class SharepointSync
Inherits BaseModule
Implements ISync
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
Public Overrides Async Function Run() As Task
Try
AddInfoEntry("Starting SharepointSync.")
AddDivider()
EnsureOutputDirectoryExists()
Dim oExtDocIds = Await FetchDocIds()
If oExtDocIds Is Nothing Then
Throw New ApplicationException($"Document Ids could not be fetched!")
End If
For Each oDocId As String In oExtDocIds
Try
Logger.Debug("Fetching document from Database..")
Dim oDocument = Await GetDocumentContent(oDocId)
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)
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)
Await Database.ExecuteNonQueryAsync(oSQL)
Catch ex As Exception
Logger.Error(ex)
AddWarnEntry("Error while running Sync: " & ex.Message)
End Try
Next
'TODO
AddInfoEntry("Finished Sync.")
Catch ex As Exception
Logger.Error(ex)
AddWarnEntry("Error while running Sync: " & ex.Message)
End Try
End Function
Public Overrides Function Cleanup() As Task
Return Task.CompletedTask
End Function
Public Overrides Function TestConfigIsComplete() As Boolean
Dim oComplete = TestConfigIsCompleteBase()
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
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)
If oTable Is Nothing OrElse oTable.Rows.Count = 0 Then
Logger.Warn("Document with Id [{0}] was not found in SharePoint Database!", pDocumentId)
Return Nothing
End If
Dim oRow = oTable.Rows.Item(0)
Dim oFilename As String = oRow.ItemEx("LeafName", "")
Dim oPath As String = oRow.ItemEx("DirName", "")
Dim oContent As Byte() = CType(oRow.Item("Content"), Byte())
Return New Entities.SharepointDocument() With {
.Id = pDocumentId,
.Name = oFilename,
.Path = oPath,
.Data = oContent
}
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
End Class
End Namespace