-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add demo uploader/downloader web app
- Loading branch information
1 parent
f0a4901
commit b7545ab
Showing
27 changed files
with
1,806 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<html> | ||
<head> | ||
<title>Swarm.js Demo</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="icon" type="image/png" href="ethereum_icon.png"> | ||
<script src="swarm.min.js"></script> | ||
<script src="index.js"></script> | ||
<style> | ||
* { font-family: helvetica; } | ||
input { width: 160px; } | ||
</style> | ||
<head> | ||
<body> | ||
<h3><img src="ethereum_icon.png"/> Swarm.js Demo</h3> | ||
|
||
<p>Provider: | ||
<input type="text" id="provider" value="http://swarm-gateways.net" onkeyup="setProvider()"> | ||
</input> | ||
</p> | ||
|
||
<p> | ||
<button onclick="download()">Download</button> | ||
<button onclick="upload()">Upload a File</button> | ||
<button onclick="upload(true)">Upload a Directory</button> | ||
<button onclick="upload(new Buffer(prompt('Text:')))">Upload a String</button> | ||
</p> | ||
|
||
<div id="output"></div> | ||
|
||
<div> | ||
<p><strong>Downloads to try:</strong></p> | ||
<table> | ||
<tr> | ||
<td>Demo text</td> | ||
<td>62cb02b7d506e24f347ba0e8029e24bac12c4c2edd80498c9cbe64c30b97b96b</td> | ||
</tr> | ||
<tr> | ||
<td>Demo image</td> | ||
<td>76b9b41b169cbe78cf92f4fa413065536051588db5b1dd052ed12c78754d1008</td> | ||
</tr> | ||
<tr> | ||
<td>Demo directory</td> | ||
<td>6c03d9a5591796c86b998ccde9b5f4e99a10daf9da640f47ccf8a60b87103d24</td> | ||
</tr> | ||
</table> | ||
<p>This demo only interprets PNG, JPG and text. Any other binary is shown as a hex string.</p> | ||
</div> | ||
<body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
let swarm = Swarm.at("http://swarm-gateways.net"); | ||
|
||
// Uploads a file or directory to the Swarm network | ||
const upload = dir => { | ||
swarm.upload(dir).then(print).catch(console.log); | ||
}; | ||
|
||
// Downloads from Swarm and inserts on site | ||
const download = () => { | ||
const hash = prompt("Hash:"); | ||
if (hash) swarm.download(hash).then(print).catch(console.log); | ||
}; | ||
|
||
// Sets the Swarm provider / gateway | ||
const setProvider = e => { | ||
swarm = Swarm.at(document.getElementById("provider").value); | ||
}; | ||
|
||
// Pretty printers | ||
const print = val => { | ||
let p = document.createElement("p"); | ||
let o = document.getElementById("output"); | ||
p.appendChild(toHTML(val)); | ||
o.insertBefore(p, o.firstChild); | ||
}; | ||
|
||
// Interprets data as text/png/jpg/raw/directory, builds an HTML | ||
const toHTML = val => { | ||
// Directory | ||
if (val instanceof Object && !val.length) { | ||
let table = document.createElement("table"); | ||
for (var key in val) { | ||
let row = document.createElement("tr"); | ||
let cell0 = document.createElement("td"); | ||
let cell1 = document.createElement("td"); | ||
cell0.appendChild(toHTML(key)); | ||
cell1.appendChild(toHTML(val[key].data)); | ||
row.appendChild(cell0); | ||
row.appendChild(cell1); | ||
table.appendChild(row); | ||
} | ||
return table; | ||
|
||
// String | ||
} else if (typeof val === "string") { | ||
let span = document.createElement("span"); | ||
span.innerHTML = val; | ||
return span; | ||
|
||
// Buffer | ||
} else if (val.length) { | ||
// PNG | ||
if (val[1] === 80 && val[2] === 78 && val[3] === 71) { | ||
let image = document.createElement("img"); | ||
image.src = "data:image/png;base64," + btoa(String.fromCharCode.apply(null,val)); | ||
return image; | ||
|
||
// JPG | ||
} else if (val[0] === 0xFF && val[1] === 0xD8 && val[val.length-2] === 0xFF && val[val.length-1] === 0xD9) { | ||
let image = document.createElement("img"); | ||
image.src = "data:image/jpg;base64," + btoa(String.fromCharCode.apply(null,val)); | ||
return image; | ||
|
||
// Plain text / binary data | ||
} else { | ||
let isText = true; | ||
for (let i = 0; i < val.length; ++i) | ||
if (val[i] < 32 || val[i] > 126) | ||
isText = false; | ||
return toHTML(isText | ||
? [].map.call(val, c => String.fromCharCode(c)).join("") | ||
: [].map.call(val, b => ("00"+b.toString(16)).slice(-2)).join("")); | ||
}; | ||
}; | ||
}; |
Oops, something went wrong.