Skip to content

Commit

Permalink
Update detectDominantColorFromImage.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
koenoe committed Jan 16, 2025
1 parent 18c1c57 commit 7eac432
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/lib/detectDominantColorFromImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,19 @@ const CACHE_PREFIX = 'detectDominantColorFromImage:v1:';
const lambda = new LambdaClient();

async function detectDominantColorFromImage(url: string): Promise<string> {
try {
const command = new InvokeCommand({
FunctionName: Resource.DominantColor.name,
Payload: JSON.stringify({ url }),
});

const response = await lambda.send(command);
const result = JSON.parse(
Buffer.from(response.Payload!).toString(),
) as Readonly<{
color: string;
}>;

return result.color;
} catch (error) {
console.error('Error in detectDominantColorFromImage:', error);
return DEFAULT_BACKGROUND_COLOR;
}
const command = new InvokeCommand({
FunctionName: Resource.DominantColor.name,
Payload: JSON.stringify({ url }),
});

const response = await lambda.send(command);
const result = JSON.parse(
Buffer.from(response.Payload!).toString(),
) as Readonly<{
color: string;
}>;

return result.color;
}

const detectDominantColorFromImageWithCache = async (
Expand All @@ -40,9 +35,13 @@ const detectDominantColorFromImageWithCache = async (
return cachedValue;
}

const dominantColor = await detectDominantColorFromImage(url);
await setCacheItem<string>(key, dominantColor, { ttl: null });
return dominantColor;
try {
const dominantColor = await detectDominantColorFromImage(url);
await setCacheItem<string>(key, dominantColor, { ttl: null });
return dominantColor;
} catch (_error) {
return DEFAULT_BACKGROUND_COLOR;
}
};

export default detectDominantColorFromImageWithCache;

0 comments on commit 7eac432

Please sign in to comment.