使用openai进行了代码review
This commit is contained in:
@@ -1,194 +1,200 @@
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import JSZip from "jszip";
|
||||
import { marked } from "marked";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireApiAuth } from "@/lib/api-auth";
|
||||
|
||||
// Use a global prisma instance to avoid "too many connections" in dev
|
||||
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
|
||||
const prisma = globalForPrisma.prisma || new PrismaClient();
|
||||
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
||||
type ZipEntry = {
|
||||
path: string;
|
||||
isDir: boolean;
|
||||
content?: string;
|
||||
};
|
||||
|
||||
// Disable body parser strictly (Next.js App Router handles FormData naturally)
|
||||
// export const config = {
|
||||
// api: {
|
||||
// bodyParser: false,
|
||||
// },
|
||||
// };
|
||||
// No need for config in App Router route handlers.
|
||||
type ParsedMarkdown = {
|
||||
title: string;
|
||||
order: number;
|
||||
tags: string[];
|
||||
htmlContent: string;
|
||||
};
|
||||
|
||||
const MAX_RESTORE_FILE_SIZE = 50 * 1024 * 1024; // 50 MB
|
||||
|
||||
function normalizeZipPath(input: string): string {
|
||||
const noBackslashes = input.replace(/\\/g, "/");
|
||||
const noTrailingSlash = noBackslashes.endsWith("/") ? noBackslashes.slice(0, -1) : noBackslashes;
|
||||
return noTrailingSlash.trim();
|
||||
}
|
||||
|
||||
function getDepth(path: string): number {
|
||||
return path.split("/").length;
|
||||
}
|
||||
|
||||
function getParentPath(path: string): string | null {
|
||||
const parts = path.split("/");
|
||||
if (parts.length <= 1) return null;
|
||||
return parts.slice(0, -1).join("/");
|
||||
}
|
||||
|
||||
function getNameFromPath(path: string): string {
|
||||
return path.split("/").pop() || "Untitled";
|
||||
}
|
||||
|
||||
async function parseMarkdownWithFrontmatter(filePath: string, content: string): Promise<ParsedMarkdown> {
|
||||
const defaultTitle = getNameFromPath(filePath).replace(/\.md$/i, "") || "Untitled";
|
||||
let title = defaultTitle;
|
||||
let tags: string[] = [];
|
||||
let order = 0;
|
||||
let markdownBody = content;
|
||||
|
||||
const fmMatch = markdownBody.match(/^---\n([\s\S]*?)\n---\n/);
|
||||
if (fmMatch) {
|
||||
const fmString = fmMatch[1];
|
||||
markdownBody = markdownBody.slice(fmMatch[0].length);
|
||||
|
||||
const titleMatch = fmString.match(/title:\s*"(.*)"/);
|
||||
if (titleMatch && titleMatch[1]) title = titleMatch[1];
|
||||
|
||||
const tagsMatch = fmString.match(/tags:\s*\[(.*)\]/);
|
||||
if (tagsMatch && tagsMatch[1]) {
|
||||
tags = tagsMatch[1]
|
||||
.split(",")
|
||||
.map((item) => item.trim().replace(/^"|"$/g, ""))
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
const orderMatch = fmString.match(/order:\s*(\d+)/);
|
||||
if (orderMatch && orderMatch[1]) {
|
||||
order = parseInt(orderMatch[1], 10);
|
||||
}
|
||||
}
|
||||
|
||||
const htmlContent = await marked(markdownBody, { gfm: true, breaks: true });
|
||||
return { title, order, tags, htmlContent };
|
||||
}
|
||||
|
||||
function collectFolderPaths(entries: ZipEntry[]): string[] {
|
||||
const folderSet = new Set<string>();
|
||||
|
||||
for (const entry of entries) {
|
||||
const normalized = normalizeZipPath(entry.path);
|
||||
if (!normalized) continue;
|
||||
|
||||
if (entry.isDir) {
|
||||
folderSet.add(normalized);
|
||||
}
|
||||
|
||||
let current = getParentPath(normalized);
|
||||
while (current) {
|
||||
folderSet.add(current);
|
||||
current = getParentPath(current);
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(folderSet).sort((a, b) => getDepth(a) - getDepth(b));
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const authError = await requireApiAuth();
|
||||
if (authError) return authError;
|
||||
|
||||
try {
|
||||
const formData = await req.formData();
|
||||
const file = formData.get("file") as File;
|
||||
const file = formData.get("file");
|
||||
|
||||
if (!file) {
|
||||
if (!(file instanceof File)) {
|
||||
return NextResponse.json({ error: "No file uploaded" }, { status: 400 });
|
||||
}
|
||||
if (file.size <= 0) {
|
||||
return NextResponse.json({ error: "Empty file" }, { status: 400 });
|
||||
}
|
||||
if (file.size > MAX_RESTORE_FILE_SIZE) {
|
||||
return NextResponse.json({ error: "File too large" }, { status: 413 });
|
||||
}
|
||||
|
||||
const buffer = await file.arrayBuffer();
|
||||
const zip = await JSZip.loadAsync(buffer);
|
||||
const entries: ZipEntry[] = [];
|
||||
const loadTasks: Promise<void>[] = [];
|
||||
|
||||
// Map to store directory paths to their new DB IDs
|
||||
// Data format: "folder/subfolder" -> UUID
|
||||
const pathIdMap = new Map<string, string>();
|
||||
zip.forEach((rawPath, zipEntry) => {
|
||||
if (rawPath.startsWith("__MACOSX") || rawPath.includes(".DS_Store")) return;
|
||||
|
||||
// Prepare data for proper insertion order (Folders first, then files?)
|
||||
// Actually, we need to process by path depth to ensure parents exist.
|
||||
const entries: Array<{ path: string; isDir: boolean; content?: string }> = [];
|
||||
loadTasks.push((async () => {
|
||||
const path = normalizeZipPath(rawPath);
|
||||
if (!path) return;
|
||||
|
||||
// 1. Read all entries
|
||||
const filePromises: Promise<void>[] = [];
|
||||
|
||||
zip.forEach((relativePath, zipEntry) => {
|
||||
if (relativePath.startsWith("__MACOSX") || relativePath.includes(".DS_Store")) {
|
||||
return; // Skip system files
|
||||
}
|
||||
|
||||
const promise = (async () => {
|
||||
if (zipEntry.dir) {
|
||||
// Remove trailing slash for consistency
|
||||
const cleanPath = relativePath.endsWith('/') ? relativePath.slice(0, -1) : relativePath;
|
||||
if (cleanPath) {
|
||||
entries.push({ path: cleanPath, isDir: true });
|
||||
}
|
||||
} else {
|
||||
if (relativePath.endsWith(".md")) {
|
||||
const content = await zipEntry.async("string");
|
||||
entries.push({ path: relativePath, isDir: false, content });
|
||||
}
|
||||
entries.push({ path, isDir: true });
|
||||
return;
|
||||
}
|
||||
})();
|
||||
filePromises.push(promise);
|
||||
if (!path.endsWith(".md")) return;
|
||||
|
||||
const content = await zipEntry.async("string");
|
||||
entries.push({ path, isDir: false, content });
|
||||
})());
|
||||
});
|
||||
|
||||
await Promise.all(filePromises);
|
||||
|
||||
// 2. Clear existing Pages (Transaction usually)
|
||||
// Since sqlite doesn't support nested transactions well in Prisma sometimes,
|
||||
// we'll just do it sequentially but quickly.
|
||||
// Ideally: await prisma.$transaction([prisma.page.deleteMany(), ...])
|
||||
// But logic is complex (recursive id generation), so we delete first.
|
||||
// WARNING: This is destructive.
|
||||
await prisma.page.deleteMany();
|
||||
|
||||
// 3. Sort entries by path depth (number of slashes)
|
||||
entries.sort((a, b) => {
|
||||
const depthA = a.path.split('/').length;
|
||||
const depthB = b.path.split('/').length;
|
||||
return depthA - depthB;
|
||||
});
|
||||
|
||||
// Helper to get or create parent folder
|
||||
const ensureParent = async (entryPath: string): Promise<string | null> => {
|
||||
const parts = entryPath.split('/');
|
||||
if (parts.length <= 1) return null; // Root level
|
||||
|
||||
const parentPath = parts.slice(0, -1).join('/');
|
||||
|
||||
// If parent already processed
|
||||
if (pathIdMap.has(parentPath)) {
|
||||
return pathIdMap.get(parentPath)!;
|
||||
}
|
||||
|
||||
// If parent folder was not explicitly in Zip (implicit folder), create it
|
||||
// Recursively ensure its parent exists
|
||||
const grandParentId = await ensureParent(parentPath);
|
||||
const folderName = parts[parts.length - 2];
|
||||
|
||||
const newFolder = await prisma.page.create({
|
||||
data: {
|
||||
title: folderName,
|
||||
type: 'folder',
|
||||
parentId: grandParentId
|
||||
}
|
||||
});
|
||||
|
||||
pathIdMap.set(parentPath, newFolder.id);
|
||||
return newFolder.id;
|
||||
};
|
||||
|
||||
// 4. Process entries
|
||||
for (const entry of entries) {
|
||||
// Determine parent
|
||||
// If it's a file "A/B.md", parent path is "A".
|
||||
// If it's a folder "A/B", parent path is "A".
|
||||
// Since we sorted by depth, "A" should be processed before "A/B".
|
||||
|
||||
// However, implicit folders might be skipped in sorting if they aren't in `entries`.
|
||||
// So `ensureParent` handles implicit creation.
|
||||
|
||||
const parentId = await ensureParent(entry.path);
|
||||
|
||||
if (entry.isDir) {
|
||||
// Check if already created by ensureParent
|
||||
if (!pathIdMap.has(entry.path)) {
|
||||
const name = entry.path.split('/').pop() || "Untitled Folder";
|
||||
const folder = await prisma.page.create({
|
||||
data: {
|
||||
title: name,
|
||||
type: 'folder',
|
||||
parentId: parentId
|
||||
}
|
||||
});
|
||||
pathIdMap.set(entry.path, folder.id);
|
||||
}
|
||||
} else {
|
||||
// It is a File (.md)
|
||||
const filename = entry.path.split('/').pop()?.replace('.md', '') || "Untitled";
|
||||
|
||||
// Parse Frontmatter
|
||||
let title = filename;
|
||||
let tags: string[] = [];
|
||||
let order = 0;
|
||||
let markdownBody = entry.content || "";
|
||||
|
||||
// Regex for frontmatter
|
||||
const fmMatch = markdownBody.match(/^---\n([\s\S]*?)\n---\n/);
|
||||
if (fmMatch) {
|
||||
const fmString = fmMatch[1];
|
||||
markdownBody = markdownBody.slice(fmMatch[0].length);
|
||||
|
||||
// Simple parsing
|
||||
// title: "Foo"
|
||||
// tags: ["a", "b"]
|
||||
// order: 1
|
||||
|
||||
const titleMatch = fmString.match(/title:\s*"(.*)"/);
|
||||
if (titleMatch) title = titleMatch[1];
|
||||
|
||||
const tagsMatch = fmString.match(/tags:\s*\[(.*)\]/);
|
||||
if (tagsMatch) {
|
||||
// "a", "b" -> split
|
||||
tags = tagsMatch[1].split(',').map(s => s.trim().replace(/^"|"$/g, '')).filter(Boolean);
|
||||
}
|
||||
|
||||
const orderMatch = fmString.match(/order:\s*(\d+)/);
|
||||
if (orderMatch) order = parseInt(orderMatch[1]);
|
||||
}
|
||||
|
||||
// Convert Markdown to HTML for storage (Editor uses HTML)
|
||||
// Ensure GFM is enabled (default true in new versions, but explicit is good)
|
||||
// breaks: true converts \n to <br> (GitHub style)
|
||||
const htmlContent = await marked(markdownBody, { gfm: true, breaks: true });
|
||||
|
||||
await prisma.page.create({
|
||||
data: {
|
||||
title: title,
|
||||
type: 'file',
|
||||
content: htmlContent,
|
||||
tags: JSON.stringify(tags),
|
||||
order: order,
|
||||
parentId: parentId
|
||||
}
|
||||
});
|
||||
}
|
||||
await Promise.all(loadTasks);
|
||||
if (entries.length === 0) {
|
||||
return NextResponse.json({ error: "No valid markdown entries found in zip" }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, count: entries.length });
|
||||
const folderPaths = collectFolderPaths(entries);
|
||||
const fileEntries = entries
|
||||
.filter((entry): entry is ZipEntry & { content: string } => !entry.isDir && typeof entry.content === "string")
|
||||
.sort((a, b) => getDepth(a.path) - getDepth(b.path));
|
||||
|
||||
} catch (e) {
|
||||
console.error("Restore failed:", e);
|
||||
return NextResponse.json({ error: "Restore failed: " + String(e) }, { status: 500 });
|
||||
// Parse markdown before DB transaction to keep lock time low.
|
||||
const parsedFiles = await Promise.all(
|
||||
fileEntries.map(async (entry) => {
|
||||
const parsed = await parseMarkdownWithFrontmatter(entry.path, entry.content);
|
||||
return { entry, parsed };
|
||||
})
|
||||
);
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.page.deleteMany();
|
||||
|
||||
const folderIdMap = new Map<string, string>();
|
||||
for (const folderPath of folderPaths) {
|
||||
const parentPath = getParentPath(folderPath);
|
||||
const parentId = parentPath ? folderIdMap.get(parentPath) || null : null;
|
||||
|
||||
const folder = await tx.page.create({
|
||||
data: {
|
||||
title: getNameFromPath(folderPath),
|
||||
type: "folder",
|
||||
parentId,
|
||||
},
|
||||
});
|
||||
folderIdMap.set(folderPath, folder.id);
|
||||
}
|
||||
|
||||
for (const { entry, parsed } of parsedFiles) {
|
||||
const parentPath = getParentPath(entry.path);
|
||||
const parentId = parentPath ? folderIdMap.get(parentPath) || null : null;
|
||||
|
||||
await tx.page.create({
|
||||
data: {
|
||||
title: parsed.title,
|
||||
type: "file",
|
||||
content: parsed.htmlContent,
|
||||
tags: JSON.stringify(parsed.tags),
|
||||
order: parsed.order,
|
||||
parentId,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
count: entries.length,
|
||||
files: parsedFiles.length,
|
||||
folders: folderPaths.length,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Restore failed:", error);
|
||||
return NextResponse.json({ error: "Restore failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user