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

Commit 3977bd8

Browse files
author
John Richard Chipps-Harding
committed
Filter on filetype
1 parent 03c3fa8 commit 3977bd8

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ You can configure a few things by editing the file `src/config/index.tsx`.
1919
- `DIR` - The directory to watch.
2020
- `MAX_IMAGES` - How many images to request. Default 10.
2121
- `POLL_INTERVAL` - How often should the frontend look for updates. Default 5 seconds.
22+
- `FILE_EXTENSION` - The filetypes supported. Default `png`.
23+
- `FILE_MIME` - The file mime type. Default `image/png`.

src/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const DIR = `D:\\stable-diffusion-webui\\outputs\\txt2img-images`;
22
export const MAX_IMAGES = 10;
33
export const POLL_INTERVAL = 5 * 1000;
4+
export const FILE_EXTENSION = "png";
5+
export const FILE_MIME = "image/png";

src/pages/_app.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import "@/styles/globals.css";
22
import type { AppProps } from "next/app";
33

4-
function MyApp({ Component, pageProps }: AppProps) {
4+
const MyApp = ({ Component, pageProps }: AppProps) => {
55
return <Component {...pageProps} />;
6-
}
6+
};
77

88
export default MyApp;

src/pages/api/image.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "fs";
33

44
import type { NextApiRequest, NextApiResponse } from "next";
55

6-
import { DIR } from "@/config";
6+
import { DIR, FILE_MIME } from "@/config";
77

88
type QueryData = {
99
image: string;
@@ -25,7 +25,7 @@ export default async function handler(
2525
const stat = fs.statSync(filePath);
2626

2727
res.writeHead(200, {
28-
"Content-Type": "image/png",
28+
"Content-Type": FILE_MIME,
2929
"Content-Length": stat.size,
3030
});
3131

src/pages/api/images.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import path from "path";
12
import fs from "fs";
23

34
import type { NextApiRequest, NextApiResponse } from "next";
45

5-
import { DIR, MAX_IMAGES } from "@/config";
6+
import { DIR, FILE_EXTENSION, MAX_IMAGES } from "@/config";
67

78
type Data = {
89
images: string[];
@@ -15,19 +16,20 @@ export default async function handler(
1516
const files = await fs.promises.readdir(DIR);
1617

1718
const images = files
18-
.map(function (fileName) {
19+
.map((fileName) => {
1920
const stat = fs.statSync(DIR + "/" + fileName);
2021
return {
2122
name: fileName,
2223
time: stat.mtime.getTime(),
2324
};
2425
})
25-
.sort(function (a, b) {
26+
.sort((a, b) => {
2627
return a.time - b.time;
2728
})
29+
.filter((f) => path.extname(f.name).toLowerCase() === `.${FILE_EXTENSION}`)
2830
.reverse()
29-
.map(function (v) {
30-
return v.name;
31+
.map((f) => {
32+
return f.name;
3133
});
3234

3335
res.status(200).json({ images: images.slice(0, MAX_IMAGES) });

0 commit comments

Comments
 (0)