Spaces:
Running
Running
class PDFComplianceAnalyzer { | |
constructor() { | |
this.fileInput = document.getElementById('file-upload'); | |
this.loadingElement = document.getElementById('loading'); | |
this.resultsContainer = document.getElementById('analysis-results'); | |
this.reportContainer = document.getElementById('compliance-report'); | |
this.reportContent = document.querySelector('.report-content'); | |
this.initializeEventListeners(); | |
} | |
initializeEventListeners() { | |
this.fileInput.addEventListener('change', (e) => this.handleFileChange(e)); | |
this.setupDragAndDrop(); | |
} | |
setupDragAndDrop() { | |
const uploadArea = document.querySelector('.upload-area'); | |
['dragenter', 'dragover'].forEach(eventName => { | |
uploadArea.addEventListener(eventName, (e) => { | |
e.preventDefault(); | |
uploadArea.classList.add('drag-over'); | |
}); | |
}); | |
['dragleave', 'drop'].forEach(eventName => { | |
uploadArea.addEventListener(eventName, (e) => { | |
e.preventDefault(); | |
uploadArea.classList.remove('drag-over'); | |
}); | |
}); | |
uploadArea.addEventListener('drop', (e) => { | |
const files = Array.from(e.dataTransfer.files); | |
if (files.length > 0 && files[0].type === 'application/pdf') { | |
this.analyzePDF(files[0]); | |
} else { | |
this.showError('Por favor, envie apenas arquivos PDF.'); | |
} | |
}); | |
} | |
handleFileChange(e) { | |
const file = e.target.files[0]; | |
if (file && file.type === 'application/pdf') { | |
this.analyzePDF(file); | |
} else { | |
this.showError('Por favor, selecione um arquivo PDF válido.'); | |
} | |
} | |
async analyzePDF(file) { | |
this.showLoading(true); | |
this.clearResults(); | |
try { | |
// Simulação da análise do PDF | |
await this.validatePDF(file); | |
const report = await this.generateComplianceReport(file); | |
this.displayResults(report); | |
} catch (error) { | |
this.showError(error.message); | |
} finally { | |
this.showLoading(false); | |
} | |
} | |
async validatePDF(file) { | |
return new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.onload = (e) => { | |
const content = new Uint8Array(e.target.result); | |
const header = content.slice(0, 4); | |
const isPDF = String.fromCharCode(...header) === '%PDF'; | |
if (!isPDF) { | |
reject(new Error('O arquivo não é um PDF válido.')); | |
} | |
resolve(true); | |
}; | |
reader.onerror = () => reject(new Error('Erro ao ler o arquivo.')); | |
reader.readAsArrayBuffer(file); | |
}); | |
} | |
async generateComplianceReport(file) { | |
// Simulação de análise de conformidade | |
await new Promise(resolve => setTimeout(resolve, 1500)); | |
return { | |
status: 'compliant', | |
confidence: 92.5, | |
details: { | |
format: 'Válido', | |
version: 'PDF 1.7', | |
pages: 1, | |
signature: 'Presente e válida', | |
metadata: 'Completo', | |
accessibility: 'Conforme' | |
}, | |
checks: [ | |
{ name: 'Estrutura do documento', status: 'pass', score: 100 }, | |
{ name: 'Metadados', status: 'pass', score: 95 }, | |
{ name: 'Assinatura digital', status: 'pass', score: 100 }, | |
{ name: 'Acessibilidade', status: 'pass', score: 85 } | |
] | |
}; | |
} | |
displayResults(report) { | |
this.reportContainer.classList.remove('hidden'); | |
const statusClass = report.status === 'compliant' ? 'alert-success' : 'alert-error'; | |
const statusText = report.status === 'compliant' ? 'Conforme' : 'Não conforme'; | |
this.reportContent.innerHTML = ` | |
<div class="alert ${statusClass}"> | |
<h3>Status: ${statusText}</h3> | |
<p>Nível de confiança: ${report.confidence}%</p> | |
</div> | |
<div class="report-section"> | |
<h3>Detalhes do Documento</h3> | |
${Object.entries(report.details).map(([key, value]) => ` | |
<div class="metric"> | |
<span>${key}:</span> | |
<span>${value}</span> | |
</div> | |
`).join('')} | |
</div> | |
<div class="report-section"> | |
<h3>Verificações de Conformidade</h3> | |
${report.checks.map(check => ` | |
<div class="metric"> | |
<span>${check.name}</span> | |
<span>${check.score}% - ${check.status === 'pass' ? 'Aprovado' : 'Reprovado'}</span> | |
</div> | |
`).join('')} | |
</div> | |
`; | |
} | |
showError(message) { | |
this.resultsContainer.innerHTML = ` | |
<div class="alert alert-error"> | |
<p>${message}</p> | |
</div> | |
`; | |
} | |
showLoading(show) { | |
if (show) { | |
this.loadingElement.classList.remove('hidden'); | |
} else { | |
this.loadingElement.classList.add('hidden'); | |
} | |
} | |
clearResults() { | |
this.resultsContainer.innerHTML = ''; | |
this.reportContainer.classList.add('hidden'); | |
this.reportContent.innerHTML = ''; | |
} | |
} | |
// Inicialização | |
document.addEventListener('DOMContentLoaded', () => { | |
new PDFComplianceAnalyzer(); | |
}); |