forked from syvb/upload2ipfs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
378 lines (346 loc) · 16.1 KB
/
Copy pathindex.html
File metadata and controls
378 lines (346 loc) · 16.1 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<title>Upload to IPFS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Upload files to the IPFS network using this simple tool.">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>
<script type="text/javascript">
function addOptionIfNotExists(selectElem, value, text) {
var exists = Array.from(selectElem.options).some(option => option.value === value);
if (!exists) {
var option = document.createElement("option");
option.text = text;
option.value = value;
selectElem.add(option);
}
}
window.addEventListener("load", function () {
// Only show the modal if the user hasn't accepted the terms before
if (!localStorage.getItem('termsAccepted')) {
var warningModal = new bootstrap.Modal(document.getElementById('warningModal'), {
backdrop: 'static', // Disable clicking outside the modal to close it
keyboard: false // Disable using the keyboard to close the modal
});
warningModal.show();
document.getElementById('acceptBtn').addEventListener('click', function () {
localStorage.setItem('termsAccepted', 'true'); // Set a flag when accepted
warningModal.hide();
});
document.getElementById('declineBtn').addEventListener('click', function () {
window.location.href = 'https://google.com'; // Redirect if declined
});
}
var gatewayMatchingList = {
"https://ipfs.network.thegraph.com/ipfs/api/v0/cat?arg=": "https://ipfs.network.thegraph.com/ipfs/api/v0/add?pin=true",
"https://ipfs.gitcoin.co/api/v0/cat/": "https://ipfs.gitcoin.co/api/v0/add?pin=true",
"https://ipfs-2.gitcoin.co/api/v0/cat/": "https://ipfs-2.gitcoin.co/api/v0/add?pin=true",
"https://ipfs-3.gitcoin.co/api/v0/cat/": "https://ipfs-3.gitcoin.co/api/v0/add?pin=true",
"https://ipfs.treejer.com/ipfs/": "https://ipfs.treejer.com/api/v0/add?pin=true",
"https://ipfs.oversas.org/ipfs/": "https://ipfs.oversas.org/api/v0/add?pin=true",
"https://api.thegraph.com/ipfs/api/v0/cat?arg=": "https://api.thegraph.com/ipfs/api/v0/add?pin=true",
"https://ipfs.questbook.app:8080/api/v0/cat?arg=": "https://ipfs.questbook.app/api/v0/add?pin=true",
"https://gw-seattle.crustcloud.io/ipfs/": "https://gw-seattle.crustcloud.io:443/api/v0/add",
"https://magic.decentralized-content.com/ipfs/": "https://ipfs-uploader.zora.co/api/v0/add",
"https://ipfs.decentralized-content.com/ipfs/": "https://ipfs-uploader.zora.co/api/v0/add",
};
var downloadGateways = [
"https://magic.decentralized-content.com/ipfs/",
"https://ipfs.decentralized-content.com/ipfs/",
"https://ipfs.network.thegraph.com/ipfs/api/v0/cat?arg=",
"https://api.thegraph.com/ipfs/api/v0/cat?arg=",
"https://ipfs.gitcoin.co/api/v0/cat/",
"https://ipfs-2.gitcoin.co/api/v0/cat/",
"https://ipfs-3.gitcoin.co/api/v0/cat/",
"https://gateway.ipfs.io/ipfs/",
"https://gateway.pinata.cloud/ipfs/",
"https://ipfs.io/ipfs/",
"https://ipfs.questbook.app:8080/api/v0/cat?arg=",
"https://test.cf-ipfs.com/ipfs/",
"https://lb-test.staging.cloudflare-ipfs.com/ipfs/",
"https://chat.ipfs.io/api/v0/cat/",
"https://gateway-int.ipfs.io/api/v0/cat/",
"https://cloudflare-ipfs.com/ipfs/",
"https://ipfs.staging.cloudflare-ipfs.com/ipfs/",
"https://ipfs.eth.aragon.network/ipfs/",
"https://ipfs.treejer.com/ipfs/",
"https://ipfs.stardustxr.org/ipfs/",
"https://ipfs.oversas.org/ipfs/",
"https://ipfs.web3.party/ipfs/",
"https://gw-seattle.crustcloud.io/ipfs/",
"https://ipfs.dlux.io/ipfs/",
"https://cdn.ipfsscan.io/ipfs/",
"https://ipfs.flock.io/ipfs/",
"https://ipfs.omniflix.studio/ipfs/",
"https://ipfs-gateway.omniflix.studio/ipfs/",
];
var uploadGateways = [
"https://ipfs-uploader.zora.co/api/v0/add",
"https://upload.ipfs.zora.co/api/v0/add",
"https://ipfs.effect.ai/api/v0/add?pin=true",
"https://unauthipfs.subquery.network/ipfs/api/v0/add?pin=true",
"https://ipfs.gitcoin.co/api/v0/add?pin=true",
"https://ipfs-2.gitcoin.co/api/v0/add?pin=true",
"https://ipfs-3.gitcoin.co/api/v0/add?pin=true",
"https://demo.storj-ipfs.com/api/v0/add",
"https://ipfs.treejer.com/api/v0/add?pin=true",
"https://ipfs.oversas.org/api/v0/add?pin=true",
"https://api.thegraph.com/ipfs/api/v0/add?pin=true",
"https://ipfs.network.thegraph.com/ipfs/api/v0/add?pin=true",
"https://ipfs.questbook.app/api/v0/add?pin=true",
"https://gw-seattle.crustcloud.io:443/api/v0/add"
];
var fileHash = "bafybeifx7yeb55armcsxwwitkymga5xf53dxiarykms3ygqic223w5sk3m";
var selectElem = document.getElementById("gateway-select");
var uploadSelectElem = document.getElementById("upload-gateway-select");
var downloadCounter = document.getElementById("download-count");
var uploadCounter = document.getElementById("upload-count");
var downloadSelectElem = document.getElementById("gateway-select");
var uploadSelectElem = document.getElementById("upload-gateway-select");
// Populate the select elements with gateways
downloadGateways.forEach(gateway => addOptionIfNotExists(downloadSelectElem, gateway, gateway));
uploadGateways.forEach(gateway => addOptionIfNotExists(uploadSelectElem, gateway, gateway));
// Populate the download gateway select element
downloadGateways.forEach(gateway => {
addOptionIfNotExists(selectElem, gateway, gateway);
});
// Populate the upload gateway select element
uploadGateways.forEach(gateway => {
addOptionIfNotExists(uploadSelectElem, gateway, gateway);
});
document.getElementById("file").addEventListener("change", function () {
if (this.files.length > 0) {
document.getElementById("submit").style.display = 'block';
} else {
document.getElementById("submit").style.display = 'none';
}
});
// Functionality to check gateway status
document.getElementById("check-download-gateways").addEventListener("click", () => checkGateways(downloadGateways, selectElem, fileHash));
document.getElementById("check-upload-gateways").addEventListener("click", () => checkGateways(uploadGateways, uploadSelectElem, fileHash, true));
selectElem.onchange = () => {
var matchingUploadGateway = gatewayMatchingList[selectElem.value];
if (matchingUploadGateway) {
uploadSelectElem.value = matchingUploadGateway;
}
};
// Form submission handling
document.getElementById("submit").addEventListener("click", submitForm);
});
function updateProgressBar(progressBarId, workingCount, totalGateways) {
const percentage = Math.round((workingCount / totalGateways) * 100);
const progressBar = document.getElementById(progressBarId);
progressBar.style.width = `${percentage}%`;
progressBar.setAttribute('aria-valuenow', percentage);
progressBar.innerText = `${workingCount}/${totalGateways} working`; // Updated text display
}
function checkGateways(gateways, selectElem, fileHash, isUpload = false) {
selectElem.innerHTML = ""; // Clear existing options
let workingCount = 0;
let statusElementId = isUpload ? "upload-progress" : "download-progress";
gateways.forEach(gateway => {
const url = isUpload ? gateway : `${gateway}${fileHash}`;
const method = isUpload ? "POST" : "HEAD";
const body = isUpload ? new FormData() : null;
if (isUpload) {
const blob = new Blob(["Hello, world!"], { type: 'text/plain' });
body.append('file', blob, 'hello.txt');
}
const fetchPromise = fetch(url, { method, body });
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Request timed out')), 3000);
});
Promise.race([fetchPromise, timeoutPromise])
.then(response => {
if (response.ok) {
addOptionIfNotExists(selectElem, gateway, gateway);
workingCount++;
} else {
console.error(`Error from ${gateway}: ${response.status} ${response.statusText}`);
response.text().then(text => console.error(text));
}
updateProgressBar(statusElementId, workingCount, gateways.length);
})
.catch(error => {
console.error(`Network or timeout error on ${gateway}: ${error.message}`);
updateProgressBar(statusElementId, workingCount, gateways.length);
});
});
}
function submitForm(e) {
e.preventDefault();
var files = document.getElementById("file").files;
if (files.length === 0) return;
var uploadSelectElem = document.getElementById("upload-gateway-select");
var fd = new FormData();
Array.from(files).forEach(file => fd.append("file", file));
document.getElementById("loading").style.display = "block";
document.getElementById("submit").disabled = true;
var req = new XMLHttpRequest();
req.onerror = () => handleError();
req.onload = () => handleSuccess(req);
req.open("POST", uploadSelectElem.value);
req.send(fd);
}
async function generateCID(file) {
const ipfs = await Ipfs.create();
const { cid } = await ipfs.add(file);
await ipfs.stop();
return cid.toString();
}
async function handleSuccess(req) {
var lines = req.responseText.split("\n");
lines = lines.filter(line => line.trim() !== ""); // Remove empty lines
var json = lines.map(line => {
try {
return JSON.parse(line);
} catch (e) {
console.error("Error parsing JSON:", e);
return null;
}
}).filter(item => item !== null);
var selectElem = document.getElementById("gateway-select");
await updateUIAfterUpload(json, selectElem);
}
function handleError() {
alert("There was an error uploading the file.");
document.getElementById("loading").style.display = "none";
document.getElementById("submit").disabled = false;
}
async function updateUIAfterUpload(json, selectElem) {
var hashesList = document.getElementById("hashes");
const fileInput = document.getElementById("file");
const files = fileInput.files;
for (const [index, obj] of json.entries()) {
var li = document.createElement("li");
var link = document.createElement("a");
link.setAttribute("target", "_blank");
// Handle both response formats
var hash = obj.Hash || obj.cid;
var name = obj.Name || obj.name;
var size = obj.Size || obj.size;
if (!hash || !name) {
const file = files[index];
hash = await generateCID(file);
name = file.name;
}
var paramConnector = selectElem.value.includes("?") ? "&" : "?";
var urlWithFilename = selectElem.value + hash + paramConnector + "filename=" + encodeURIComponent(name);
link.setAttribute("href", urlWithFilename);
link.innerText = `${hash} (${name}) - Size: ${size} bytes`;
li.appendChild(link);
hashesList.appendChild(li);
}
document.getElementById("loading").style.display = "none";
document.getElementById("submit").disabled = false;
hashesList.style.display = "block";
document.getElementById("pin-code").innerText = json.map(obj => `ipfs pin add ${obj.Hash || obj.cid}`).join("\n");
document.getElementById("pin-div").style.display = "block";
}
</script>
<style>
body {
font-family: 'Montserrat', sans-serif;
background-color: #2c3e50;
/* Dark grayish blue */
color: #bdc3c7;
/* Light silver */
}
h1,
h2 {
margin-top: 0.33em;
margin-bottom: 0.5em;
text-align: center;
}
.alert {
border: 3px dashed gold;
}
footer {
text-align: center;
padding: 1em 0;
}
</style>
</head>
<body>
<!-- Warning Modal -->
<div class="modal fade" id="warningModal" tabindex="-1" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalLabel">Terms of Use</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Use for educational purposes only. Do not upload sensitive files, as it is possible for the gateway owner to
view the files you upload, and it is not possible to remove the uploaded files using this tool.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="acceptBtn">Accept</button>
<button type="button" class="btn btn-danger" id="declineBtn">Decline</button>
</div>
</div>
</div>
</div>
<div class="container py-5">
<h1>Upload files to IPFS</h1>
<div class="row">
<div class="col-md-6">
<div class="progress my-2">
<div id="download-progress" class="progress-bar bg-success" role="progressbar" aria-valuenow="0"
aria-valuemin="0" aria-valuemax="100" style="width: 0%;">0%</div>
</div>
<div class="d-grid gap-2">
<button id="check-download-gateways" class="btn btn-primary btn-lg btn-block">Check & filter download
gateways</button>
</div>
</div>
<div class="col-md-6">
<div class="progress my-2">
<div id="upload-progress" class="progress-bar bg-success" role="progressbar" aria-valuenow="0"
aria-valuemin="0" aria-valuemax="100" style="width: 0%;">0%</div>
</div>
<div class="d-grid gap-2">
<button id="check-upload-gateways" class="btn btn-primary btn-lg btn-block">Check & filter upload
gateways</button>
</div>
</div>
</div>
<div class="text-center my-4">
This will upload files to the <a href="https://ipfs.io">IPFS</a> network. You can hold <kbd>CTRL</kbd> while
selecting files to upload multiple at once.
</div>
<div class="row">
<div class="col-md-6">
<label for="gateway-select" class="form-label">Select a gateway to view your uploaded files:</label>
<select id="gateway-select" class="form-select"></select>
</div>
<div class="col-md-6">
<label for="upload-gateway-select" class="form-label">Select a gateway to upload your files:</label>
<select id="upload-gateway-select" class="form-select"></select>
</div>
</div>
<form id="addForm" class="form my-3" method="post" enctype="multipart/form-data">
<div class="mb-3">
<label for="file" class="form-label">Choose file(s):</label>
<input type="file" class="form-control" name="file" id="file" multiple>
</div>
<button class="btn btn-light" id="submit" style="display: none;">Upload</button>
</form>
<div id="loading" class="alert alert-info" role="alert" style="display: none;">Loading...</div>
<ul id="hashes" class="list-group my-3" style="display: none;"></ul>
<div id="pin-div" class="alert alert-warning" role="warning" style="display: none;">
Despite being pinned, the file uploaded to the IPFS node might be deleted. To ensure its permanence, you can pin
these files on your IPFS node:<br>
<code id="pin-code"></code>
</div>
<footer>
Created by <a href="https://smitop.com">Smitop</a>, modified by <a href="https://github.com/yusufgurdogan">Yusuf
Gürdoğan</a>.
</footer>
</div>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>