-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
212 lines (184 loc) · 5.88 KB
/
test.html
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Directus Image Gallery</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
padding: 20px 0;
}
.image-card {
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease;
}
.image-card:hover {
transform: translateY(-4px);
}
.image-card img {
width: 100%;
height: 200px;
object-fit: cover;
}
.image-info {
padding: 15px;
}
.image-info h3 {
margin: 0;
color: #333;
}
.image-date {
color: #666;
font-size: 0.9em;
margin-top: 8px;
}
.loading {
text-align: center;
padding: 40px;
font-size: 1.2em;
color: #666;
}
.error {
background: #fee;
color: #c00;
padding: 15px;
border-radius: 6px;
margin: 20px 0;
}
.auth-form {
max-width: 400px;
margin: 40px auto;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.auth-form input {
width: 100%;
padding: 8px;
margin: 8px 0;
border: 1px solid #ddd;
border-radius: 4px;
}
.auth-form button {
width: 100%;
padding: 10px;
background: #6644FF;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
}
.auth-form button:hover {
background: #5533EE;
}
</style>
</head>
<body>
<h1>Directus Image Gallery</h1>
<div id="authForm" class="auth-form">
<h2>Authentication Required</h2>
<input type="text" id="staticToken" placeholder="Enter your ADMIN_TOKEN">
<button onclick="setStaticToken()">Access Gallery</button>
</div>
<div id="gallery" class="gallery" style="display: none">
<div class="loading">Loading images...</div>
</div>
<script>
<!-- Change this URL. -->
const API_URL = 'https://directus-XXX-8055.prg1.zerops.app';
let accessToken = localStorage.getItem('directusToken');
// Check if we have a token and show appropriate view
if (accessToken) {
document.getElementById('authForm').style.display = 'none';
document.getElementById('gallery').style.display = 'grid';
initGallery();
}
function setStaticToken() {
const token = document.getElementById('staticToken').value;
if (token) {
accessToken = token;
localStorage.setItem('directusToken', accessToken);
document.getElementById('authForm').style.display = 'none';
document.getElementById('gallery').style.display = 'grid';
initGallery();
}
}
async function fetchImages() {
try {
const response = await fetch(`${API_URL}/items/images?sort=-date_created`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
if (response.status === 401 || response.status === 403) {
localStorage.removeItem('directusToken');
document.getElementById('authForm').style.display = 'block';
document.getElementById('gallery').style.display = 'none';
throw new Error('Authentication failed');
}
if (!response.ok) throw new Error('Failed to fetch images');
const data = await response.json();
return data.data;
} catch (error) {
console.error('Error fetching images:', error);
throw error;
}
}
function formatDate(dateString) {
return new Date(dateString).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
}
function createImageCard(image) {
return `
<div class="image-card">
<img src="${API_URL}/assets/${image.image}?access_token=${accessToken}" alt="${image.name || 'Untitled'}">
<div class="image-info">
<h3>${image.name || 'Untitled'}</h3>
<div class="image-date">${formatDate(image.date_created)}</div>
</div>
</div>
`;
}
async function initGallery() {
const galleryElement = document.getElementById('gallery');
try {
const images = await fetchImages();
if (images.length === 0) {
galleryElement.innerHTML = '<div class="loading">No images found</div>';
return;
}
galleryElement.innerHTML = images.map(createImageCard).join('');
} catch (error) {
if (error.message === 'Authentication failed') {
return; // Already handled in fetchImages
}
galleryElement.innerHTML = `
<div class="error">
Failed to load images. Please try again later.
</div>
`;
}
}
</script>
</body>
</html>