| 39 | ]; |
| 40 | |
| 41 | async function inspectImageFile() { |
| 42 | let fileBytes = null; |
| 43 | let fileTypeConstant = null; |
| 44 | try { |
| 45 | // Load Image |
| 46 | fileTypeConstant = |
| 47 | ['image/jpeg', 'image/bmp', 'image/png', 'image/svg'].indexOf( |
| 48 | mime.getType(imagePath) |
| 49 | ) + 1; |
| 50 | fileBytes = Buffer.from(fs.readFileSync(imagePath)).toString('base64'); |
| 51 | } catch (error) { |
| 52 | console.log(error); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Specify item to inspect |
| 57 | const item = { |
| 58 | byteItem: { |
| 59 | type: fileTypeConstant, |
| 60 | data: fileBytes, |
| 61 | }, |
| 62 | }; |
| 63 | |
| 64 | // Specify inspect configuration to match information with mentioned infotypes. |
| 65 | const inspectConfig = { |
| 66 | infoTypes: infoTypes, |
| 67 | includeQuote: true, |
| 68 | }; |
| 69 | |
| 70 | // Combine configurations into a request for the service. |
| 71 | const request = { |
| 72 | parent: `projects/${projectId}/locations/global`, |
| 73 | inspectConfig: inspectConfig, |
| 74 | item: item, |
| 75 | }; |
| 76 | |
| 77 | // Use the client to send the request. |
| 78 | const [response] = await dlp.inspectContent(request); |
| 79 | |
| 80 | // Print Findings |
| 81 | const findings = response.result.findings; |
| 82 | if (findings.length > 0) { |
| 83 | console.log(`Findings: ${findings.length}\n`); |
| 84 | findings.forEach(finding => { |
| 85 | console.log(`InfoType: ${finding.infoType.name}`); |
| 86 | console.log(`\tQuote: ${finding.quote}`); |
| 87 | console.log(`\tLikelihood: ${finding.likelihood} \n`); |
| 88 | }); |
| 89 | } else { |
| 90 | console.log('No findings.'); |
| 91 | } |
| 92 | } |
| 93 | inspectImageFile(); |
| 94 | // [END dlp_inspect_image_file] |
| 95 | } |