Jobs Media List functional
This commit is contained in:
30
util/generateThumbnail.ts
Normal file
30
util/generateThumbnail.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user