Imports DigitalData.Modules.Base
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging


Public Class Helpers
    Private ReadOnly LogConfig As LogConfig
    Private ReadOnly Logger As Logger
    Private ReadOnly FileEx As FilesystemEx

    Public Sub New(pLogConfig As LogConfig)
        LogConfig = pLogConfig
        Logger = pLogConfig.GetLogger()
        FileEx = New FilesystemEx(pLogConfig)
    End Sub

    Public Function GetFileProperties(pFilePath As String, pDateImportedAt As Date) As FileProperties
        Try
            Using oFileStream As New IO.FileStream(pFilePath, IO.FileMode.Open, IO.FileAccess.Read)
                Using oMemoryStream As New IO.MemoryStream()
                    oFileStream.CopyTo(oMemoryStream)
                    Dim oContents = oMemoryStream.ToArray()

                    Dim oFileInfo As New IO.FileInfo(pFilePath)
                    Dim oExtension As String = oFileInfo.Extension

                    Dim oFileName As String = oFileInfo.Name
                    Dim oFileCreatedAt As Date = oFileInfo?.CreationTime
                    Dim oFileModifiedAt As Date = oFileInfo?.LastWriteTime
                    Dim oFileHash As String = FileEx.GetChecksum(oFileInfo.FullName)

                    Return New FileProperties With {
                            .FileName = oFileInfo.Name,
                            .FileCreatedAt = oFileCreatedAt,
                            .FileChangedAt = oFileModifiedAt,
                            .FileContents = oContents,
                            .FileImportedAt = pDateImportedAt,
                            .FileChecksum = oFileHash,
                            .FileInfoRaw = oFileInfo
                    }
                End Using
            End Using
        Catch ex As Exception
            Logger.Error(ex)
            Return Nothing
        End Try
    End Function
End Class

