1 2// Check for the various File API support. 3if (window.File && window.FileReader && window.FileList && window.Blob) { 4 // Success! All the File APIs are supported. 5} else { 6 alert('Please use a web browser that supports HTML5 file APIs.') 7} 8 9function formatCredentialTextForHeader(credentialText) { 10 // Replace any CR/LF pairs with a newline character. 11 credentialText = credentialText.replace(/\r\n/g, '\n') 12 13 // Add line endings for C-language variable declaration. 14 credentialText = credentialText.replace(/\n/g, '\\n" \\\n "') 15 16 // Remove '\n"' from the last line of the declaration and add a semicolon. 17 credentialText = credentialText.slice(0, -9) + '"\n' 18 return credentialText 19} 20 21function generateCertificateConfigurationHeader() { 22 var pemCertificateText = '' 23 var pemPrivateKeyText = '' 24 var filename = 'demo_config.h' 25 var outputText = '' 26 27 var readerCertificate = new FileReader() 28 var readerPrivateKey = new FileReader() 29 30 // Start certificate read 31 readerCertificate.readAsText(pemInputFileCertificate.files[0]) 32 33 // Define a handler to create appropriate client certificate file text. 34 readerCertificate.onload = function (e) { 35 pemCertificateText = e.target.result 36 37 // Add C-language variable declaration plus EOL formatting. 38 pemCertificateText = ' "' + formatCredentialTextForHeader(pemCertificateText) 39 40 // Because this is async, read next file inline. 41 readerPrivateKey.readAsText(pemInputFilePrivateKey.files[0]) 42 } 43 44 // Define a handler to create appropriate private key file text. 45 readerPrivateKey.onload = function (e) { 46 pemPrivateKeyText = e.target.result 47 48 // Add C-language variable declaration plus EOL formatting. 49 pemPrivateKeyText = ' "' + formatCredentialTextForHeader(pemPrivateKeyText) 50 51 outputText = awsIotProfileTemplate 52 outputText = outputText.replace('<IOTEndpoint>', '"' + document.getElementById('AWSEndpoint').value + '"') 53 outputText = outputText.replace('<IOTThingName>', '"' + document.getElementById('thingName').value + '"') 54 outputText = outputText.replace('<ClientCertificatePEM>', pemCertificateText) 55 outputText = outputText.replace('<ClientPrivateKeyPEM>', pemPrivateKeyText) 56 57 // Because this is async, handle download generation inline. 58 var downloadBlob = new Blob([outputText], { type: 'text/plain' }) 59 if (window.navigator.msSaveOrOpenBlob) { 60 window.navigator.msSaveBlob(downloadBlob, filename) 61 } else { 62 var downloadLink = document.createElement('a') 63 downloadLink.href = window.URL.createObjectURL(downloadBlob) 64 downloadLink.download = filename 65 document.body.appendChild(downloadLink) 66 downloadLink.click() 67 document.body.removeChild(downloadLink) 68 } 69 } 70} 71