Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 61f7abc

Browse files
author
John Richard Chipps-Harding
authored
Fix broken images (#6)
1 parent 2390955 commit 61f7abc

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/pages/api/images.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ type Data = {
99
images: string[];
1010
};
1111

12+
// Files appear to take a while to write... make sure they have existed for a little
13+
const MIN_DIFF = 2000;
14+
1215
export default async function handler(
1316
req: NextApiRequest,
1417
res: NextApiResponse<Data>
1518
) {
1619
const files = await fs.promises.readdir(DIR);
1720

21+
const now = new Date().getTime();
22+
1823
const images = files
1924
.map((fileName) => {
2025
const stat = fs.statSync(DIR + "/" + fileName);
@@ -27,6 +32,10 @@ export default async function handler(
2732
return a.time - b.time;
2833
})
2934
.filter((f) => path.extname(f.name).toLowerCase() === `.${FILE_EXTENSION}`)
35+
.filter((f) => {
36+
const diff = now - f.time;
37+
return diff > MIN_DIFF;
38+
})
3039
.reverse()
3140
.map((f) => {
3241
return f.name;

0 commit comments

Comments
 (0)