tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
handle failed image transforms in icon
awarm.space
6 months ago
5f7cd63d
18f21812
+46
-42
1 changed file
expand all
collapse all
unified
split
app
lish
[did]
[publication]
icon.ts
+46
-42
app/lish/[did]/[publication]/icon.ts
···
19
19
}: {
20
20
params: { did: string; publication: string };
21
21
}) {
22
22
-
let did = decodeURIComponent(params.did);
23
23
-
let uri;
24
24
-
if (/^(?!\.$|\.\.S)[A-Za-z0-9._:~-]{1,512}$/.test(params.publication)) {
25
25
-
uri = AtUri.make(
26
26
-
did,
27
27
-
"pub.leaflet.publication",
28
28
-
params.publication,
29
29
-
).toString();
30
30
-
}
31
31
-
let { data: publication } = await supabaseServerClient
32
32
-
.from("publications")
33
33
-
.select(
34
34
-
`*,
35
35
-
publication_subscriptions(*),
36
36
-
documents_in_publications(documents(*))
37
37
-
`,
38
38
-
)
39
39
-
.eq("identity_did", did)
40
40
-
.or(`name.eq."${params.publication}", uri.eq."${uri}"`)
41
41
-
.single();
22
22
+
try {
23
23
+
let did = decodeURIComponent(params.did);
24
24
+
let uri;
25
25
+
if (/^(?!\.$|\.\.S)[A-Za-z0-9._:~-]{1,512}$/.test(params.publication)) {
26
26
+
uri = AtUri.make(
27
27
+
did,
28
28
+
"pub.leaflet.publication",
29
29
+
params.publication,
30
30
+
).toString();
31
31
+
}
32
32
+
let { data: publication } = await supabaseServerClient
33
33
+
.from("publications")
34
34
+
.select(
35
35
+
`*,
36
36
+
publication_subscriptions(*),
37
37
+
documents_in_publications(documents(*))
38
38
+
`,
39
39
+
)
40
40
+
.eq("identity_did", did)
41
41
+
.or(`name.eq."${params.publication}", uri.eq."${uri}"`)
42
42
+
.single();
42
43
43
43
-
let record = publication?.record as PubLeafletPublication.Record | null;
44
44
-
if (!record?.icon) return redirect("/icon.png");
44
44
+
let record = publication?.record as PubLeafletPublication.Record | null;
45
45
+
if (!record?.icon) return redirect("/icon.png");
45
46
46
46
-
let identity = await idResolver.did.resolve(did);
47
47
-
let service = identity?.service?.find((f) => f.id === "#atproto_pds");
48
48
-
console.log(identity);
49
49
-
if (!service) return null;
50
50
-
let cid = (record.icon.ref as unknown as { $link: string })["$link"];
51
51
-
const response = await fetch(
52
52
-
`${service.serviceEndpoint}/xrpc/com.atproto.sync.getBlob?did=${did}&cid=${cid}`,
53
53
-
);
54
54
-
let blob = await response.blob();
55
55
-
let resizedImage = await sharp(await blob.arrayBuffer())
56
56
-
.resize({ width: 32, height: 32 })
57
57
-
.toBuffer();
58
58
-
console.log("fetched favicon!");
59
59
-
return new Response(new Uint8Array(resizedImage), {
60
60
-
headers: {
61
61
-
"Content-Type": "image/png",
62
62
-
"CDN-Cache-Control": "s-maxage=86400, stale-while-revalidate=86400",
63
63
-
"Cache-Control": "public, max-age=3600",
64
64
-
},
65
65
-
});
47
47
+
let identity = await idResolver.did.resolve(did);
48
48
+
let service = identity?.service?.find((f) => f.id === "#atproto_pds");
49
49
+
console.log(identity);
50
50
+
if (!service) return null;
51
51
+
let cid = (record.icon.ref as unknown as { $link: string })["$link"];
52
52
+
const response = await fetch(
53
53
+
`${service.serviceEndpoint}/xrpc/com.atproto.sync.getBlob?did=${did}&cid=${cid}`,
54
54
+
);
55
55
+
let blob = await response.blob();
56
56
+
let resizedImage = await sharp(await blob.arrayBuffer())
57
57
+
.resize({ width: 32, height: 32 })
58
58
+
.toBuffer();
59
59
+
console.log("fetched favicon!");
60
60
+
return new Response(new Uint8Array(resizedImage), {
61
61
+
headers: {
62
62
+
"Content-Type": "image/png",
63
63
+
"CDN-Cache-Control": "s-maxage=86400, stale-while-revalidate=86400",
64
64
+
"Cache-Control": "public, max-age=3600",
65
65
+
},
66
66
+
});
67
67
+
} catch (e) {
68
68
+
return redirect("/icon.png");
69
69
+
}
66
70
}