index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. // https://github.com/ipfs/js-ipfs/tree/ipfs%400.55.4/docs/core-api
  3. const IPFS = require('ipfs');
  4. async function create_pin_tag(node, cid) {
  5. const item = document.createElement('li');
  6. for await(const file of node.ls(cid)) {
  7. if(file.name != file.path) {
  8. item.appendChild(document.createTextNode(file.name));
  9. item.appendChild(document.createElement('br'));
  10. }
  11. }
  12. // > If you can't decide between CIDv0 and CIDv1, consider choosing CIDv1
  13. // > for your new project [...]. This is more future-proof and safe for use
  14. // > in browser contexts.
  15. // https://docs.ipfs.tech/concepts/content-addressing/#identifier-formats
  16. Object.entries({
  17. // .toString() uses base58btc by default in js-ipfs v0.55.4
  18. // https://web.archive.org/web/20220811092631/https://upload.wikimedia.org/wikipedia/commons/3/38/Original_source_code_bitcoin-version-0.1.0_file_base58.h.png
  19. 'CID v1 base58btc': cid.toV1().toString('base58btc'),
  20. 'CID v1 base32': cid.toV1().toString('base32'),
  21. 'CID v0 base58btc': cid.toV0().toString('base58btc'),
  22. }).forEach(([label, cid_string]) => {
  23. item.appendChild(document.createTextNode(label + ": "));
  24. const link = document.createElement('a');
  25. link.href = "https://ipfs.io/ipfs/" + cid_string;
  26. link.innerText = cid_string.toString();
  27. item.appendChild(link);
  28. item.appendChild(document.createElement('br'));
  29. });
  30. const unpinButton = document.createElement('button');
  31. unpinButton.innerText = 'unpin';
  32. unpinButton.onclick = async function() {
  33. await node.pin.rm(cid);
  34. unpinButton.remove();
  35. };
  36. item.appendChild(unpinButton);
  37. return item;
  38. }
  39. async function initialize() {
  40. const node = await IPFS.create();
  41. const node_info = await node.id();
  42. console.log('ipfs node id: ' + node_info.id);
  43. const pinList = document.createElement('ul');
  44. const fileReader = new FileReader();
  45. fileReader.onload = async function(event) {
  46. const file = {
  47. path: fileSelector.files[0].name,
  48. content: fileReader.result,
  49. };
  50. const pin = await node.add(file, {wrapWithDirectory: true});
  51. pinList.appendChild(await create_pin_tag(node, pin.cid));
  52. };
  53. const fileSelector = document.createElement('input');
  54. fileSelector.type = 'file';
  55. fileSelector.onchange = function() {
  56. // .readAsBinaryString() breaks binary files somehow
  57. fileReader.readAsArrayBuffer(fileSelector.files[0]);
  58. };
  59. document.body.appendChild(fileSelector);
  60. for await(const { cid, type } of node.pin.ls()) {
  61. if(type != 'indirect') {
  62. pinList.appendChild(await create_pin_tag(node, cid));
  63. }
  64. }
  65. document.body.appendChild(pinList);
  66. }
  67. initialize();