feat: Optimisation des images

This commit is contained in:
2022-04-06 13:13:09 +02:00
parent ba8443dca6
commit 93f2c7cc35
9 changed files with 1111 additions and 42 deletions

View File

@ -1,4 +1,5 @@
import { createWriteStream, existsSync, mkdirSync } from "fs";
import sharp from "sharp";
import fs from "fs/promises";
import { pipeline } from "stream";
import { promisify } from "util";
@ -56,15 +57,24 @@ async function fetchData() {
for (const answer of question.questions_id.answers) {
const uuid = answer.answers_id.image;
if (uuid) {
console.log(`${folder}/${uuid}`);
const response = await fetchAsset(uuid);
await streamPipeline(
response.body,
createWriteStream(`${folder}/${uuid}.png`)
);
try {
const thumbnail = sharp().resize({ height: 200 }).webp();
await streamPipeline(
response.body,
thumbnail,
createWriteStream(`${folder}/${uuid}.webp`)
);
} catch (err) {
console.log(err);
}
}
}
}
}
// await sharp('src/assets/arbre.png').resize({ width: 440, height: 690 }).webp().toFile('public/arbre.webp')
}
fetchData();