wallpapers: make sure file is an image

This commit is contained in:
Philippe Loctaux 2023-05-17 23:14:19 +02:00
parent 58cb959ef4
commit 27161f554f

View file

@ -1,6 +1,7 @@
import exifr from "npm:exifr@^7.1.3"; import exifr from "npm:exifr@^7.1.3";
import { writeJson } from "https://deno.land/x/jsonfile@1.0.0/mod.ts"; import { writeJson } from "https://deno.land/x/jsonfile@1.0.0/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const publicPath = "/public"; const publicPath = "/public";
const wallpapersPath = "/wallpapers"; const wallpapersPath = "/wallpapers";
@ -17,10 +18,13 @@ const wallpapers: MyWallpaper[] = [];
// For each file in the wallpapers directory // For each file in the wallpapers directory
for await (const dirEntry of Deno.readDir(imagesPath)) { for await (const dirEntry of Deno.readDir(imagesPath)) {
if (!dirEntry.isFile || dirEntry.name === ".gitkeep") { // Make sure it is a file and it is an image
if (!dirEntry.isFile || !mime.getType(dirEntry.name)?.startsWith("image/")) {
continue; continue;
} }
console.log(`Processing ${dirEntry.name}`);
// Open file // Open file
const path = `${imagesPath}/${dirEntry.name}`; const path = `${imagesPath}/${dirEntry.name}`;
const bytes = await Deno.readFile(path); const bytes = await Deno.readFile(path);
@ -44,8 +48,6 @@ for await (const dirEntry of Deno.readDir(imagesPath)) {
// Final filename // Final filename
const file = `${wallpapersPath}/${dirEntry.name}`; const file = `${wallpapersPath}/${dirEntry.name}`;
console.log(`Processed ${file}`);
wallpapers.push({ wallpapers.push({
file, file,
date, date,