Created
May 9, 2023 22:21
-
-
Save dcsan/98fa835c133395ae979923f4ff3582fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const makeEmbed = async (params: MakeEmbedParams) => { | |
let { agent, imageUrl, imageAlt, encoding: imageFormat } = params; | |
imageFormat = imageFormat || guessImageFormat(imageUrl) | |
imageAlt = imageAlt || 'image' | |
const check = fs.existsSync(imageUrl) | |
if (!check) { | |
const msg = `Unable to find image ${imageUrl}`; | |
console.error(msg); | |
// FIXME dont crash in prod env if an image is missing | |
throw new Error(msg); | |
// return | |
} | |
clog.log('file exists', { imageUrl, imageFormat }) | |
const data = fs.readFileSync(imageUrl); | |
clog.log('read data', { length: data.length }) | |
const response = await agent.uploadBlob( | |
data, { encoding: imageFormat } | |
); | |
clog.log('uploadBlob response', JSON.stringify(response, null, 2)) | |
if (!response.success) { | |
const msg = `Unable to upload image ${imageUrl}`; | |
console.error(msg, response); | |
throw new Error(msg); | |
} | |
const { data: { blob: image } } = response; | |
const embed: ImageEmbed = { | |
$type: "app.bsky.embed.images", | |
images: [ | |
{ | |
image, | |
alt: imageAlt, | |
}, | |
], | |
} | |
return embed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment