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

Commit 0f5947f

Browse files
author
John Richard Chipps-Harding
authored
Recursive Folders (#11)
1 parent cb00259 commit 0f5947f

File tree

5 files changed

+83
-18
lines changed

5 files changed

+83
-18
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the
1717
You can configure a few things by editing the file `src/config/index.tsx`.
1818

1919
- `DIR` - The directory to watch.
20-
- `MAX_IMAGES` - How many images to request. Default 10.
20+
- `RECURSIVE` - Should we scan recursively within folders. Default is `true`.
21+
- `MAX_IMAGES` - How many images to request. Default `10`.
2122
- `POLL_INTERVAL` - How often should the frontend look for updates. Default 5 seconds.
2223
- `RETRY_INTERVAL` - How frequently should the frontend retry polling on failure.
2324
- `FILE_EXTENSION` - The filetypes supported. Default `png`.

package-lock.json

Lines changed: 64 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
"next": "12.3.1",
1616
"react": "18.2.0",
1717
"react-dom": "18.2.0",
18+
"recursive-readdir": "^2.2.2",
1819
"rooks": "^7.4.0"
1920
},
2021
"devDependencies": {
2122
"@types/node": "18.8.3",
2223
"@types/react": "18.0.21",
2324
"@types/react-dom": "18.0.6",
25+
"@types/recursive-readdir": "^2.2.1",
2426
"@typescript-eslint/eslint-plugin": "^5.39.0",
2527
"@typescript-eslint/parser": "^5.39.0",
2628
"eslint": "8.25.0",

src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const DIR = `D:\\stable-diffusion-webui\\outputs\\txt2img-images`;
2+
export const RECURSIVE = true;
23
export const MAX_IMAGES = 10;
34
export const POLL_INTERVAL = 5 * 1000;
45
export const RETRY_INTERVAL = 10 * 1000;

src/pages/api/images.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
import path from "path";
22
import fs from "fs";
33

4+
import recursive from "recursive-readdir";
45
import type { NextApiRequest, NextApiResponse } from "next";
56

6-
import { DIR, FILE_EXTENSION, MAX_IMAGES } from "@/config";
7+
import { DIR, FILE_EXTENSION, MAX_IMAGES, RECURSIVE } from "@/config";
78

89
type Data = {
910
images: string[];
1011
};
1112

1213
// Files appear to take a while to write... make sure they have existed for a little
13-
const MIN_DIFF = 2000;
14+
const MIN_DIFF = 2 * 1000;
1415

1516
export default async function handler(
16-
req: NextApiRequest,
17+
_: NextApiRequest,
1718
res: NextApiResponse<Data>
1819
) {
19-
const files = await fs.promises.readdir(DIR);
20+
const files = RECURSIVE
21+
? await recursive(DIR)
22+
: (await fs.promises.readdir(DIR)).map((f) => path.join(DIR, f));
2023

2124
const now = new Date().getTime();
2225

2326
const images = files
2427
.map((fileName) => {
25-
const stat = fs.statSync(DIR + "/" + fileName);
28+
const stat = fs.statSync(fileName);
2629
return {
27-
name: fileName,
30+
name: path.relative(DIR, fileName),
2831
time: stat.mtime.getTime(),
2932
};
3033
})
3134
.sort((a, b) => {
3235
return a.time - b.time;
3336
})
34-
.filter((f) => path.extname(f.name).toLowerCase() === `.${FILE_EXTENSION}`)
37+
.filter(
38+
(f) =>
39+
path.extname(f.name).toLowerCase() ===
40+
`.${FILE_EXTENSION}`.toLowerCase()
41+
)
3542
.filter((f) => {
3643
const diff = now - f.time;
3744
return diff > MIN_DIFF;

0 commit comments

Comments
 (0)