Add folder and post details fetching in ManagePage; implement delete confirmation modal for folders with recursive deletion option in API.
This commit is contained in:
40
src/app/api/admin/folders/details/route.ts
Normal file
40
src/app/api/admin/folders/details/route.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), 'posts');
|
||||
|
||||
function getFolderStats(folderPath: string) {
|
||||
const fullPath = path.join(postsDirectory, folderPath);
|
||||
let items = 0;
|
||||
let size = 0;
|
||||
let created = '';
|
||||
try {
|
||||
const stat = fs.statSync(fullPath);
|
||||
created = stat.birthtime.toISOString();
|
||||
const walk = (dir: string) => {
|
||||
const files = fs.readdirSync(dir);
|
||||
for (const file of files) {
|
||||
const filePath = path.join(dir, file);
|
||||
const stats = fs.statSync(filePath);
|
||||
if (stats.isDirectory()) {
|
||||
walk(filePath);
|
||||
} else {
|
||||
items++;
|
||||
size += stats.size;
|
||||
}
|
||||
}
|
||||
};
|
||||
walk(fullPath);
|
||||
} catch {
|
||||
// ignore errors
|
||||
}
|
||||
return { created, items, size };
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const folderPath = searchParams.get('path') || '';
|
||||
const stats = getFolderStats(folderPath);
|
||||
return NextResponse.json(stats);
|
||||
}
|
||||
Reference in New Issue
Block a user