Common: Work on Watcher

This commit is contained in:
Jonathan Jenne
2022-01-21 15:50:29 +01:00
parent 27d33672f2
commit 408dacf1b5
6 changed files with 300 additions and 231 deletions

View File

@@ -21,12 +21,11 @@ Namespace DocumentResultList
''' </summary>
Public Property Contents As Byte()
' Optional properties
Public Property LastWriteTime As Date = Nothing
Public Property FullPath As String = Nothing
Public Property TempPath As String = Nothing
Public Property FileHash As String = Nothing
Public Sub New(pPrimaryKey As Long)
Id = pPrimaryKey

View File

@@ -55,11 +55,11 @@ Namespace DocumentResultList
Dim oDocumentInfo As Client.DocumentInfo = Client.GetDocumentInfo(User.UserId, pObjectId)
Dim oFileInfo As New FileInfo(oDocumentInfo.FullPath)
Dim oResultDocumentInfo As New Document(pObjectId) With {
.Contents = Load_FromDisk(oDocumentInfo.FullPath),
.AccessRight = oDocumentInfo.AccessRight,
.FullPath = oDocumentInfo.FullPath,
.Extension = oFileInfo.Extension.Substring(1)
}
.Contents = Load_FromDisk(oDocumentInfo.FullPath),
.AccessRight = oDocumentInfo.AccessRight,
.FullPath = oDocumentInfo.FullPath,
.Extension = oFileInfo.Extension.Substring(1)
}
Return oResultDocumentInfo
Catch ex As Exception
@@ -76,11 +76,12 @@ Namespace DocumentResultList
End If
Dim oResultDocumentInfo As New Document(pObjectId) With {
.Contents = oFileObject._FileContents,
.Extension = oFileObject._FileExtension,
.AccessRight = Rights.AccessRight.FULL,
.FullPath = Nothing
}
.Contents = oFileObject._FileContents,
.Extension = oFileObject._FileExtension,
.AccessRight = Rights.AccessRight.FULL,
.FileHash = oFileObject._FileHash,
.FullPath = Nothing
}
Return oResultDocumentInfo
End Function

View File

@@ -1,121 +0,0 @@
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow.Base
Imports DigitalData.Modules.Language.DateTimeEx
Imports System.IO
Imports System.Text
Imports System.Timers
Namespace DocumentResultList
Public Class Opener
Inherits BaseClass
Private WithEvents FileOpenTimer As New Timer
' TODO: Hashes for checking if the opened file was modified externally
Private HashOriginalFile As String = Nothing
Private HashOpenedFile As String = Nothing
Private OpenFiles As New Dictionary(Of Integer, Document)
Public Event ProcessEnded As EventHandler(Of Document)
Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig)
End Sub
Public Function OpenDocument(pDocument As Document) As Boolean
Dim oProcessId As Integer = Nothing
If pDocument.FullPath Is Nothing OrElse pDocument.FullPath.Trim = String.Empty Then
oProcessId = OpenFileFromPath(pDocument)
ElseIf pDocument.Extension IsNot Nothing AndAlso pDocument.Contents IsNot Nothing Then
oProcessId = OpenFileFromByteArry(pDocument)
End If
If IsNothing(oProcessId) Then
Logger.Warn("Process Id was empty. Returning false.")
Return False
End If
OpenFiles.Add(oProcessId, pDocument)
Return True
End Function
Private Function OpenFileFromByteArry(pDocument As Document) As Integer
Try
' TODO: Open file from temp folder
Dim oTempPath = Path.Combine(Path.GetTempPath(), Constants.TEMP_PATH_SUBFOLDER)
Dim oDirectory = Directory.CreateDirectory(oTempPath)
Dim oFileName = $"{pDocument.Id}-{Now.UnixTimestamp}.{pDocument.Extension}"
Dim oFilePath = Path.Combine(oTempPath, oFileName)
Using oMemoryStream As New MemoryStream(pDocument.Contents)
Using oStreamWriter As New StreamWriter(oFilePath, append:=False, Encoding.UTF8)
oMemoryStream.CopyTo(oMemoryStream)
End Using
End Using
Dim oProcess = OpenFile(oFilePath)
Return oProcess.Id
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function OpenFileFromPath(pDocument As Document) As Integer
Try
Dim oProcess = OpenFile(pDocument.FullPath)
Return oProcess.Id
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function OpenFile(pFilePath As String) As Process
Dim oProcess = Process.Start(New ProcessStartInfo With {
.FileName = pFilePath
})
Return oProcess
End Function
Private Sub FileOpenTimer_Elapsed() Handles FileOpenTimer.Elapsed
Try
Dim oIds = Process.GetProcesses().
Select(Function(process) process.Id).
ToList()
Dim oNewFileOpenList As New Dictionary(Of Integer, Document)
For Each oOpenFile In OpenFiles
If oIds.Contains(oOpenFile.Key) Then
oNewFileOpenList.Add(oOpenFile.Key, oOpenFile.Value)
End If
Next
If oNewFileOpenList.Count < OpenFiles.Count Then
Dim oClosedFiles = OpenFiles.
Except(oNewFileOpenList).
ToList()
If oClosedFiles.Count = 1 Then
Dim oOpenFile = oClosedFiles.First()
RaiseEvent ProcessEnded(Me, oOpenFile.Value)
End If
OpenFiles = oNewFileOpenList
End If
Catch ex As Exception
Logger.Error(ex)
End Try
End Sub
End Class
End Namespace

View File

@@ -0,0 +1,177 @@
Imports System.IO
Imports System.Text
Imports System.Timers
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow.Base
Imports DigitalData.Modules.Language.DateTimeEx
Namespace DocumentResultList
Public Class Watcher
Inherits BaseClass
Private WithEvents FileOpenTimer As New Timer
Private FileEx As Modules.Filesystem.File
' TODO: Hashes for checking if the opened file was modified externally
Private HashOriginalFile As String = Nothing
Private HashOpenedFile As String = Nothing
''' <summary>
''' List of opened files containing the filepath that was opened and the document id
''' </summary>
Private ReadOnly OpenFiles As New List(Of OpenFile)
Public Event FileChanged As EventHandler(Of FileChangedArgs)
Public Class OpenFile
Public Document As Document
Public ProcessId As Integer
Public FilePath As String
Public CurrentlyProcessing As Boolean = False
End Class
Public Class FileChangedArgs
Public File As OpenFile
End Class
Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig)
FileEx = New Modules.Filesystem.File(pLogConfig)
End Sub
Public Function OpenDocument(pDocument As Document) As Boolean
Dim oResult As Tuple(Of Process, String) = Nothing
If pDocument.FullPath IsNot Nothing OrElse pDocument.FullPath.Trim <> String.Empty Then
' TODO: DONT put into openfiles
oResult = OpenFileFromPath(pDocument)
ElseIf pDocument.Extension IsNot Nothing AndAlso pDocument.Contents IsNot Nothing Then
oResult = OpenFileFromByteArray(pDocument)
End If
If IsNothing(oResult) Then
Logger.Warn("Process Id was empty. File [{0}] could not be opened.", pDocument.Id)
Return False
End If
Dim oProcess = oResult.Item1
Dim oFilePath = oResult.Item2
Logger.Debug("File [{0}] opened with ProcessId [{1}]", oFilePath, oProcess.Id)
OpenFiles.Add(New OpenFile With {
.Document = pDocument,
.FilePath = oFilePath,
.ProcessId = oProcess.Id
})
Return True
End Function
Public Sub FileSaved(pOpenFile As OpenFile)
pOpenFile.CurrentlyProcessing = False
End Sub
Private Function OpenFileFromByteArray(pDocument As Document) As Tuple(Of Process, String)
Try
Dim oTempPath = Path.Combine(Path.GetTempPath(), Constants.TEMP_PATH_SUBFOLDER)
Dim oDirectory = Directory.CreateDirectory(oTempPath)
Dim oFileName = $"{pDocument.Id}-{Now.UnixTimestamp}.{pDocument.Extension}"
Dim oFilePath = Path.Combine(oTempPath, oFileName)
Using oMemoryStream As New MemoryStream(pDocument.Contents)
Using oStreamWriter As New StreamWriter(oFilePath, append:=False, Encoding.UTF8)
oMemoryStream.CopyTo(oMemoryStream)
End Using
End Using
Dim oProcess = DoOpenFile(oFilePath)
Return New Tuple(Of Process, String)(oProcess, oFilePath)
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function OpenFileFromPath(pDocument As Document) As Tuple(Of Process, String)
Try
Dim oProcess = DoOpenFile(pDocument.FullPath)
Return New Tuple(Of Process, String)(oProcess, pDocument.FullPath)
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function DoOpenFile(pFilePath As String) As Process
Dim oProcess = Process.Start(New ProcessStartInfo With {
.FileName = pFilePath
})
Return oProcess
End Function
Private Sub FileOpenTimer_Elapsed() Handles FileOpenTimer.Elapsed
Try
For Each oOpenFile In OpenFiles
If oOpenFile.CurrentlyProcessing = False Then
Continue For
End If
Dim oIsLocked = FileEx.TestFileIsLocked(oOpenFile.FilePath)
If oIsLocked Then
Continue For
End If
Dim oOldHash = oOpenFile.Document.FileHash
Dim oNewHash = FileEx.GetChecksum(oOpenFile.FilePath)
If oNewHash.Equals(oOldHash) = False Then
' File changed
RaiseEvent FileChanged(Me, New FileChangedArgs() With {.File = oOpenFile})
oOpenFile.CurrentlyProcessing = True
Else
OpenFiles.Remove(oOpenFile)
End If
Next
Catch ex As Exception
Logger.Error(ex)
End Try
'Try
' Dim oIds = Process.GetProcesses().
' Select(Function(process) process.Id).
' ToList()
' Dim oNewFileOpenList As New Dictionary(Of Integer, Document)
' For Each oOpenFile In OpenFiles
' If oIds.Contains(oOpenFile.Key) Then
' oNewFileOpenList.Add(oOpenFile.Key, oOpenFile.Value)
' End If
' Next
' If oNewFileOpenList.Count < OpenFiles.Count Then
' Dim oClosedFiles = OpenFiles.
' Except(oNewFileOpenList).
' ToList()
' If oClosedFiles.Count = 1 Then
' Dim oOpenFile = oClosedFiles.First()
' RaiseEvent ProcessEnded(Me, oOpenFile.Value)
' End If
' OpenFiles = oNewFileOpenList
' End If
'Catch ex As Exception
' Logger.Error(ex)
'End Try
End Sub
End Class
End Namespace