Something went wrong. Try again.
Large file storage for AT Protocol: an IPFS server & gateway with XRPC upload & admin. atfs.dev
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182// Writing the settings into a card image.//// The splicing itself — manifest parsing, whole-image and per-setting digest// verification, padding, and the save tiers — is @jphastings/gosd's. What is// here is the atfs-shaped part: which settings this flow writes, and getting// a manifest in hand *before* the button is pressed, since the browser's save// dialog only counts as user-initiated if it is reached before the first// await.
import { deriveManifestURL, fetchManifest, parseManifest, withPlaceholders } from "@jphastings/gosd/downloads";
/** The settings this flow writes, and deliberately nothing under ingress/. */export function settingsFor({ ownerDid, identityKey, deviceName }) { return { "env/ATFS_OWNER_DID": ownerDid, "env/ATFS_IDENTITY_KEY": identityKey, ...(deviceName ? { hostname: deviceName } : {}), };}
export function patchedName(filename) { return filename.replace(/(\.img)?$/i, (ext) => `-configured${ext || ".img"}`);}
/** * Reads the catalogue of images this page can fetch for itself. * * Entries are made absolute as they are read: the catalogue may reasonably * name an image relative to this page, and gosd's URL handling wants a real * URL. */export async function loadImageCatalogue(base = location.href) { try { const response = await fetch(new URL("./images.json", base), { cache: "no-cache" }); if (!response.ok) return []; const body = await response.json(); if (!Array.isArray(body?.images)) return [];
return body.images .filter((image) => typeof image?.url === "string") .map((image) => ({ ...image, url: new URL(image.url, base).href, manifest: image.manifest ? new URL(image.manifest, base).href : undefined, })); } catch { return []; }}
export async function manifestForCatalogueEntry(image) { return fetchManifest(image.manifest ?? deriveManifestURL(image.url));}
export async function manifestFromFile(file) { if (!file) throw new Error("choose the image's .inject.json as well"); return parseManifest(JSON.parse(await file.text()));}
/** * Writes the settings into an image and saves the result. * * Both routes are the same call. A file already on disk is handed over as a * blob: URL, which fetch streams exactly as it would a download — so an image * never has to be held in memory just because it came from a disk. */export async function writeImage({ manifest, source, file, settings, onProgress }) { const url = file ? URL.createObjectURL(file) : source; const name = patchedName(file ? file.name : manifest.image.filename);
try { const result = await withPlaceholders( url, {}, { manifest, config: settings, suggestedName: name, onProgress }, ); return { savedAs: name, savedVia: result.savedVia, sha256: result.sha256 }; } finally { if (file) URL.revokeObjectURL(url); }}