Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Language Imports System.IO Imports DigitalData.Services.EDMIService.Security Namespace Methods.IDB.GetFileObject Public Class GetFileObjectMethod Inherits BaseMethod Public Sub New(pLogConfig As LogConfig, pDatabaseIDB As MSSQLServer, pDatabaseECM As MSSQLServer, pGlobalState As GlobalState) MyBase.New(pLogConfig, pDatabaseIDB, pDatabaseECM, pGlobalState) End Sub Public Function Run(pData As GetFileObjectRequest) As GetFileObjectResponse Try If Helpers.TestObjectIdExists(pData.ObjectId) = False Then LogAndThrow("ObjectId does not exist!") End If Dim oSQL = $"SELECT * FROM VWIDB_FILE_OBJECT WHERE IDB_OBJ_ID = {pData.ObjectId}" Dim oTable = DatabaseIDB.GetDatatable(oSQL) If oTable Is Nothing OrElse oTable.Rows.Count = 0 Then LogAndThrow("Error while getting FileObject data!") End If Dim oRow As DataRow = oTable.First() Dim oFileHash As String = oRow.ItemEx("FILE_HASH", "") Dim oFileSize As Long = oRow.ItemEx(Of Long)("FILE_SIZE", 0) Dim oFileExtension As String = oRow.ItemEx(Of String)("EXTENSION") Dim oFilePath = oRow.ItemEx("RELPATH", "") Dim oFileName = oRow.ItemEx("Filename", "") Dim oFullFileName = Path.Combine(oFilePath, oFileName) Dim oFileObject As New FileObject With { .ObjectId = pData.ObjectId, .FileHash = oFileHash, .FileSize = oFileSize, .FileExtension = oFileExtension, .FilePath = oFullFileName } If pData.LoadFileContents = True Then If File.Exists(oFullFileName) = False Then Throw New FileNotFoundException("FileObject not Found!", oFullFileName) End If Dim oContents As Byte() = LoadFileContents(oFullFileName) If oContents Is Nothing Then Throw New FileNotFoundException("FileObject not Found!", oFullFileName) End If oFileObject.FileContents = oContents End If Return New GetFileObjectResponse(oFileObject) Catch ex As FileNotFoundException Logger.Error(ex) Return New GetFileObjectResponse(ex, ex.FileName) Catch ex As Exception Logger.Error(ex) Return New GetFileObjectResponse(ex) End Try End Function Private Function LoadFileContents(pFilePath As String) As Byte() Try Dim password = Environment.GetEnvironmentVariable("DD_FILE_ENCRYPTION_PASSWORD") If String.IsNullOrWhiteSpace(password) Then Logger.Warn("No encryption password set (DD_FILE_ENCRYPTION_PASSWORD). Attempting legacy plain read for file [{0}]", pFilePath) Return ReadPlain(pFilePath) End If Try ' Try decrypt first (preferred path) Logger.Debug("Attempting AES decrypt for file [{0}]", pFilePath) Return SecureFileHandler.DecryptFileToBytes(pFilePath, password) Catch exDec As Exception Logger.Warn("Decrypt failed for file [{0}]. Falling back to plain read. Reason: {1}", pFilePath, exDec.Message) Logger.Error(exDec) Return ReadPlain(pFilePath) End Try Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function Private Function LoadFileContents_Old(pFilePath As String) As Byte() Try Using oFileStream As New FileStream(pFilePath, FileMode.Open, FileAccess.Read) Using oMemoryStream As New MemoryStream() oFileStream.CopyTo(oMemoryStream) Dim oContents = oMemoryStream.ToArray() Return oContents End Using End Using Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function Private Function ReadPlain(pFilePath As String) As Byte() Using oFileStream As New FileStream(pFilePath, FileMode.Open, FileAccess.Read, FileShare.Read) Using oMemoryStream As New MemoryStream() oFileStream.CopyTo(oMemoryStream) Return oMemoryStream.ToArray() End Using End Using End Function End Class End Namespace