images resize avec sharp
This commit is contained in:
74
ssg_hooks/cacheImages.js
Normal file
74
ssg_hooks/cacheImages.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { promisify } from 'util';
|
||||
|
||||
import { resizeImages } from '../ssg_hooks/resizeImages.js'
|
||||
|
||||
export async function cacheImages() {
|
||||
const sourceFolder = './.output/public/api/assets';
|
||||
const destinationFolder = './public/api/assets';
|
||||
|
||||
if (!fs.existsSync(destinationFolder)) fs.mkdirSync(destinationFolder, { recursive: true });
|
||||
|
||||
const readdir = promisify(fs.readdir);
|
||||
const stat = promisify(fs.stat);
|
||||
const copyFile = promisify(fs.copyFile);
|
||||
|
||||
async function directoryExists(directoryPath) {
|
||||
try {
|
||||
const stats = await fs.promises.stat(directoryPath);
|
||||
return stats.isDirectory();
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
return false;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function copyFilesIfNotExist(sourceFolder, destinationFolder) {
|
||||
try {
|
||||
const exists = await directoryExists(sourceFolder);
|
||||
if (!exists) {
|
||||
console.log(`Source folder '${sourceFolder}' does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const files = await readdir(sourceFolder);
|
||||
for (const file of files) {
|
||||
const sourceFilePath = path.join(sourceFolder, file);
|
||||
const destinationFilePath = path.join(destinationFolder, file);
|
||||
const sourceFileStat = await stat(sourceFilePath);
|
||||
|
||||
if (sourceFileStat.isFile()) {
|
||||
try {
|
||||
await stat(destinationFilePath);
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
await copyFile(sourceFilePath, destinationFilePath);
|
||||
console.log(`Copied '${file}' to '${destinationFolder}'.`);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log('Files copied successfully.');
|
||||
|
||||
console.log('Start images resizing.');
|
||||
|
||||
const imageSizes = [
|
||||
{ small: 750 },
|
||||
{ large: 1920 },
|
||||
];
|
||||
|
||||
await resizeImages(imageSizes);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
copyFilesIfNotExist(sourceFolder, destinationFolder);
|
||||
}
|
35
ssg_hooks/crawlImages.js
Normal file
35
ssg_hooks/crawlImages.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import { createDirectus, staticToken, rest, readFiles } from '@directus/sdk';
|
||||
import fs from 'fs';
|
||||
|
||||
export async function crawlImages(routes) {
|
||||
const client = createDirectus(process.env.DIRECTUS_URL)
|
||||
.with(staticToken(process.env.DIRECTUS_API_TOKEN))
|
||||
.with(rest());
|
||||
|
||||
const directusFiles = await client.request(
|
||||
readFiles({
|
||||
query: {
|
||||
filter: {
|
||||
type: {
|
||||
_eq: 'image/',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
for (let image of directusFiles) {
|
||||
if (image.type != "image/heic") {
|
||||
const fileExists = async (filePath) => !!(await fs.promises.access(filePath, fs.constants.F_OK).then(() => true).catch(() => false));
|
||||
|
||||
const filePath = `./public/api/assets/${image.id}.webp`;
|
||||
fileExists(filePath)
|
||||
.then(exists => {
|
||||
if (!exists) {
|
||||
routes.add(`/api/assets/${image.id}.webp`);
|
||||
}
|
||||
})
|
||||
.catch(error => console.error('Error:', error));
|
||||
}
|
||||
}
|
||||
}
|
35
ssg_hooks/resizeImages.js
Normal file
35
ssg_hooks/resizeImages.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import fs from 'fs';
|
||||
import sharp from 'sharp';
|
||||
|
||||
export async function resizeImages(sizes) {
|
||||
const sourceFolder = './public/api/assets';
|
||||
const outputFolder = './.output/public';
|
||||
|
||||
for (const size of sizes) {
|
||||
const key = Object.keys(size)[0];
|
||||
const sizeFolder = `${outputFolder}/${key}`;
|
||||
if (!fs.existsSync(sizeFolder)) {
|
||||
fs.mkdirSync(sizeFolder, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(sourceFolder);
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = `${sourceFolder}/${file}`;
|
||||
|
||||
const image = sharp(filePath);
|
||||
|
||||
for (const size of sizes) {
|
||||
const key = Object.keys(size)[0];
|
||||
const sizeFolder = `${outputFolder}/${key}`;
|
||||
const width = parseInt(size[key]);
|
||||
|
||||
await image.clone().resize({ width }).toFile(`${sizeFolder}/${file}`);
|
||||
}
|
||||
}
|
||||
|
||||
// fs.rmSync('./.output/public/api/assets', { recursive: true, force: true });
|
||||
|
||||
console.log('Images resized and saved successfully.');
|
||||
}
|
Reference in New Issue
Block a user