File Container, Prepare Import for Marvman
This commit is contained in:
@@ -85,6 +85,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AppConfig.vb" />
|
||||
<Compile Include="ContainerResult.vb" />
|
||||
<Compile Include="DocumentResult.vb" />
|
||||
<Compile Include="Exceptions.vb" />
|
||||
<Compile Include="DatabaseResult.vb" />
|
||||
<Compile Include="EDMService.vb" />
|
||||
|
||||
20
SERVICES/DDEDM_NetworkService/DocumentResult.vb
Normal file
20
SERVICES/DDEDM_NetworkService/DocumentResult.vb
Normal file
@@ -0,0 +1,20 @@
|
||||
Imports DigitalData.Modules.Filesystem
|
||||
|
||||
<Serializable>
|
||||
Public Class DocumentResult
|
||||
Public ReadOnly OK As Boolean
|
||||
Public ReadOnly ErrorMessage As String
|
||||
Public Document As DocumentObject
|
||||
Public HasContents As Boolean
|
||||
Public Contents As Byte()
|
||||
|
||||
Public Sub New()
|
||||
OK = True
|
||||
ErrorMessage = Nothing
|
||||
End Sub
|
||||
|
||||
Public Sub New(ErrorMessage As String)
|
||||
OK = False
|
||||
Me.ErrorMessage = ErrorMessage
|
||||
End Sub
|
||||
End Class
|
||||
@@ -3,7 +3,6 @@ Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports DigitalData.Modules.Filesystem
|
||||
Imports System.IO
|
||||
Imports DigitalData.Services.EDMService
|
||||
|
||||
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)>
|
||||
Public Class EDMService
|
||||
@@ -28,17 +27,35 @@ Public Class EDMService
|
||||
_logger = LogConfig.GetLogger()
|
||||
End Sub
|
||||
|
||||
#Region "Auth"
|
||||
Private Function TestUserAuth() As Boolean
|
||||
Try
|
||||
Dim oSQL As String = $"SELECT FNICM_AUTH_USER('{_username}') FROM RDB$DATABASE;"
|
||||
Dim oResult As Boolean = Database.GetScalarValue(oSQL)
|
||||
|
||||
Return oResult
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
#End Region
|
||||
|
||||
#Region "Heartbeat"
|
||||
Public Function Heartbeat() As Boolean Implements IEDMService.Heartbeat
|
||||
Return True
|
||||
End Function
|
||||
#End Region
|
||||
#Region "Request"
|
||||
Public Function CreateDatabaseRequest(Name As String, Optional Debug As Boolean = False) As String Implements IEDMService.CreateDatabaseRequest
|
||||
Public Sub CreateRequest(Name As String, Optional Debug As Boolean = False)
|
||||
_request = New Request(Name, _username, Database, Debug)
|
||||
_debug = Debug
|
||||
|
||||
_logger.Info("Creating request {0}/{1}", _request.Name, _request.RequestId)
|
||||
End Sub
|
||||
|
||||
Public Function CreateDatabaseRequest(Name As String, Optional Debug As Boolean = False) As String Implements IEDMService.CreateDatabaseRequest
|
||||
CreateRequest(Name, Debug)
|
||||
|
||||
Return _request.Name
|
||||
End Function
|
||||
@@ -111,80 +128,123 @@ Public Class EDMService
|
||||
|
||||
|
||||
#End Region
|
||||
#Region "FileContainer"
|
||||
Private Function GetFilePath(ContainerId As String) As String
|
||||
Return Path.Combine(AppConfig.ContainerPath, ContainerId & ".enc")
|
||||
End Function
|
||||
Private Sub TestFileExists(ContainerId)
|
||||
Dim oContainerPath = GetFilePath(ContainerId)
|
||||
|
||||
If Not IO.File.Exists(oContainerPath) Then
|
||||
Throw New FileNotFoundException("Container existiert nicht", oContainerPath)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Public Function GetFile(ContainerId As String) As ContainerResult Implements IEDMService.GetFile
|
||||
#Region "Document"
|
||||
Public Function CreateFile(FileName As String, Contents() As Byte) As DocumentResult Implements IEDMService.CreateFile
|
||||
Try
|
||||
TestFileExists(ContainerId)
|
||||
Dim oContainer As FileContainer
|
||||
Dim oContainerId As String
|
||||
|
||||
Dim oContainerPath = GetFilePath(ContainerId)
|
||||
Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath)
|
||||
If Not TestUserAuth() Then
|
||||
Throw New Exception("User not authorized")
|
||||
End If
|
||||
|
||||
Dim oResult As New ContainerResult With {
|
||||
.Container = oContainer.GetFile()
|
||||
oContainer = FileContainer.Create(LogConfig, AppConfig.ContainerPassword)
|
||||
oContainerId = oContainer.ContainerId
|
||||
_logger.Debug("Container created with id {0}", oContainerId)
|
||||
|
||||
Dim oExtension As String = Path.GetExtension(FileName).Substring(1)
|
||||
_logger.Debug("File extension of file {0} is {1}", FileName, oExtension)
|
||||
|
||||
Dim oSQL = $"SELECT FNICM_NEW_DOC('{FileName}','{oExtension}','{oContainerId}','{GetContainerName(oContainerId)}','{_username}') FROM RDB$DATABASE;"
|
||||
Dim oDocId As Int64 = Database.GetScalarValue(oSQL)
|
||||
|
||||
If oDocId = -1 Then
|
||||
_logger.Warn("Database returned -1 while creating Document Entry. File was not saved!")
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
_logger.Debug("Database Entry created with DocId {0}", oDocId)
|
||||
|
||||
oContainer.SetFile(Contents, FileName)
|
||||
oContainer.SaveAs(GetContainerPath(oContainerId))
|
||||
|
||||
_logger.Debug("File saved in Container!", FileName)
|
||||
|
||||
Return New DocumentResult() With {
|
||||
.Document = New DocumentObject(oContainerId, oDocId, FileName)
|
||||
}
|
||||
|
||||
Return oResult
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return New ContainerResult(ex.Message)
|
||||
Return New DocumentResult(ex.Message)
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function CreateFile(Contents() As Byte, Extension As String) As String Implements IEDMService.CreateFile
|
||||
Public Function UpdateFile(DocObject As DocumentObject, Contents() As Byte) As DocumentResult Implements IEDMService.UpdateFile
|
||||
Try
|
||||
Dim oContainer As FileContainer = FileContainer.Create(LogConfig, AppConfig.ContainerPassword)
|
||||
Dim oContainerId As String = oContainer.FileId
|
||||
TestFileExists(DocObject.ContainerId)
|
||||
|
||||
oContainer.SetFile(Contents, Extension)
|
||||
oContainer.SaveAs(GetFilePath(oContainerId))
|
||||
' TODO: update db
|
||||
|
||||
Return oContainerId
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function UpdateFile(FileId As String, Contents() As Byte) As String Implements IEDMService.UpdateFile
|
||||
Try
|
||||
TestFileExists(FileId)
|
||||
|
||||
Dim oFilePath = GetFilePath(FileId)
|
||||
Dim oFilePath = GetContainerPath(DocObject.ContainerId)
|
||||
Dim oFileContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oFilePath)
|
||||
|
||||
oFileContainer.SetFile(Contents, oFileContainer.Extension)
|
||||
oFileContainer.SetFile(Contents, oFileContainer.GetFile.FileName)
|
||||
oFileContainer.Save()
|
||||
|
||||
Return oFileContainer.FileId
|
||||
Return New DocumentResult() With {
|
||||
.Document = DocObject
|
||||
}
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function DeleteFile(FileId As String) As Boolean Implements IEDMService.DeleteFile
|
||||
Public Function GetFile(DocObject As DocumentObject) As DocumentResult Implements IEDMService.GetFile
|
||||
Try
|
||||
TestFileExists(FileId)
|
||||
TestFileExists(DocObject.ContainerId)
|
||||
|
||||
Dim oFilePath = GetFilePath(FileId)
|
||||
Dim oContainerPath = GetContainerPath(DocObject.ContainerId)
|
||||
Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath)
|
||||
Dim oContents As Byte() = oContainer.GetFile().Contents
|
||||
|
||||
Return New DocumentResult With {
|
||||
.Document = DocObject,
|
||||
.Contents = oContents
|
||||
}
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return New DocumentResult(ex.Message)
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function DeleteFile(DocObject As DocumentObject) As Boolean Implements IEDMService.DeleteFile
|
||||
Try
|
||||
TestFileExists(DocObject.ContainerId)
|
||||
|
||||
Dim oFilePath = GetContainerPath(DocObject.ContainerId)
|
||||
IO.File.Delete(oFilePath)
|
||||
|
||||
'TODO: Delete doc from db
|
||||
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
|
||||
Private Function GetContainerPath(ContainerId As String) As String
|
||||
Return Path.Combine(AppConfig.ContainerPath, GetContainerName(ContainerId))
|
||||
End Function
|
||||
|
||||
Private Function GetContainerName(ContainerId As String) As String
|
||||
Return ContainerId & ".enc"
|
||||
End Function
|
||||
|
||||
Private Sub TestFileExists(ContainerId)
|
||||
Dim oContainerPath = GetContainerPath(ContainerId)
|
||||
|
||||
If Not IO.File.Exists(oContainerPath) Then
|
||||
Throw New FileNotFoundException("Container existiert nicht", oContainerPath)
|
||||
End If
|
||||
End Sub
|
||||
#End Region
|
||||
|
||||
#Region "Index"
|
||||
Public Function SetFileIndex(DocObject As DocumentObject, Syskey As String, Value As String) As Object Implements IEDMService.SetFileIndex
|
||||
Throw New NotImplementedException()
|
||||
End Function
|
||||
#End Region
|
||||
End Class
|
||||
@@ -26,18 +26,23 @@ Interface IEDMService
|
||||
Function ExecuteNonQuery(SQL As String) As NonQueryResult
|
||||
#End Region
|
||||
|
||||
#Region "FileContainer"
|
||||
#Region "Document"
|
||||
<OperationContract>
|
||||
Function CreateFile(Contents As Byte(), Extension As String) As String
|
||||
Function CreateFile(FileName As String, Contents As Byte()) As DocumentResult
|
||||
|
||||
<OperationContract>
|
||||
Function UpdateFile(ContainerId As String, Contents As Byte()) As String
|
||||
Function UpdateFile(DocObject As DocumentObject, Contents As Byte()) As DocumentResult
|
||||
|
||||
<OperationContract>
|
||||
Function GetFile(ContainerId As String) As ContainerResult
|
||||
Function GetFile(DocObject As DocumentObject) As DocumentResult
|
||||
|
||||
<OperationContract>
|
||||
Function DeleteFile(ContainerId As String) As Boolean
|
||||
Function DeleteFile(DocObject As DocumentObject) As Boolean
|
||||
#End Region
|
||||
|
||||
#Region "Index"
|
||||
<OperationContract>
|
||||
Function SetFileIndex(DocObject As DocumentObject, Syskey As String, Value As String)
|
||||
#End Region
|
||||
|
||||
End Interface
|
||||
@@ -4,6 +4,8 @@ Imports System.Configuration
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Database.Exceptions
|
||||
Imports System.ServiceModel.Channels
|
||||
Imports System.ServiceModel.Dispatcher
|
||||
|
||||
Public Class WindowsService
|
||||
Inherits ServiceBase
|
||||
|
||||
Reference in New Issue
Block a user