|
| 1 | +import * as Earthstar from "https://cdn.earthstar-project.org/js/earthstar.web.v10.0.0-rc.1.js"; |
| 2 | + |
| 3 | +// Earthstar stuff |
| 4 | +const settings = new Earthstar.SharedSettings(); |
| 5 | + |
| 6 | +async function setupSettings() { |
| 7 | + const author = await Earthstar.Crypto.generateAuthorKeypair("chou"); |
| 8 | + |
| 9 | + settings.author = author; |
| 10 | + |
| 11 | + const shareKeypair = await Earthstar.Crypto.generateShareKeypair( |
| 12 | + "attachments" |
| 13 | + ); |
| 14 | + |
| 15 | + settings.addShare(shareKeypair.shareAddress); |
| 16 | + const result = await settings.addSecret( |
| 17 | + shareKeypair.shareAddress, |
| 18 | + shareKeypair.secret |
| 19 | + ); |
| 20 | + |
| 21 | + console.log("SECRET", result); |
| 22 | +} |
| 23 | + |
| 24 | +// await setupSettings(); |
| 25 | + |
| 26 | +const shareAddress = settings.shares[0]; |
| 27 | + |
| 28 | +const shareSecret = settings.shareSecrets[shareAddress]; |
| 29 | + |
| 30 | +const replica = new Earthstar.Replica({ |
| 31 | + driver: new Earthstar.ReplicaDriverWeb(shareAddress), |
| 32 | + shareSecret: settings.shareSecrets[shareAddress], |
| 33 | +}); |
| 34 | + |
| 35 | +// DOM Stuff |
| 36 | +const fileInput = document.querySelector('input[type="file"]'); |
| 37 | +const uploadMessage = document.getElementById("upload-result"); |
| 38 | +const downloadUrl = document.getElementById("download-url"); |
| 39 | + |
| 40 | +fileInput.addEventListener("change", async (event) => { |
| 41 | + event.preventDefault(); |
| 42 | + |
| 43 | + const file = event.target.files[0]; |
| 44 | + |
| 45 | + const timestamp = Date.now(); |
| 46 | + |
| 47 | + const extension = file.name.split(".")[1]; |
| 48 | + |
| 49 | + // TODO: use a proper hash ideally |
| 50 | + const path = `/uploads/!${timestamp}.${extension}`; |
| 51 | + |
| 52 | + console.log(path); |
| 53 | + |
| 54 | + const result = await replica.set(settings.author, { |
| 55 | + path, |
| 56 | + text: `This was uploaded by ANDREWWWW`, |
| 57 | + attachment: file.stream(), |
| 58 | + // 48 hours |
| 59 | + deleteAfter: Date.now() * 1000 + 172_800_000_000, |
| 60 | + }); |
| 61 | + |
| 62 | + if (Earthstar.isErr(result)) { |
| 63 | + uploadMessage.textContent = "Failed to upload"; |
| 64 | + } else { |
| 65 | + uploadMessage.textContent = "Successfully uploaded!"; |
| 66 | + } |
| 67 | + |
| 68 | + // console.log(result); |
| 69 | + |
| 70 | + const peer = new Earthstar.Peer(); |
| 71 | + |
| 72 | + peer.addReplica(replica); |
| 73 | + |
| 74 | + const syncer = peer.sync("http://localhost:8000"); |
| 75 | + |
| 76 | + // syncer.onStatusChange((newStatus) => { |
| 77 | + // console.log("STATUS", newStatus) |
| 78 | + // }) |
| 79 | + |
| 80 | + await syncer.isDone(); |
| 81 | + |
| 82 | + console.log("SYNCED!!!"); |
| 83 | + |
| 84 | + const link = `http://localhost:8000/downloads/${timestamp}.${extension}`; |
| 85 | + downloadUrl.href = link; |
| 86 | + downloadUrl.textContent = `Download image at ${link}`; |
| 87 | +}); |
0 commit comments