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:
2025-06-19 13:23:35 +02:00
parent 13c841adb8
commit 24d03302b6
4 changed files with 185 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ import path from 'path';
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { path: itemPath, name, type } = body;
const { path: itemPath, name, type, recursive } = body;
if (!name || !type) {
return NextResponse.json(
@@ -58,8 +58,8 @@ export async function POST(request: NextRequest) {
}, { status: 404 });
}
// For folders, check if it's empty
if (type === 'folder') {
// For folders, check if it's empty unless recursive is true
if (type === 'folder' && !recursive) {
try {
const files = await fs.readdir(fullPath);
if (files.length > 0) {

View 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);
}

View File

@@ -0,0 +1,18 @@
import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';
const postsDirectory = path.join(process.cwd(), 'posts');
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const slug = searchParams.get('slug');
if (!slug) return NextResponse.json({ size: null, created: null });
try {
const filePath = path.join(postsDirectory, `${slug}.md`);
const stat = fs.statSync(filePath);
return NextResponse.json({ size: stat.size, created: stat.birthtime.toISOString() });
} catch {
return NextResponse.json({ size: null, created: null });
}
}