Skip to content

Commit

Permalink
avif conversion atm is meh: lovell/sharp#2597
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdymomocat committed Jun 26, 2024
1 parent be832ed commit 795edf0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webtrotion-astro-notion-cms-website-blog",
"version": "1.2.6",
"version": "1.2.7",
"private": false,
"scripts": {
"dev": "astro dev",
Expand Down
20 changes: 10 additions & 10 deletions src/lib/notion/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ export async function getAllTagsWithCounts(): Promise<{ name: string, count: num
}


export function generateFilePath(url: URL, convertoAvif: boolean = false) {
export function generateFilePath(url: URL, convertoWebp: boolean = false) {
const BASE_DIR = "./public/notion/";
if (!fs.existsSync(BASE_DIR)) {
fs.mkdirSync(BASE_DIR);
Expand All @@ -530,14 +530,14 @@ export function generateFilePath(url: URL, convertoAvif: boolean = false) {
const filename = decodeURIComponent(url.pathname.split("/").slice(-1)[0]);
let filepath = `${dir}/${filename}`;

if (convertoAvif && isConvImageType(filename)) {
filepath = `${dir}/${filename.substring(0, filename.lastIndexOf('.'))}.avif`
if (convertoWebp && isConvImageType(filename)) {
filepath = `${dir}/${filename.substring(0, filename.lastIndexOf('.'))}.webp`
}
return filepath;
}

export function isConvImageType(filepath: string) {
if (filepath.includes('.png') || filepath.includes('.jpg') || filepath.includes('.jpeg') || filepath.includes('.webp')) {
if (filepath.includes('.png') || filepath.includes('.jpg') || filepath.includes('.jpeg') || filepath.includes('.avif')) {
return true;
}
return false;
Expand Down Expand Up @@ -604,13 +604,13 @@ export async function downloadFile(url: URL, optimize_img: boolean = true, isFav


if (isImage && isConvImageType(filepath) && optimize_img) {
// Process and write only the optimized avif image
const avifPath = generateFilePath(url, true);
// console.log('Writing to', avifPath);
// Process and write only the optimized webp image
const webpPath = generateFilePath(url, true);
// console.log('Writing to', webpPath);
await stream.pipe(sharp()
// .resize({ width: 1024 }) // Adjust the size as needed for "medium"
.avif({ quality: 80 })) // Adjust quality as needed
.toFile(avifPath)
.webp({ quality: 80 })) // Adjust quality as needed
.toFile(webpPath)
.catch(err => {
console.error('Error processing image:', err);
});
Expand Down Expand Up @@ -852,7 +852,7 @@ function _buildBlock(blockObject: responses.BlockObject): Block {
image.File = {
Type: blockObject.image.type,
Url: blockObject.image.file.url,
OptimizedUrl: isConvImageType(blockObject.image.file.url) && OPTIMIZE_IMAGES ? (blockObject.image.file.url.substring(0, blockObject.image.file.url.lastIndexOf('.')) + ".avif") : blockObject.image.file.url,
OptimizedUrl: isConvImageType(blockObject.image.file.url) && OPTIMIZE_IMAGES ? (blockObject.image.file.url.substring(0, blockObject.image.file.url.lastIndexOf('.')) + ".webp") : blockObject.image.file.url,
ExpiryTime: blockObject.image.file.expiry_time,
};
}
Expand Down

0 comments on commit 795edf0

Please sign in to comment.