nsite-ts/public/main.js

143 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-09-01 13:26:37 -05:00
import { multiServerUpload, BlossomClient } from "blossom-client-sdk";
import { SimplePool } from "nostr-tools";
2024-09-07 17:15:12 -05:00
const logContainer = document.getElementById("log");
function log(...args) {
const el = document.createElement("div");
el.innerText = args.join(" ");
logContainer.appendChild(el);
}
2024-09-01 13:26:37 -05:00
const uploadButton = document.getElementById("upload-button");
/** @type {HTMLInputElement} */
const filesInput = document.getElementById("files");
/**
* @param {FileSystemFileEntry} fileEntry
* @returns {File}
*/
export function readFileSystemFile(fileEntry) {
return new Promise((res, rej) => {
fileEntry.file(
(file) => res(file),
(err) => rej(err),
);
});
}
/**
* @param {FileSystemDirectoryEntry} directory
* @returns {FileSystemEntry[]}
*/
export function readFileSystemDirectory(directory) {
return new Promise((res, rej) => {
directory.createReader().readEntries(
(entries) => res(entries),
(err) => rej(err),
);
});
}
/**
* uploads a file system entry to blossom servers
* @param {FileSystemEntry} entry
* @returns {{file: File, path: string, sha256: string}[]}
*/
async function readFileSystemEntry(entry) {
const files = [];
if (entry instanceof FileSystemFileEntry && entry.isFile) {
try {
const file = await readFileSystemFile(entry);
const sha256 = await BlossomClient.getFileSha256(file);
const path = entry.fullPath;
files.push({ file, path, sha256 });
} catch (e) {
2024-09-07 17:15:12 -05:00
log("Failed to add" + entry.fullPath);
log(e.message);
2024-09-01 13:26:37 -05:00
}
} else if (entry instanceof FileSystemDirectoryEntry && entry.isDirectory) {
const entries = await readFileSystemDirectory(entry);
for (const e of entries) files.push(...(await readFileSystemEntry(e)));
}
return files;
}
/**
* uploads a file system entry to blossom servers
* @param {FileList} list
* @returns {{file: File, path: string, sha256: string}[]}
*/
async function readFileList(list) {
const files = [];
for (const file of list) {
const path = file.webkitRelativePath ? file.webkitRelativePath : file.name;
const sha256 = await BlossomClient.getFileSha256(file);
files.push({ file, path, sha256 });
}
return files;
}
const pool = new SimplePool();
/**
* uploads a file system entry to blossom servers
* @param {{file:File, path:string}} files
* @param {import("blossom-client-sdk").Signer} signer
2024-09-07 17:15:12 -05:00
* @param {*} auth
* @param {string[]} servers
* @param {string[]} relays
2024-09-01 13:26:37 -05:00
*/
2024-09-07 17:15:12 -05:00
async function uploadFiles(files, signer, auth, servers, relays) {
2024-09-01 13:26:37 -05:00
for (const { file, path, sha256 } of files) {
try {
2024-09-07 17:15:12 -05:00
const upload = multiServerUpload(servers, file, signer, auth);
2024-09-01 13:26:37 -05:00
let published = false;
for await (let { blob } of upload) {
if (!published) {
const signed = await signer({
kind: 34128,
content: "",
created_at: Math.round(Date.now() / 1000),
tags: [
["d", path],
["x", sha256],
],
});
2024-09-07 17:15:12 -05:00
await pool.publish(relays, signed);
2024-09-01 13:26:37 -05:00
2024-09-07 17:15:12 -05:00
log("Published", path, sha256, signed.id);
2024-09-01 13:26:37 -05:00
}
}
} catch (error) {
2024-09-07 17:15:12 -05:00
log(`Failed to upload ${path}`, error);
2024-09-01 13:26:37 -05:00
}
}
}
uploadButton.addEventListener("click", async () => {
if (!window.nostr) return alert("Missing NIP-07 signer");
const signer = (draft) => window.nostr.signEvent(draft);
2024-09-07 17:15:12 -05:00
const relays = document.getElementById("relays").value.split(/\n|,/);
const servers = document.getElementById("servers").value.split(/\n|,/);
2024-09-01 13:26:37 -05:00
try {
if (filesInput.files) {
const files = await readFileList(filesInput.files);
// strip leading dir
for (const file of files) file.path = file.path.replace(/^[^\/]+\//, "/");
2024-09-07 17:15:12 -05:00
log(`Found ${files.length} files`);
2024-09-01 13:26:37 -05:00
2024-09-07 17:15:12 -05:00
await uploadFiles(files, signer, undefined, servers, relays);
2024-09-01 13:26:37 -05:00
}
} catch (error) {
alert(`Failed to upload files: ${error.message}`);
}
});