2022-01-28 04:59:21 +00:00
|
|
|
import { getPlaiceholder } from 'plaiceholder';
|
|
|
|
|
import { buildUrl } from 'cloudinary-build-url';
|
|
|
|
|
|
|
|
|
|
export default async function buildBase64Data(
|
2022-01-28 22:54:47 +00:00
|
|
|
cloudinaryUrl,
|
|
|
|
|
imageSource,
|
2022-01-28 04:59:21 +00:00
|
|
|
alt,
|
|
|
|
|
additionalProps = {}
|
|
|
|
|
) {
|
2022-01-28 22:54:47 +00:00
|
|
|
let imagePath = imageSource;
|
|
|
|
|
if (cloudinaryUrl) {
|
|
|
|
|
const folderName = process.env.PUBLIC_FOLDER_NAME;
|
|
|
|
|
const cloudName = process.env.PUBLIC_CLOUD_NAME;
|
2022-01-28 04:59:21 +00:00
|
|
|
|
2022-01-28 22:54:47 +00:00
|
|
|
if (imageSource && alt && additionalProps) {
|
|
|
|
|
imagePath = buildUrl(`${folderName}/${imageSource}`, {
|
|
|
|
|
cloud: {
|
|
|
|
|
cloudName,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-01-28 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imagePath) {
|
|
|
|
|
try {
|
|
|
|
|
const { base64, img } = await getPlaiceholder(imagePath, { size: 10 });
|
|
|
|
|
return {
|
|
|
|
|
imageProps: {
|
|
|
|
|
...img,
|
|
|
|
|
blurDataURL: base64,
|
|
|
|
|
},
|
|
|
|
|
alt,
|
|
|
|
|
...additionalProps,
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Error getting plaiceholder
|
|
|
|
|
// throw new Error('Error creating plaiceholder base64 image');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|