Update package-lock.json with new SWC binaries for various platforms; enhance Admin and Manage pages with folder path display and drag-and-drop functionality for posts, including move post API integration.
This commit is contained in:
35
src/app/api/admin/posts/move/route.ts
Normal file
35
src/app/api/admin/posts/move/route.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), 'posts');
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { slug, from, to } = body;
|
||||
if (!slug) {
|
||||
return NextResponse.json({ error: 'Missing slug' }, { status: 400 });
|
||||
}
|
||||
// Compute source and destination paths
|
||||
const fromPath = from && from.trim() !== ''
|
||||
? path.join(postsDirectory, from, `${slug}.md`)
|
||||
: path.join(postsDirectory, `${slug}.md`);
|
||||
const toPath = to && to.trim() !== ''
|
||||
? path.join(postsDirectory, to, `${slug}.md`)
|
||||
: path.join(postsDirectory, `${slug}.md`);
|
||||
|
||||
// Ensure source exists
|
||||
if (!fs.existsSync(fromPath)) {
|
||||
return NextResponse.json({ error: 'Source post does not exist' }, { status: 404 });
|
||||
}
|
||||
// Ensure destination directory exists
|
||||
fs.mkdirSync(path.dirname(toPath), { recursive: true });
|
||||
// Move the file
|
||||
fs.renameSync(fromPath, toPath);
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error moving post:', error);
|
||||
return NextResponse.json({ error: 'Error moving post' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ const postsDirectory = path.join(process.cwd(), 'posts');
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { title, date, tags, summary, content } = body;
|
||||
const { title, date, tags, summary, content, path: folderPath } = body;
|
||||
|
||||
// Create slug from title
|
||||
const slug = title
|
||||
@@ -25,8 +25,15 @@ export async function POST(request: Request) {
|
||||
author: process.env.NEXT_PUBLIC_BLOG_OWNER + "'s" || 'Anonymous',
|
||||
});
|
||||
|
||||
// Write the file
|
||||
const filePath = path.join(postsDirectory, `${slug}.md`);
|
||||
// Write the file in the correct folder if provided
|
||||
let filePath;
|
||||
if (folderPath && folderPath.trim() !== '') {
|
||||
filePath = path.join(postsDirectory, folderPath, `${slug}.md`);
|
||||
// Ensure the directory exists
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
} else {
|
||||
filePath = path.join(postsDirectory, `${slug}.md`);
|
||||
}
|
||||
fs.writeFileSync(filePath, frontmatter);
|
||||
|
||||
return NextResponse.json({ success: true, slug });
|
||||
|
||||
Reference in New Issue
Block a user