itty-bitty/docs/render/ipfs.js

53 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2022-07-27 12:04:38 +00:00
loadSyle(window.script.replace("js", "css"))
2022-06-11 15:48:43 +00:00
2022-06-15 14:49:46 +00:00
loadScript("https://unpkg.com/ipfs@0.63.2/dist/index.min.js", (scr) => {
let cid = params.url.replace("ipfs:",'');
getData(cid);
})
2022-06-29 15:34:07 +00:00
// async function test(cid) {
// let file = {
// path: "hello.txt",
// content: "Hello World 101",
// }
// console.log("adding", file)
2022-06-15 14:49:46 +00:00
2022-06-29 15:34:07 +00:00
// const node = await Ipfs.create()
// console.log("adding", file)
2022-06-15 14:49:46 +00:00
2022-06-29 15:34:07 +00:00
// const fileAdded = await node.add(file);
// console.log("added", fileAdded)
// }
// async function addData(data) {
// const node = await Ipfs.create()
// const results = node.add(data)
// for await (const { cid } of results) {
// console.log(cid.toString())
// }
// }
2022-06-11 15:48:43 +00:00
async function getData(cid) {
const node = await Ipfs.create()
const stream = node.cat(cid)
let data = ''
2022-06-29 15:34:07 +00:00
let chunks = []
for await (const chunk of stream) { chunks.push(chunk) }
var a = new FileReader();
a.onload = function(e) {
console.log("Got IPFS Data", e.target.result)
location.href=e.target.result
2022-06-11 15:48:43 +00:00
}
2022-06-29 15:34:07 +00:00
a.readAsDataURL(new Blob(chunks, {encoding:"UTF-8",type:"text/plain"}));
2022-06-11 15:48:43 +00:00
console.log("Got IPFS Data", data)
return data;
2022-06-29 15:34:07 +00:00
2022-06-11 15:48:43 +00:00
}
let url = params.url.replace("ipfs:", "https://ipfs.io/ipfs/");
2022-06-29 15:34:07 +00:00
// location.href = url;