EDMI: First version of Updating files by replacing them!

This commit is contained in:
Jonathan Jenne
2022-01-31 16:31:13 +01:00
parent ab10268c99
commit 17bbaac7a0
30 changed files with 581 additions and 241 deletions

View File

@@ -21,6 +21,11 @@ Namespace DocumentResultList
''' </summary>
Private ReadOnly OpenFiles As New List(Of OpenFile)
''' <summary>
''' List of files that changed and are processed by the client, ie. updated
''' </summary>
Private ReadOnly ProcessedFiles As New List(Of OpenFile)
Public Event FileChanged As EventHandler(Of FileChangedArgs)
Public Class OpenFile
@@ -39,15 +44,15 @@ Namespace DocumentResultList
FileEx = New Modules.Filesystem.File(pLogConfig)
End Sub
Public Function OpenDocument(pDocument As Document) As Boolean
Public Async Function OpenDocument(pDocument As Document) As Task(Of Boolean)
Dim oResult As Tuple(Of Process, String) = Nothing
If pDocument.FullPath IsNot Nothing OrElse pDocument.FullPath.Trim <> String.Empty Then
If pDocument.FullPath IsNot Nothing AndAlso 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)
oResult = Await OpenFileFromByteArray(pDocument)
End If
@@ -67,14 +72,21 @@ Namespace DocumentResultList
.ProcessId = oProcess.Id
})
If FileOpenTimer.Enabled = False Then
FileOpenTimer.Interval = 10000
FileOpenTimer.Start()
End If
Return True
End Function
Public Sub FileSaved(pOpenFile As OpenFile)
pOpenFile.CurrentlyProcessing = False
OpenFiles.Remove(pOpenFile)
ProcessedFiles.Remove(pOpenFile)
End Sub
Private Function OpenFileFromByteArray(pDocument As Document) As Tuple(Of Process, String)
Private Async Function OpenFileFromByteArray(pDocument As Document) As Task(Of Tuple(Of Process, String))
Try
Dim oTempPath = Path.Combine(Path.GetTempPath(), Constants.TEMP_PATH_SUBFOLDER)
Dim oDirectory = Directory.CreateDirectory(oTempPath)
@@ -82,8 +94,8 @@ Namespace DocumentResultList
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)
Using oFileStream As New FileStream(oFilePath, FileMode.Create, FileAccess.Write)
Await oMemoryStream.CopyToAsync(oFileStream)
End Using
End Using
@@ -119,14 +131,20 @@ Namespace DocumentResultList
Private Sub FileOpenTimer_Elapsed() Handles FileOpenTimer.Elapsed
Try
For Each oOpenFile In OpenFiles
' All files that are currently processe/updated on the outside,
' will not be checked again.
If oOpenFile.CurrentlyProcessing = False Then
Dim oFileIsProcessed = ProcessedFiles.Contains(oOpenFile)
Debug.WriteLine($"File is processed: [{oFileIsProcessed}]")
If oFileIsProcessed = True Then
Continue For
End If
' Check if the file is currently in use, and skip if it is.
Dim oIsLocked = FileEx.TestFileIsLocked(oOpenFile.FilePath)
Debug.WriteLine($"File is locked: [{oIsLocked}]")
If oIsLocked Then
Continue For
End If
@@ -134,53 +152,37 @@ Namespace DocumentResultList
' If this point is reached, we assume the file was closed again after opening it.
' ------
Debug.WriteLine($"File Closed")
' Compute the current hash of the file and compare it with the one
' in the database.
Dim oOldHash = oOpenFile.Document.FileHash
Dim oNewHash = FileEx.GetChecksum(oOpenFile.FilePath)
Debug.WriteLine($"Old Hash: [{oOldHash}]")
Debug.WriteLine($"New Hash: [{oNewHash}]")
' If the the file did not change, remove it from the watch list
If oNewHash.Equals(oOldHash) Then
ProcessedFiles.Remove(oOpenFile)
OpenFiles.Remove(oOpenFile)
Continue For
End If
' If this point is reached, we assume the file changed.
' ------
Debug.WriteLine($"File Changed")
' The File changed, so mark the file as being processed/updated
' and notify any listeners of the FileChanged event
oOpenFile.CurrentlyProcessing = True
' and notify any listeners of the FileChanged event
ProcessedFiles.Add(oOpenFile)
RaiseEvent FileChanged(Me, New FileChangedArgs() With {.File = oOpenFile})
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

View File

@@ -229,19 +229,28 @@ Public Class frmDocumentResultList
End If
Catch ex As Exception
Logger.Error(ex)
Show_CriticalError(ex.Message)
Show_CriticalError(ex)
Finally
Cursor = Cursors.Default
End Try
End Sub
Public Sub Watcher_FileChanged(sender As Object, e As DocumentResultList.Watcher.FileChangedArgs) Handles Watcher.FileChanged
Public Async Sub Watcher_FileChanged(sender As Object, e As DocumentResultList.Watcher.FileChangedArgs) Handles Watcher.FileChanged
Try
Await _IDBClient.UpdateFileAsync(e.File.Document.Id, e.File.FilePath, New Options.UpdateFileOptions With {
.CreateNewFileVersion = False,
.Language = Environment.User.Language,
.Username = Environment.User.UserName
})
Catch ex As Exception
Logger.Error(ex)
Show_CriticalError(ex)
Finally
e.File.CurrentlyProcessing = False
' Signal to the watcher that the file is no longer in use
Watcher.FileSaved(e.File)
End Try
End Sub
@@ -565,11 +574,12 @@ Public Class frmDocumentResultList
Close()
End Sub
Private Sub GridControl_DoubleClick(sender As Object, e As EventArgs) Handles GridControl1.DoubleClick, GridControl2.DoubleClick, GridControl3.DoubleClick
Private Async Sub GridControl_DoubleClick(sender As Object, e As EventArgs) Handles GridControl1.DoubleClick, GridControl2.DoubleClick, GridControl3.DoubleClick
If _CurrentDocument IsNot Nothing AndAlso _CurrentDocument.AccessRight > Rights.AccessRight.VIEW_ONLY Then
Process.Start(New ProcessStartInfo With {
.FileName = _CurrentDocument.FullPath
})
'Process.Start(New ProcessStartInfo With {
' .FileName = _CurrentDocument.FullPath
'})
Await Watcher.OpenDocument(_CurrentDocument)
End If
End Sub
@@ -578,6 +588,11 @@ Public Class frmDocumentResultList
labelCriticalError.Caption = Message
End Sub
Public Sub Show_CriticalError(pException As Exception)
labelCriticalError.Visibility = BarItemVisibility.Always
labelCriticalError.Caption = pException.Message
End Sub
Public Sub Show_Warning(Message As String)
labelWarning.Visibility = BarItemVisibility.Always
labelWarning.Caption = Message
@@ -701,15 +716,17 @@ Public Class frmDocumentResultList
oPropertyDialog.Show()
End Sub
Private Sub MenuItemFileOpen_ItemClick(sender As Object, e As ItemClickEventArgs) Handles MenuItemFileOpen.ItemClick
Private Async Sub MenuItemFileOpen_ItemClick(sender As Object, e As ItemClickEventArgs) Handles MenuItemFileOpen.ItemClick
If TestPathExists(OPEN_FILE) = False Then
Exit Sub
End If
Try
Process.Start(New ProcessStartInfo With {
.FileName = _CurrentDocument.FullPath
})
Await Watcher.OpenDocument(_CurrentDocument)
'Process.Start(New ProcessStartInfo With {
' .FileName = _CurrentDocument.FullPath
'})
Catch ex As Exception
Logger.Error(ex)
End Try