dont wait for download

This commit is contained in:
hzrd149 2024-09-07 17:15:12 -05:00
parent 34d373e532
commit a02f16a86f
7 changed files with 54 additions and 23 deletions

2
.env
View File

@ -1,4 +1,4 @@
NOSTR_RELAYS=wss://nostrue.com,wss://nos.lol,wss://relay.damus.io NOSTR_RELAYS=wss://nos.lol,wss://relay.damus.io
BLOSSOM_SERVERS=https://cdn.hzrd149.com BLOSSOM_SERVERS=https://cdn.hzrd149.com
MAX_FILE_SIZE='2 MB' MAX_FILE_SIZE='2 MB'

View File

@ -1,5 +1,5 @@
# A list of nostr relays to search # A list of nostr relays to search
NOSTR_RELAYS=wss://nostrue.com,wss://nos.lol,wss://relay.damus.io NOSTR_RELAYS=wss://nos.lol,wss://relay.damus.io
# A list of fallback blossom servers # A list of fallback blossom servers
BLOSSOM_SERVERS=https://cdn.satellite.earth BLOSSOM_SERVERS=https://cdn.satellite.earth

View File

@ -14,8 +14,33 @@
</script> </script>
</head> </head>
<body> <body>
<label>relays</label>
<br />
<textarea type="text" id="relays" cols="50" rows="4"></textarea>
<br />
<br />
<label>blossom servers</label>
<br />
<textarea type="text" id="servers" cols="50" rows="4"></textarea>
<br />
<br />
<input type="file" id="files" webkitdirectory directory multiple /> <input type="file" id="files" webkitdirectory directory multiple />
<button id="upload-button">Upload nsite</button> <button id="upload-button">Upload nsite</button>
<div
id="log"
style="
max-height: 50em;
max-width: 80em;
width: 100%;
border: 1px solid gray;
min-height: 8em;
margin: 0.5em 0;
overflow: auto;
font-size: 0.8em;
gap: 0.1em;
white-space: pre;
"
></div>
<script type="module" src="./main.js"></script> <script type="module" src="./main.js"></script>
</body> </body>
</html> </html>

View File

@ -1,6 +1,13 @@
import { multiServerUpload, BlossomClient } from "blossom-client-sdk"; import { multiServerUpload, BlossomClient } from "blossom-client-sdk";
import { SimplePool } from "nostr-tools"; import { SimplePool } from "nostr-tools";
const logContainer = document.getElementById("log");
function log(...args) {
const el = document.createElement("div");
el.innerText = args.join(" ");
logContainer.appendChild(el);
}
const uploadButton = document.getElementById("upload-button"); const uploadButton = document.getElementById("upload-button");
/** @type {HTMLInputElement} */ /** @type {HTMLInputElement} */
@ -47,8 +54,8 @@ async function readFileSystemEntry(entry) {
files.push({ file, path, sha256 }); files.push({ file, path, sha256 });
} catch (e) { } catch (e) {
console.log("Failed to add" + entry.fullPath); log("Failed to add" + entry.fullPath);
console.log(e); log(e.message);
} }
} else if (entry instanceof FileSystemDirectoryEntry && entry.isDirectory) { } else if (entry instanceof FileSystemDirectoryEntry && entry.isDirectory) {
const entries = await readFileSystemDirectory(entry); const entries = await readFileSystemDirectory(entry);
@ -79,11 +86,14 @@ const pool = new SimplePool();
* uploads a file system entry to blossom servers * uploads a file system entry to blossom servers
* @param {{file:File, path:string}} files * @param {{file:File, path:string}} files
* @param {import("blossom-client-sdk").Signer} signer * @param {import("blossom-client-sdk").Signer} signer
* @param {*} auth
* @param {string[]} servers
* @param {string[]} relays
*/ */
async function uploadFiles(files, signer, auth) { async function uploadFiles(files, signer, auth, servers, relays) {
for (const { file, path, sha256 } of files) { for (const { file, path, sha256 } of files) {
try { try {
const upload = multiServerUpload(["https://cdn.hzrd149.com", "https://cdn.satellite.earth"], file, signer, auth); const upload = multiServerUpload(servers, file, signer, auth);
let published = false; let published = false;
for await (let { blob } of upload) { for await (let { blob } of upload) {
@ -97,13 +107,13 @@ async function uploadFiles(files, signer, auth) {
["x", sha256], ["x", sha256],
], ],
}); });
await pool.publish(["wss://nostrue.com"], signed); await pool.publish(relays, signed);
console.log("Published", path, sha256, signed); log("Published", path, sha256, signed.id);
} }
} }
} catch (error) { } catch (error) {
console.warn(`Failed to upload ${path}`, error); log(`Failed to upload ${path}`, error);
} }
} }
} }
@ -112,6 +122,8 @@ uploadButton.addEventListener("click", async () => {
if (!window.nostr) return alert("Missing NIP-07 signer"); if (!window.nostr) return alert("Missing NIP-07 signer");
const signer = (draft) => window.nostr.signEvent(draft); const signer = (draft) => window.nostr.signEvent(draft);
const relays = document.getElementById("relays").value.split(/\n|,/);
const servers = document.getElementById("servers").value.split(/\n|,/);
try { try {
if (filesInput.files) { if (filesInput.files) {
@ -120,16 +132,9 @@ uploadButton.addEventListener("click", async () => {
// strip leading dir // strip leading dir
for (const file of files) file.path = file.path.replace(/^[^\/]+\//, "/"); for (const file of files) file.path = file.path.replace(/^[^\/]+\//, "/");
console.log(`Found files`, files); log(`Found ${files.length} files`);
// const auth = await BlossomClient.createUploadAuth( await uploadFiles(files, signer, undefined, servers, relays);
// files.map((f) => f.sha256),
// signer,
// );
// console.log("Created upload auth", auth);
await uploadFiles(files, signer);
} }
} catch (error) { } catch (error) {
alert(`Failed to upload files: ${error.message}`); alert(`Failed to upload files: ${error.message}`);

View File

@ -12,5 +12,5 @@ keyvSqlite.on("error", (err) => {
process.exit(1); process.exit(1);
}); });
export const files = new Keyv({ store: keyvSqlite, ttl: 1000 * 60 * 60 * 24, namespace: "files" }); export const files = new Keyv({ store: keyvSqlite, namespace: "files" });
export const downloaded = new Keyv({ store: keyvSqlite, ttl: 1000 * 30, namespace: "downloaded" }); export const downloaded = new Keyv({ store: keyvSqlite, ttl: 1000 * 60 * 5, namespace: "downloaded" });

View File

@ -36,8 +36,6 @@ async function downloadFile(sha256: string, servers = BLOSSOM_SERVERS) {
} }
export async function downloadSite(pubkey: string) { export async function downloadSite(pubkey: string) {
if (await downloaded.get(pubkey)) return;
const user = await ndk.getUser({ pubkey }); const user = await ndk.getUser({ pubkey });
const blossomServers = await ndk.fetchEvent([{ kinds: [USER_BLOSSOM_SERVER_LIST_KIND], authors: [pubkey] }]); const blossomServers = await ndk.fetchEvent([{ kinds: [USER_BLOSSOM_SERVER_LIST_KIND], authors: [pubkey] }]);

View File

@ -51,7 +51,10 @@ app.use(async (ctx, next) => {
if (pubkey) { if (pubkey) {
if (!(await downloaded.get(pubkey))) { if (!(await downloaded.get(pubkey))) {
await downloadSite(pubkey); // don't wait for download
downloadSite(pubkey);
await downloaded.set(pubkey, true);
} }
await send(ctx, join(pubkey, ctx.path), { root: "data/sites", index: "index.html" }); await send(ctx, join(pubkey, ctx.path), { root: "data/sites", index: "index.html" });