Skip to content

Commit

Permalink
added exception to image generator (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
e-for-eshaan authored Dec 11, 2023
1 parent c106870 commit 7b47b7a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 39 deletions.
63 changes: 41 additions & 22 deletions src/api-helpers/vercel-generator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,46 @@ export const createImageUsingVercel = async (
env: 'node' | 'browser',
index?: number
): Promise<ImageFile> => {
const fileName = (index || 0) + 1 + '.png';
const fonts = await getFontsForImageGeneration(env);
const generatedImage = new ImageResponse(
<CardTemplate cardType={cardType} data={data} />,
{
width: parseInt(CARD_WIDTH) * SCALE_FACTOR,
height: parseInt(CARD_HEIGHT) * SCALE_FACTOR,
fonts: INTER_FONT_STYLES.map((fontData, index) => ({
name: 'Inter',
data: fonts[index],
style: 'normal',
weight: fontData.weight
}))
try {
const fileName = (index || 0) + 1 + '.png';
const fonts = await getFontsForImageGeneration(env);

const generatedImage = new ImageResponse(
<CardTemplate cardType={cardType} data={data} />,
{
width: parseInt(CARD_WIDTH) * SCALE_FACTOR,
height: parseInt(CARD_HEIGHT) * SCALE_FACTOR,
fonts: INTER_FONT_STYLES.map((fontData, index) => ({
name: 'Inter',
data: fonts[index],
style: 'normal',
weight: fontData.weight
}))
}
);

let imageCopy = generatedImage.clone();
let imageArrayBuffer;

try {
imageArrayBuffer = await generatedImage.arrayBuffer();
} catch (arrayBufferError) {
console.error(
'Error converting image to array buffer:',
arrayBufferError
);
throw new Error('Image array buffer conversion failed');
}
);
let imageCopy = generatedImage.clone();
const imageArrayBuffer = await generatedImage.arrayBuffer();
const imageBuffer = arrayBufferToBuffer(imageArrayBuffer);
return {
data: imageBuffer,
fileName,
image: imageCopy
};

const imageBuffer = arrayBufferToBuffer(imageArrayBuffer);

return {
data: imageBuffer,
fileName,
image: imageCopy
};
} catch (error) {
console.error('Error in createImageUsingVercel:', error);
throw new Error('Image creation failed');
}
};
40 changes: 23 additions & 17 deletions src/pages/api/image-gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DEV } from '../../../constants/general';
import { updatedGhData } from '@/mocks/github';
import { getDataFromGithubResponse } from '@/api-helpers/card-data-adapter';
import { CardTypes, sequence } from '../../../types/cards';
import { ImageFile } from '@/types/images';

export const fetchData = async (): Promise<GitHubDataResponse> => {
if (process.env.NEXT_PUBLIC_APP_ENVIRONMENT === DEV) {
Expand All @@ -24,26 +25,31 @@ export const fetchData = async (): Promise<GitHubDataResponse> => {
export const generateImages = async (
data: GitHubDataResponse,
customSequence?: CardTypes[]
) => {
console.log(chalk.yellow('Generating images...'));
): Promise<ImageFile[]> => {
try {
console.log(chalk.yellow('Generating images...'));

const cardSequence = customSequence || sequence;
const cardSequence = customSequence || sequence;

const adaptedData = getDataFromGithubResponse(data);
const cardsToBeGenerated = cardSequence.filter((card) =>
Boolean(adaptedData[card])
);
const adaptedData = getDataFromGithubResponse(data);
const cardsToBeGenerated = cardSequence.filter((card) =>
Boolean(adaptedData[card])
);

const imageFileBuffers = await Promise.all(
cardsToBeGenerated.map((cardName, index) =>
createImageUsingVercel(
{ ...adaptedData[cardName], username: data.user.login },
cardName,
'node',
index
const imageFileBuffers = await Promise.all(
cardsToBeGenerated.map((cardName, index) =>
createImageUsingVercel(
{ ...adaptedData[cardName], username: data.user.login },
cardName,
'node',
index
)
)
)
);
);

return imageFileBuffers;
return imageFileBuffers;
} catch (error) {
console.error('Error in generateImages:', error);
throw new Error('Image generation failed');
}
};

0 comments on commit 7b47b7a

Please sign in to comment.