More ui more ui

This commit is contained in:
Jaret Burkett committed 2025-02-19 20:54:02 -07:00
1 parent cef7d9e594
commit b0d8fc220d
18 files changed
+722 -13

No files matched your search

@@ -0,0 +1,67 @@
import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';
import { getDatasetsRoot } from '@/app/api/datasets/utils';
export async function POST(request: Request) {
const datasetsPath = await getDatasetsRoot();
const body = await request.json();
const { datasetName } = body;
const datasetFolder = path.join(datasetsPath, datasetName);
try {
// Check if folder exists
if (!fs.existsSync(datasetFolder)) {
return NextResponse.json(
{ error: `Folder '${datasetName}' not found` },
{ status: 404 }
);
}
// Find all images recursively
const imageFiles = findImagesRecursively(datasetFolder);
// Format response
const result = imageFiles.map(imgPath => ({
img_path: imgPath
}));
return NextResponse.json({ images: result });
} catch (error) {
console.error('Error finding images:', error);
return NextResponse.json(
{ error: 'Failed to process request' },
{ status: 500 }
);
}
}
/**
* Recursively finds all image files in a directory and its subdirectories
* @param dir Directory to search
* @returns Array of absolute paths to image files
*/
function findImagesRecursively(dir: string): string[] {
const imageExtensions = ['.png', '.jpg', '.jpeg', '.webp'];
let results: string[] = [];
const items = fs.readdirSync(dir);
for (const item of items) {
const itemPath = path.join(dir, item);
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
// If it's a directory, recursively search it
results = results.concat(findImagesRecursively(itemPath));
} else {
// If it's a file, check if it's an image
const ext = path.extname(itemPath).toLowerCase();
if (imageExtensions.includes(ext)) {
results.push(itemPath);
}
}
}
return results;
}