31 lines
676 B
TypeScript
31 lines
676 B
TypeScript
import fs from "fs-extra";
|
|
import { access } from "fs/promises";
|
|
import imageThumbnail from "image-thumbnail";
|
|
import path from "path";
|
|
|
|
export default async function GenerateThumbnail(
|
|
file: string,
|
|
thumbPath: string
|
|
) {
|
|
try {
|
|
//Ensure the thumbs directory exists.
|
|
await fs.ensureDir(path.dirname(thumbPath));
|
|
|
|
try {
|
|
await access(thumbPath);
|
|
return;
|
|
return;
|
|
} catch {}
|
|
|
|
const thumbnail = await imageThumbnail(file, {
|
|
responseType: "buffer",
|
|
height: 250,
|
|
width: 250,
|
|
});
|
|
|
|
await fs.writeFile(thumbPath, thumbnail);
|
|
} catch (err) {
|
|
console.error("Error when genenerating thumbnail:", thumbPath);
|
|
}
|
|
}
|