VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ArmGraphMailAttachment"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit



Public URL As String
Public Id As String
Public Name As String
Public ContentType As String
Public IsInline As Boolean

Public LocalPath As String

Public Sub Load_A_COM()

End Sub

Public Function Save(as_Path As String) As Boolean
    Save = False
    
    Dim ls_bin_or_json As Variant
  
    Dim FileData() As Byte
    
    ' get content
    If Not GetWebService(URL & "/$value?" & GetRND, ls_bin_or_json) Then
        Exit Function
    End If
    
    'delete the file
    If FileExists(as_Path) Then
        Call Kill(as_Path)
    End If
    
    ' the returned content is a RAW data
    FileData = ls_bin_or_json
    
    Dim fnum As Long
    fnum = FreeFile
    
    Open as_Path For Binary As #fnum
    Put #fnum, , FileData
    Close #fnum

    Save = True
End Function

Public Function ToJSON() As String

    Dim contentString As String
    contentString = ""
    
    ' load file
    If FileExists(LocalPath) Then
        Dim binFile() As Byte
        binFile = ReadFile(LocalPath)
        
        contentString = encodeBase64(binFile)
    End If
    
    Dim ls_json As String
        ls_json = "{" & _
                  """@odata.type"": ""#microsoft.graph.fileAttachment""," & _
                  """name"":""" & Name & """," & _
                  """contentBytes"":""" & contentString & """" & _
                  "}"
    ToJSON = ls_json
End Function

Private Function FileExists(ByVal Fname As String) As Boolean
        FileExists = IIf(Dir(Fname) <> "", True, False)
End Function

Private Function ReadFile(sFile As String) As Byte()
    Dim nFile       As Integer

    nFile = FreeFile
    Open sFile For Binary Access Read As #nFile
    If LOF(nFile) > 0 Then
        ReDim ReadFile(0 To LOF(nFile) - 1)
        Get nFile, , ReadFile
    End If
    Close #nFile
End Function

Private Function encodeBase64(ByRef arrData() As Byte) As String
On Error GoTo EmptyArray
    Dim length As Long
    
    length = UBound(arrData)
    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
    
    Set objXML = New MSXML2.DOMDocument
    
    Set objNode = objXML.createElement("b64")
    objNode.dataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    encodeBase64 = objNode.Text
 
    Set objNode = Nothing
    Set objXML = Nothing
    Exit Function
EmptyArray:
End Function
 
 
Private Function decodeBase64(ByVal strData As String) As Byte()
    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
    
    Set objXML = New MSXML2.DOMDocument
    Set objNode = objXML.createElement("b64")
    objNode.dataType = "bin.base64"
    objNode.Text = strData
    decodeBase64 = objNode.nodeTypedValue
    
    Set objNode = Nothing
    Set objXML = Nothing
End Function
