Imports System.IO
Imports System.Security.Cryptography

Public Class FileEx
    ''' <summary>
    ''' Reads the file at `FilePath` and computes a SHA256 Hash from its contents
    ''' </summary>
    ''' <param name="pFilePath"></param>
    ''' <returns></returns>
    Public Shared Function GetChecksumFromFileContents(pFilePath As String) As String
        Try
            Using oFileStream = IO.File.OpenRead(pFilePath)
                Using oStream As New BufferedStream(oFileStream, 1200000)
                    Dim oChecksum() As Byte = SHA256.Create.ComputeHash(oStream)
                    Return BaseUtils.FormatHash(oChecksum)
                End Using
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Shared Function GetHashFromFileContents(pFilePath As String) As String
        Return GetChecksumFromFileContents(pFilePath)
    End Function

    ''' <summary>
    ''' Returns a Dictionary path in the form of [Base]\[Year]\[Month]\[Day]
    ''' </summary>
    ''' <param name="pBaseDirectory">The basedirectory</param>
    ''' <param name="pDate">The date to be used</param>
    ''' <returns>The final directory path</returns>
    Public Shared Function GetDateDirectory(pBaseDirectory As String, pDate As Date) As String
        Dim oDateDirectory = StringEx.GetDateString(pDate)
        Dim oFinalDirectory As String = Path.Combine(pBaseDirectory, oDateDirectory)
        Return oFinalDirectory
    End Function

    ''' <summary>
    ''' Returns a Dictionary path in the form of [Base]\[Year]\[Month]\[Day] based on the current date
    ''' </summary>
    ''' <param name="pBaseDirectory">The basedirectory</param>
    ''' <returns>The final directory path</returns>
    Public Shared Function GetDateDirectory(pBaseDirectory As String) As String
        Return GetDateDirectory(pBaseDirectory, Now)
    End Function

    ''' <summary>
    ''' Creates a Dictionary in the form of [Base]\[Year]\[Month]\[Day]
    ''' </summary>
    ''' <param name="pBaseDirectory">The basedirectory</param>
    ''' <param name="pDate">The date to be used</param>
    ''' <returns>The created path. If the directory cannot be created, Nothing.</returns>
    Public Shared Function CreateDateDirectory(pBaseDirectory As String, pDate As Date) As String
        Dim oDateDirectory = StringEx.GetDateString(pDate)
        Dim oFinalDirectory As String = Path.Combine(pBaseDirectory, oDateDirectory)

        If Directory.Exists(oFinalDirectory) = False Then
            Try
                Directory.CreateDirectory(oFinalDirectory)
            Catch ex As Exception
                Return Nothing
            End Try
        End If

        Return oFinalDirectory
    End Function

    ''' <summary>
    ''' Creates a Dictionary in the form of [Base]\[Year]\[Month]\[Day] based on the current date
    ''' </summary>
    ''' <param name="pBaseDirectory">The basedirectory</param>
    ''' <returns>The created path. If the directory cannot be created, Nothing.</returns>
    Public Shared Function CreateDateDirectory(pBaseDirectory As String) As String
        Return CreateDateDirectory(pBaseDirectory, Now)
    End Function

End Class
