-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
276 lines (264 loc) · 7.7 KB
/
index.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
const handleFileUpload = (selector, handler) => {
document.addEventListener("DOMContentLoaded", () => {
const element = document.querySelector(selector);
element.addEventListener("change", async (event) => {
try {
const logElement = document.querySelector("#log");
const files = event.currentTarget.files;
if (files.length) {
await handler(files[0]);
}
}catch (e) {
console.error(e);
}
})
})
}
// step 1
const getUploadUrl = async () => {
const dataRes = await fetch("get_upload_url");
if (!dataRes.ok) {
throw dataRes;
}
return await dataRes.json();
}
// step 2
const uploadFile = async (data, file) => {
const formData = new FormData();
formData.append("Content-Type", file.type);
Object.entries(data.fields).forEach(([k, v]) => {
formData.append(k, v);
});
formData.append("file", file); // must be the last one
const postRes = await fetch(data.url, {
method: "POST",
body: formData,
});
if (!postRes.ok) {
throw postRes;
}
};
// step 3
const updateAvatar = async (data) => {
const {key} = data;
const updateAvatarRes = await fetch("update_avatar", {
method: "POST",
body: JSON.stringify({key}),
});
if (!updateAvatarRes.ok) {
throw updateAvatarRes;
}
}
handleFileUpload("#post", async (file) => {
const data = await getUploadUrl(); // 1
await uploadFile(data, file); // 2
await updateAvatar(data); // 3
location.reload();
});
handleFileUpload("#post-noupdate", async (file) => {
const data = await getUploadUrl(); // 1
await uploadFile(data, file); // 2
// step 3 is missing
location.reload();
});
handleFileUpload("#post-doubleupload", async (file) => {
const data = await getUploadUrl(); // 1
await uploadFile(data, file); // 2
await updateAvatar(data); // 3
// upload again
await uploadFile(data, file); // 2
location.reload();
});
document.addEventListener("DOMContentLoaded", async () => {
// update users and the currently signed in user
const [users, username] = await Promise.all([
(async () => {
const usersRes = await fetch("users");
if (!usersRes.ok) {
throw usersRes;
}
return await usersRes.json();
})(),
(async () => {
const whoamiRes = await fetch("whoami");
if (!whoamiRes.ok) {
throw whoamiRes;
}
return (await whoamiRes.json()).username;
})(),
])
// users table
document.querySelector("#users").innerHTML = "";
users.map(({Username, Name}) => {
const userElement = document.querySelector("#user-template").content.cloneNode(true);
if (username === Username) {
userElement.firstElementChild.classList.add("current-user");
}
userElement.querySelector("img").src = `user/${Username}/avatar`;
userElement.querySelector("h3").textContent = Name;
[...userElement.querySelectorAll(".username")].forEach((e) => e.textContent = Username);
userElement.querySelector("a").addEventListener("click", async (e) => {
const loginRes = await fetch("login", {method: "PUT", headers: {"Content-Type": "application/json"}, body: JSON.stringify({username: Username})});
if (!loginRes.ok) {
throw loginRes;
}
location.reload();
e.preventDefault();
});
return userElement;
}).forEach((userElement) => document.querySelector("#users").appendChild(userElement));
if (username) {
// update the DOM that the user is signed in
[...document.querySelectorAll("*[data-loggedin-hide=true]")].forEach((e) => e.style.display = "none");
[...document.querySelectorAll("*[data-loggedin-show=true]")].forEach((e) => e.style.display = "block");
[...document.querySelectorAll("*[data-username-fill=true]")].forEach((e) => e.textContent = username);
}
});
</script>
<style>
.user-card {
display: inline-flex;
margin: 1rem;
border: 1px solid gray;
padding: 0.5rem;
position: relative;
}
.user-card img {
width: 100px;
height: 100px;
object-fit: contain;
margin-right: 1rem;
}
.user-card h3 {
margin-bottom: 0.2rem;
margin-top: 0;
}
.user-card .user-data {
display:flex;
flex-direction:column;
justify-content:space-between;
}
.user-card.current-user a {
display: none;
}
.user-card.current-user::before {
content: "Current user";
position: absolute;
left:0;
top:0;
background-color: rgba(144,238,144, 0.6);
padding: 5px;
}
*[data-loggedin-show=true]{
display: none;
}
.s3bucket {
border-collapse: collapse;
}
.s3bucket tbody tr td:first-child {
max-width: 120px;
text-overflow: ellipsis;
overflow: hidden;
}
.s3bucket tbody tr td:nth-child(5) {
max-width: 250px;
overflow: scroll;
}
.s3bucket tbody tr td:nth-child(6) {
max-width: 250px;
overflow: scroll;
}
.s3bucket tbody td {
padding-right: 20px;
padding-left: 20px;
padding-top: 10px;
padding-bottom: 10px;
}
.s3bucket tbody tr:nth-child(even) {
background-color: #eeeeee;
}
.ddb-table {
border-collapse: collapse;
}
.ddb-table tbody td {
padding-right: 20px;
padding-left: 20px;
padding-top: 10px;
padding-bottom: 10px;
}
.ddb-table tbody tr:nth-child(even) {
background-color: #eeeeee;
}
</style>
</head>
<body>
<h2>How to use</h2>
<p>This demo website simulates users logging in and changing their avatars</p>
<p>There are 3 users, each with a distinct image</p>
<p>First, log in as a user using the link in their user card</p>
<p>After signing in, use the form below to upload a new avatar image</p>
<p>You can also inspect the S3 bucket contents and the DynamoDB table at the bottom of the page. You can see that changing the avatar image makes sure no leftover objects are left in the bucket</p>
<p>To see what happens if the browser does not send the thirds request (which sets the new Avatar in the DynamoDB table and deletes the old file), use the second file input. You can see that a new object is created in the bucket that has Status=Pending tag. With a lifecycle config, S3 automatically cleans them up usually within 2-3 days. Come back later to observe this.</p>
<h2>Users</h2>
<div id="users">Loading users...</div>
<template id="user-template">
<div class="user-card">
<img src="">
<div class="user-data">
<div>
<h3></h3>
<small class="username"></small>
</div>
<a href="#">Sign in as <span class="username"/></a>
</div>
</div>
</template>
<h2>Update avatar</h2>
<p data-loggedin-hide="true">You are not logged in. Choose a user above and log in</p>
<div data-loggedin-show="true">
<p>Upload a new image for <span data-username-fill="true"/></p>
<p>It will reload the page when it finishes</p>
<input id="post" type="file"/>
<hr/>
<p>Want to try out what happens if the user does not send the 3rd request? Use this input:</p>
<p>It will reload the page when it finishes</p>
<input id="post-noupdate" type="file"/>
<hr/>
<p>Feeling extra evil? Try what happens if the user uploads the file again after the backend processed the old one</p>
<p>It will reload the page when it finishes</p>
<input id="post-doubleupload" type="file"/>
</div>
<h1>Debugging</h1>
<h2>Bucket contents</h2>
<table class="s3bucket">
<thead>
<tr>
<th>Key</th>
<th>Last modified</th>
<th>Size</th>
<th>Content Type</th>
<th>Metadata</th>
<th>Expiration</th>
</tr>
<tbody>$$BUCKET_CONTENTS$$</tbody>
</thead>
</table>
<h2>Database contents</h2>
<table class="ddb-table">
<thead>
<tr>
<th>Username</th>
<th>Name</th>
<th>Avatar</th>
</tr>
<tbody>$$TABLE_CONTENTS$$</tbody>
</thead>
</table>
</body>
</html>