File size: 3,758 Bytes
f2c6292
 
 
 
 
 
 
c481e2a
 
f2c6292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c481e2a
f2c6292
 
 
 
c481e2a
f2c6292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c481e2a
f2c6292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c481e2a
f2c6292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c481e2a
 
f2c6292
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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();
});