From 9fa38904bbe13e38fa51fe762f24de56b5599091 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Tue, 23 Dec 2025 21:49:00 +0400 Subject: [PATCH] use cover image in publishing initial bsky post --- app/[leaflet_id]/publish/publishBskyPost.ts | 41 ++++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/app/[leaflet_id]/publish/publishBskyPost.ts b/app/[leaflet_id]/publish/publishBskyPost.ts index 9fc2efb9..04cc976b 100644 --- a/app/[leaflet_id]/publish/publishBskyPost.ts +++ b/app/[leaflet_id]/publish/publishBskyPost.ts @@ -16,6 +16,7 @@ import { getMicroLinkOgImage, getWebpageImage, } from "src/utils/getMicroLinkOgImage"; +import { fetchAtprotoBlob } from "app/api/atproto_images/route"; export async function publishPostToBsky(args: { text: string; @@ -34,24 +35,44 @@ export async function publishPostToBsky(args: { let agent = new AtpBaseClient( credentialSession.fetchHandler.bind(credentialSession), ); - let newPostUrl = args.url; - let preview_image = await getWebpageImage(newPostUrl, { - width: 1400, - height: 733, - noCache: true, - }); - let binary = await preview_image.blob(); - let resized_preview_image = await sharp(await binary.arrayBuffer()) + // Get image binary - prefer cover image, fall back to screenshot + let imageBinary: Blob | null = null; + + if (args.document_record.coverImage) { + let cid = + (args.document_record.coverImage.ref as unknown as { $link: string })[ + "$link" + ] || args.document_record.coverImage.ref.toString(); + + let coverImageResponse = await fetchAtprotoBlob(identity.atp_did, cid); + if (coverImageResponse) { + imageBinary = await coverImageResponse.blob(); + } + } + + // Fall back to screenshot if no cover image or fetch failed + if (!imageBinary) { + let preview_image = await getWebpageImage(args.url, { + width: 1400, + height: 733, + noCache: true, + }); + imageBinary = await preview_image.blob(); + } + + // Resize and upload + let resizedImage = await sharp(await imageBinary.arrayBuffer()) .resize({ width: 1200, + height: 630, fit: "cover", }) .webp({ quality: 85 }) .toBuffer(); - let blob = await agent.com.atproto.repo.uploadBlob(resized_preview_image, { - headers: { "Content-Type": binary.type }, + let blob = await agent.com.atproto.repo.uploadBlob(resizedImage, { + headers: { "Content-Type": "image/webp" }, }); let bsky = new BskyAgent(credentialSession); let post = await bsky.app.bsky.feed.post.create( -- 2.51.2