-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
127 lines (111 loc) · 3.17 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
<meta lang="en" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Agregore Drag and Drop</title>
<style>
@import url("agregore://theme/vars.css");
html {
background: var(--ag-theme-background);
color: var(--ag-theme-text);
font-family: var(--ag-theme-font-family);
}
#uploadBox {
padding: 1em;
border: 0.25em solid var(--ag-theme-primary);
border-radius: 0.5em;
display: flex;
justify-content: center;
align-items: center;
}
a {
color: var(--ag-theme-secondary);
padding: 0.15em 0.5em;
border-radius: 0.5em;
}
main {
margin: 1em;
}
</style>
<main>
<label for="protocolSelect">
Protocol:
<select id="protocolSelect">
<option value="ipfs" selected>
Inter-Planetary File System (IPFS://)
</option>
<option value="hyper">Hypercore-Protocol (HYPER://)</option>
</select>
</label>
<section id="uploadBox">
<h1>Drop a file to upload it</h1>
</section>
<ul id="uploadListBox"></ul>
<footer>
<a href="https://github.com/AgregoreWeb/agregore-drag-and-drop"
>Source Code</a
>
</footer>
</main>
<script>
function $(query) {
return document.querySelector(query)
}
const uploadBox = $('#uploadBox')
uploadBox.ondragover = () => false
uploadBox.ondrop = async (e) => {
e.preventDefault()
const { dataTransfer } = e
if(!dataTransfer) return
await uploadFiles(dataTransfer.files);
}
const uploadListBox = $('#uploadListBox')
const protocolSelect = $('#protocolSelect')
async function uploadFiles(files) {
const protocol = protocolSelect.value;
const formData = new FormData();
// Append each file to the FormData
for (const file of files) {
formData.append('file', file, file.name);
}
// Construct the URL based on the protocol
let url;
if (protocol === 'hyper') {
const hyperdriveUrl = await generateHyperdriveKey('drag-and-drop');
url = `${hyperdriveUrl}`;
} else {
url = `ipfs://bafyaabakaieac/`;
}
// Perform the upload for each file
try {
const response = await fetch(url, {
method: 'PUT',
body: formData,
});
if (!response.ok) {
addError(files, await response.text());
}
const urlResponse = protocol === 'hyper' ? response.url : response.headers.get('Location');
addURL(urlResponse);
} catch (error) {
console.error(`Error uploading ${files}:`, error);
}
}
async function generateHyperdriveKey(name) {
try {
const response = await fetch(`hyper://localhost/?key=${name}`, { method: 'POST' });
if (!response.ok) {
throw new Error(`Failed to generate Hyperdrive key: ${response.statusText}`);
}
return await response.text(); // This returns the hyper:// URL
} catch (error) {
console.error('Error generating Hyperdrive key:', error);
throw error;
}
}
function addURL(url) {
uploadListBox.innerHTML += `<li><a href="${url}">${url}</a></li>`
}
function addError(name, text) {
uploadListBox.innerHTML += `<li class="log">Error in ${name}: ${text}</li>`
}
</script>