yes
This commit is contained in:
1
src/app/api/admin/folders/route.ts
Normal file
1
src/app/api/admin/folders/route.ts
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
39
src/app/api/admin/posts/route.ts
Normal file
39
src/app/api/admin/posts/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
|
||||
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;
|
||||
|
||||
// Create slug from title
|
||||
const slug = title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/(^-|-$)/g, '');
|
||||
|
||||
// Create frontmatter
|
||||
const frontmatter = matter.stringify(content, {
|
||||
title,
|
||||
date,
|
||||
tags,
|
||||
summary,
|
||||
});
|
||||
|
||||
// Write the file
|
||||
const filePath = path.join(postsDirectory, `${slug}.md`);
|
||||
fs.writeFileSync(filePath, frontmatter);
|
||||
|
||||
return NextResponse.json({ success: true, slug });
|
||||
} catch (error) {
|
||||
console.error('Error creating post:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Error creating post' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
55
src/app/api/admin/upload/route.ts
Normal file
55
src/app/api/admin/upload/route.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), 'posts');
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file') as File;
|
||||
const folderPath = formData.get('path') as string;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No file provided' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Read the file content
|
||||
const content = await file.text();
|
||||
|
||||
// Parse frontmatter
|
||||
const { data, content: markdownContent } = matter(content);
|
||||
|
||||
// Create slug from filename (without .md extension)
|
||||
const slug = file.name.replace(/\.md$/, '');
|
||||
|
||||
// Create the full path for the file
|
||||
const fullPath = path.join(postsDirectory, folderPath, file.name);
|
||||
|
||||
// Ensure the directory exists
|
||||
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||
|
||||
// Write the file
|
||||
fs.writeFileSync(fullPath, content);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
path: fullPath,
|
||||
slug,
|
||||
title: data.title || slug,
|
||||
date: data.date || new Date().toISOString().split('T')[0],
|
||||
tags: data.tags || [],
|
||||
summary: data.summary || '',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error uploading file:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Error uploading file' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user