import { Client } from "@atcute/client"; import { fetchHandler, getPds, MAX_SITE_SIZE } from "../utils.ts"; export default async function ( did: `did:${"plc" | "web"}:${string}`, cid: string ): Promise { // if the user has been manually taken down (cache dir deleted and replaced with empty file) // this errors out for quick exit // saving the round trip to resolve the pds and blob. await Deno.mkdir(`./blobs/${did}`, { recursive: true }); const pds = await getPds(did); if (!pds) throw "PDS not found"; const { data, ok } = await new Client({ handler: fetchHandler({ service: pds }), }).get("com.atproto.sync.getBlob", { params: { did, cid, }, as: "blob", }); if (!ok) throw `${data.error}`; // if the file size cant be calculated or the file cant be written, still return the blob try { let size = 0; for await (const file of Deno.readDir(`./blobs/${did}`)) { size += (await Deno.stat(`./blobs/${did}/${file.name}`)).size; } // only write if total stays below max site size if (size + data.size <= MAX_SITE_SIZE) await Deno.writeFile(`./blobs/${did}/${cid}`, await data.bytes(), { createNew: true, }); } catch (e) { console.warn(e); } return data; }