2023-12-18 16:33:31 +01:00

111 lines
4.4 KiB
VB.net

Imports System.Drawing
Imports System.IO
Imports DevExpress.Utils.Svg
Imports EnvelopeGenerator.Common.Constants
Public Class Helpers
''' <summary>
''' Encodes the EnvelopeUUID and the ReceiverSignature into an EnvelopeKey
''' </summary>
''' <param name="pEnvelopeUuid"></param>
''' <param name="pReceiverSignature"></param>
''' <returns>The EnvelopeKey</returns>
Public Shared Function EncodeEnvelopeReceiverId(pEnvelopeUuid As String, pReceiverSignature As String) As String
Dim oString = $"{pEnvelopeUuid}::{pReceiverSignature}"
Dim oBytes = System.Text.Encoding.UTF8.GetBytes(oString)
Dim oBase64String = Convert.ToBase64String(oBytes)
Return oBase64String
End Function
''' <summary>
''' Decodes the EnvelopeKey and returns the EnvelopeUUID and the ReceiverSignature
''' </summary>
''' <param name="pEnvelopeReceiverId">The EnvelopeKey</param>
''' <returns>A tuple containing EnvelopeUUID and Receiver Signature</returns>
Public Shared Function DecodeEnvelopeReceiverId(pEnvelopeReceiverId As String) As Tuple(Of String, String)
Dim oBytes = Convert.FromBase64String(pEnvelopeReceiverId)
Dim oString = System.Text.Encoding.UTF8.GetString(oBytes)
Dim oSplit = oString.Split(New String() {"::"}, StringSplitOptions.None)
Return New Tuple(Of String, String)(oSplit(0), oSplit(1))
End Function
Public Shared Function GetEnvelopeURL(pHost As String, pEnvelopeUuid As String, pReceiverSignature As String) As String
Dim oEnvelopeUserReference As String = EncodeEnvelopeReceiverId(pEnvelopeUuid, pReceiverSignature)
Dim oURL As String = String.Format("{0}/EnvelopeKey/{1}", pHost.Trim(), oEnvelopeUserReference)
Return oURL
End Function
Public Shared Function ColorTypeToColor(pColorType As ColorType) As Color
Select Case pColorType
Case ColorType.ReceiverColor1
Return ColorTranslator.FromHtml("#22c55e")
Case ColorType.ReceiverColor2
Return ColorTranslator.FromHtml("#3b82f6")
Case ColorType.ReceiverColor3
Return ColorTranslator.FromHtml("#8b5cf6")
Case ColorType.ReceiverColor4
Return ColorTranslator.FromHtml("#f59e0b")
Case ColorType.ReceiverColor5
Return ColorTranslator.FromHtml("#ef4444")
Case ColorType.ReceiverColor6
Return ColorTranslator.FromHtml("#14b8a6")
Case ColorType.ReceiverColor7
Return ColorTranslator.FromHtml("#d946ef")
Case ColorType.ReceiverColor8
Return ColorTranslator.FromHtml("#06b6d4")
Case ColorType.ReceiverColor9
Return ColorTranslator.FromHtml("#10b981")
Case ColorType.ReceiverColor10
Return ColorTranslator.FromHtml("#84cc16")
End Select
End Function
Public Shared Function GetColorCircle(pBaseImage As SvgImage, pColor As Color) As SvgImage
Dim oColoredImage As SvgImage = pBaseImage.
Clone(Sub(el As SvgElement, table As Hashtable)
If TypeOf el Is SvgCircle Then
el.Styles.Clear()
table("StyleName") = String.Empty
table("Fill") = ColorTranslator.ToHtml(pColor)
End If
End Sub)
Return oColoredImage
End Function
Public Shared Function IsValidEmailAddress(pEmailAddress As String) As Boolean
Try
Dim oAddress = New System.Net.Mail.MailAddress(pEmailAddress)
Return oAddress.Address = pEmailAddress
Catch ex As Exception
Return False
End Try
End Function
Public Shared Async Function CopyFileAsync(sourceFile As String, destinationFile As String) As Task
Dim fileOptions = IO.FileOptions.Asynchronous Or IO.FileOptions.SequentialScan
Dim bufferSize = 4096
Using sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, fileOptions)
Using destinationStream = New FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize, fileOptions)
Await sourceStream.CopyToAsync(destinationStream, bufferSize).ConfigureAwait(False)
End Using
End Using
End Function
End Class