Spaces:
Running
Running
class PDFAnalyzer { | |
constructor() { | |
this.fileInput = document.getElementById('file-upload'); | |
this.loading = document.getElementById('loading'); | |
this.resultsContainer = document.getElementById('analysis-results'); | |
this.initializeEventListeners(); | |
} | |
initializeEventListeners() { | |
this.fileInput.addEventListener('change', (e) => this.handleFileChange(e)); | |
// Drag and drop handlers | |
const uploadArea = document.querySelector('.upload-area'); | |
uploadArea.addEventListener('dragover', (e) => { | |
e.preventDefault(); | |
uploadArea.style.borderColor = 'var(--primary-color)'; | |
}); | |
uploadArea.addEventListener('dragleave', () => { | |
uploadArea.style.borderColor = '#d1d5db'; | |
}); | |
uploadArea.addEventListener('drop', (e) => { | |
e.preventDefault(); | |
uploadArea.style.borderColor = '#d1d5db'; | |
const files = Array.from(e.dataTransfer.files); | |
this.analyzeFiles(files); | |
}); | |
} | |
async handleFileChange(e) { | |
const files = Array.from(e.target.files); | |
await this.analyzeFiles(files); | |
} | |
async analyzeFiles(files) { | |
this.loading.classList.remove('hidden'); | |
for (const file of files) { | |
try { | |
const analysis = await this.analyzePDF(file); | |
this.displayResult(analysis); | |
} catch (error) { | |
this.displayResult({ | |
name: file.name, | |
issues: [`Error analyzing file: ${error.message}`], | |
size: file.size, | |
timestamp: new Date().toISOString() | |
}); | |
} | |
} | |
this.loading.classList.add('hidden'); | |
} | |
async analyzePDF(file) { | |
return new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.onload = async (e) => { | |
const content = new Uint8Array(e.target.result); | |
const issues = []; | |
// Check file size (10MB limit) | |
if (file.size > 10000000) { | |
issues.push('File size exceeds 10MB'); | |
} | |
// Check PDF header | |
const header = content.slice(0, 4); | |
const isPDF = String.fromCharCode(...header) === '%PDF'; | |
if (!isPDF) { | |
issues.push('Invalid PDF format'); | |
} | |
resolve({ | |
name: file.name, | |
issues, | |
size: file.size, | |
timestamp: new Date().toISOString() | |
}); | |
}; | |
reader.onerror = () => reject(new Error('Failed to read file')); | |
reader.readAsArrayBuffer(file); | |
}); | |
} | |
displayResult(result) { | |
const alert = document.createElement('div'); | |
alert.className = `alert ${result.issues.length ? 'alert-error' : 'alert-success'}`; | |
alert.innerHTML = ` | |
<h3>${result.name}</h3> | |
<p>Size: ${(result.size / 1024).toFixed(2)} KB</p> | |
${result.issues.length ? ` | |
<div class="issues"> | |
<p>Issues found:</p> | |
<ul> | |
${result.issues.map(issue => `<li>${issue}</li>`).join('')} | |
</ul> | |
</div> | |
` : '<p>No issues found</p>'} | |
`; | |
this.resultsContainer.appendChild(alert); | |
} | |
} | |
// Initialize the analyzer when the DOM is loaded | |
document.addEventListener('DOMContentLoaded', () => { | |
new PDFAnalyzer(); | |
}); |