-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
334 lines (309 loc) · 11.7 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
rel="stylesheet"
crossorigin="anonymous"
/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Upload Blobs</title>
<style>
dt {
opacity: 80%;
}
object {
object-fit: cover;
}
</style>
</head>
<body>
<script>
const SUI_NETWORK = "testnet";
const SUI_VIEW_TX_URL = `https://suiscan.xyz/${SUI_NETWORK}/tx`;
const SUI_VIEW_OBJECT_URL = `https://suiscan.xyz/${SUI_NETWORK}/object`;
/**
* Handler for submit events from the HTML form.
*
* Stores the blob provided in the form, and disable the default HTML form submission.
*/
function onSubmit(event) {
// Ensure that the event does not propagate, so that the browser does not
// perform a POST with the file.
event.preventDefault();
// Disable the form while uploading.
enableForm(false);
// Store and display the blob, then re-enable the form.
storeBlob()
.then((storageInfo) => {
displayUpload(storageInfo.info, storageInfo.media_type);
alert(null);
enableForm(true);
})
.catch((error) => {
console.error(error);
alert(
"An error occurred while uploading. Check the browser console and ensure that \
the aggregator and publisher URLs are correct."
);
enableForm(true);
});
// Return false to cancel form submission.
return false;
}
/**
* Stores the file from the HTML form on Walrus.
*/
function storeBlob() {
const inputFile = document.getElementById("file-input").files[0];
const numEpochs = document.getElementById("epochs-input").value;
const basePublisherUrl = document.getElementById(
"publisher-url-input"
).value;
// Submit a PUT request with the file's content as the body to the /v1/store endpoint.
return fetch(`${basePublisherUrl}/v1/store?epochs=${numEpochs}`, {
method: "PUT",
body: inputFile,
}).then((response) => {
if (response.status === 200) {
// Parse successful responses as JSON, and return it along with the
// mime type from the the file input element.
return response.json().then((info) => {
console.log(info);
return { info: info, media_type: inputFile.type };
});
} else {
throw new Error("Something went wrong when storing the blob!");
}
});
}
/**
* Display the result of uploading the file to Walrus.
*/
function displayUpload(storage_info, media_type) {
// Extract the displayed fields from either of the two successful responses:
// - newlyCreated for blobs that have been uploaded for the first time,
// or whose duration has been extended.
// - alreadyCertified for blobs that have already been uploaded and certified.
if ("alreadyCertified" in storage_info) {
info = {
status: "Already certified",
blobId: storage_info.alreadyCertified.blobId,
endEpoch: storage_info.alreadyCertified.endEpoch,
suiRefType: "Previous Sui Certified Event",
suiRef: storage_info.alreadyCertified.event.txDigest,
suiBaseUrl: SUI_VIEW_TX_URL,
};
} else if ("newlyCreated" in storage_info) {
info = {
status: "Newly created",
blobId: storage_info.newlyCreated.blobObject.blobId,
endEpoch: storage_info.newlyCreated.blobObject.storage.endEpoch,
suiRefType: "Associated Sui Object",
suiRef: storage_info.newlyCreated.blobObject.id,
suiBaseUrl: SUI_VIEW_OBJECT_URL,
};
} else {
throw Error("Unhandled successful response!");
}
// The URL used to download and view the blob.
const baseAggregatorUrl = document.getElementById(
"aggregator-url-input"
).value;
const blobUrl = `${baseAggregatorUrl}/v1/${info.blobId}`;
fetch(blobUrl, {
method: "GET",
}).then(async (data) => {
console.log(data);
});
// The URL for viewing the event or object on chain
const suiUrl = `${info.suiBaseUrl}/${info.suiRef}`;
const isImage = media_type.startsWith("image");
// Create the HTML entry in the page for the uploaded blob.
//
// For the associated icon, we use the `<object/>` HTML element, as it allows specifying
// the media type. The walrus aggregator returns blobs as `application/octect-stream`,
// so it's necessary to specify the content type to the browser in the `object` element.
document.getElementById("uploaded-blobs").insertAdjacentHTML(
"afterBegin",
`<article class="row border rounded-2 shadow-sm mb-3">
<object type="${isImage ? media_type : ""}" data="${
isImage ? blobUrl : ""
}"
class="col-4 ps-0"></object>
<dl class="blob-info col-8 my-2">
<dt>Status</dt><dd>${info.status}</dd>
<dt>Blob ID</dt>
<dd class="text-truncate"><a href="${blobUrl}">${
info.blobId
}</a></dd>
<dt>${info.suiRefType}</dt>
<dd class="text-truncate">
<a href="${suiUrl}" target="_blank">${
info.suiRef
}</a>
</dd>
<dt>Stored until epoch</dt><dd>${info.endEpoch}</dd>
</dl>
</article>`
);
}
/**
* Helper function to disable the form while uploading.
*/
function enableForm(isEnabled) {
if (isEnabled) {
// Copy the urls, so that they are not reset.
const publisherUrl = document.getElementById(
"publisher-url-input"
).value;
const aggregatorUrl = document.getElementById(
"aggregator-url-input"
).value;
// Reset the form
document.getElementById("upload-form").reset();
// Revert the URLs to their previous values
document.getElementById("publisher-url-input").value = publisherUrl;
document.getElementById("aggregator-url-input").value = aggregatorUrl;
// Disable the progress indication
document.getElementById("submit-spinner").style.visibility =
"collapse";
document.getElementById("submit-text").textContent = "Upload";
// Re-enable the form
document
.querySelector("#upload-form fieldset")
.removeAttribute("disabled");
} else {
document
.querySelector("#upload-form fieldset")
.setAttribute("disabled", "");
document.getElementById("submit-spinner").style.visibility =
"visible";
document.getElementById("submit-text").textContent = "Uploading ...";
}
}
/**
* Helper to display an error message.
*/
function alert(message) {
const alertElement = document.getElementById("alert");
if (message !== null) {
alertElement.textContent = message;
}
alertElement.style.visibility = message !== null ? "visible" : "hidden";
}
</script>
<header class="container my-3">
<hgroup>
<h1>Walrus Blob Upload</h1>
<p class="lead">
An example uploading and displaying files with Walrus.
</p>
</hgroup>
<img src="https://aggregator-devnet.walrus.space/v1/c_oN3jO2cv7woA71V8o2bFJfAjB-NYQxtCZnn-ES3Mc" alt="">
</header>
<main class="container">
<div class="row align-items-start gx-5">
<section class="col-lg-5 mb-3">
<hgroup>
<h2>Blob Upload</h2>
<p>
Upload blobs to Walrus, and display them on this page. See the
<a href="https://docs.walrus.site" target="_blank">
Walrus documentation
</a>
for more information. The file size is limited to 10 MiB on the
default publisher. Use the
<a
href="https://docs.walrus.site/usage/client-cli.html"
target="_blank"
>CLI tool</a
>
to store bigger files.
</p>
</hgroup>
<form id="upload-form" onsubmit="return onSubmit(event)" class="mb-3">
<fieldset class="row g-3">
<div class="col-lg-6">
<label for="publisher-url-input" class="form-label">
Walrus publisher URL
</label>
<input
id="publisher-url-input"
type="url"
class="form-control"
placeholder="https://publisher-devnet.walrus.space"
value="https://publisher-devnet.walrus.space"
required
/>
</div>
<div class="col-lg-6">
<label for="aggregator-url-input" class="form-label">
Walrus aggregator URL
</label>
<input
id="aggregator-url-input"
type="url"
class="form-control"
placeholder="https://aggregator-devnet.walrus.space"
value="https://aggregator-devnet.walrus.space"
required
/>
</div>
<div class="col-12">
<label for="file-input" class="form-label"
>Blob to upload (<strong>Max 10 MiB size</strong> on the
default publisher!)</label
>
<input
id="file-input"
type="file"
class="form-control"
required
/>
</div>
<div class="col-12">
<label for="epochs-input" class="form-label">Epochs</label>
<input
id="epochs-input"
type="number"
value="1"
min="1"
placeholder="Epochs"
class="form-control"
required
/>
<div class="form-text">
The number of Walrus epochs for which to store the blob.
</div>
</div>
<button id="submit" class="btn btn-primary">
<span
id="submit-spinner"
class="spinner-border spinner-border-sm"
aria-hidden="true"
style="visibility: collapse"
></span>
<span id="submit-text" role="status">Upload</span>
</button>
</fieldset>
</form>
<div
id="alert"
class="alert alert-danger"
role="alert"
style="visibility: hidden"
></div>
</section>
<section class="col-lg-7">
<h2>Uploaded Blobs</h2>
<div id="uploaded-blobs">
<!-- Container for info about uploaded blobs -->
</div>
</section>
</div>
</main>
</body>
</html>