[WIP] A (somewhat barebones) atproto app for creating custom sites without hosting!
1import { Client } from "@atcute/client";
2import { fetchHandler, getPds, MAX_SITE_SIZE } from "../utils.ts";
3
4export default async function (
5 did: `did:${"plc" | "web"}:${string}`,
6 cid: string
7): Promise<Blob> {
8 // if the user has been manually taken down (cache dir deleted and replaced with empty file)
9 // this errors out for quick exit
10 // saving the round trip to resolve the pds and blob.
11 await Deno.mkdir(`./blobs/${did}`, { recursive: true });
12
13 const pds = await getPds(did);
14 if (!pds) throw "PDS not found";
15
16 const { data, ok } = await new Client({
17 handler: fetchHandler({ service: pds }),
18 }).get("com.atproto.sync.getBlob", {
19 params: {
20 did,
21 cid,
22 },
23 as: "blob",
24 });
25
26 if (!ok) throw `${data.error}`;
27
28 // if the file size cant be calculated or the file cant be written, still return the blob
29 try {
30 let size = 0;
31 for await (const file of Deno.readDir(`./blobs/${did}`)) {
32 size += (await Deno.stat(`./blobs/${did}/${file.name}`)).size;
33 }
34 // only write if total stays below max site size
35 if (size + data.size <= MAX_SITE_SIZE)
36 await Deno.writeFile(`./blobs/${did}/${cid}`, await data.bytes(), {
37 createNew: true,
38 });
39 } catch (e) {
40 console.warn(e);
41 }
42
43 return data;
44}