File size: 4,391 Bytes
7e1e2b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html>
<head>
    <title>Protected File Download</title>
    <style>
        .hidden {
            display: none;
        }
        .container {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        .error {
            color: red;
            margin-top: 10px;
        }
        .success {
            color: green;
            margin-top: 10px;
        }
        button, input, select {
            margin: 10px 0;
            padding: 8px;
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="passwordSection">
            <h2>Enter Password</h2>
            <input type="password" id="password" placeholder="Enter password">
            <button onclick="checkPassword()">Submit</button>
            <div id="passwordError" class="error hidden"></div>
        </div>

        <div id="itemSection" class="hidden">
            <h2>Select Item</h2>
            <select id="itemSelect">
                <option value="">Choose an item...</option>
            </select>
            <button id="downloadButton" class="hidden" onclick="downloadItem()">Download</button>
        </div>
    </div>

    <script>
        // Add event listener for Enter key
        document.getElementById('password').addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                event.preventDefault();
                checkPassword();
            }
        });

        async function checkPassword() {
            const password = document.getElementById('password').value;
            // sleep for 2 seconds
            await new Promise(resolve => setTimeout(resolve, 2000)); 
            try {
                const response = await fetch('/api/verify-password', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ password: password })
                });
                
                if (response.ok) {
                    const items = await response.json();
                    showItemSection(items);
                } else {
                    document.getElementById('passwordError').textContent = 'Invalid password';
                    document.getElementById('passwordError').classList.remove('hidden');
                }
            } catch (error) {
                console.error('Error:', error);
                document.getElementById('passwordError').textContent = 'Error connecting to server';
                document.getElementById('passwordError').classList.remove('hidden');
            }
        }

        function showItemSection(items) {
            document.getElementById('passwordSection').classList.add('hidden');
            document.getElementById('itemSection').classList.remove('hidden');
            
            const select = document.getElementById('itemSelect');
            items.forEach(item => {
                const option = document.createElement('option');
                option.value = item;
                option.textContent = item;
                select.appendChild(option);
            });

            select.addEventListener('change', function() {
                const downloadButton = document.getElementById('downloadButton');
                downloadButton.classList.toggle('hidden', !this.value);
            });
        }

        async function downloadItem() {
            const item = document.getElementById('itemSelect').value;
            try {
                const response = await fetch(`/api/download/${item}`);
                if (!response.ok) throw new Error('Download failed');
                
                const blob = await response.blob();
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = item;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
            } catch (error) {
                console.error('Error:', error);
                alert('Error downloading file');
            }
        }
    </script>
</body>
</html>