|
| 1 | +import { NextResponse } from "next/server" |
| 2 | +import clientPromise from "@/lib/mongodb" |
| 3 | + |
| 4 | +export async function POST(request: Request) { |
| 5 | + try { |
| 6 | + const { analysisId, contentId, format } = await request.json() |
| 7 | + |
| 8 | + if (!analysisId && !contentId) { |
| 9 | + return NextResponse.json({ error: "Analysis ID or Content ID is required" }, { status: 400 }) |
| 10 | + } |
| 11 | + |
| 12 | + // For preview mode or when MongoDB is not available, use mock data |
| 13 | + const mockAnalysis = { |
| 14 | + title: "Example Website", |
| 15 | + url: "https://example.com", |
| 16 | + summary: "This is a modern website with various features and content sections.", |
| 17 | + keyPoints: [ |
| 18 | + "Mobile-friendly design", |
| 19 | + "Fast loading times", |
| 20 | + "Clear navigation structure", |
| 21 | + "Strong brand messaging", |
| 22 | + "Effective call-to-actions", |
| 23 | + ], |
| 24 | + keywords: ["technology", "design", "innovation", "services", "solutions"], |
| 25 | + sustainability: { |
| 26 | + score: 78, |
| 27 | + performance: 85, |
| 28 | + scriptOptimization: 72, |
| 29 | + duplicateContent: 92, |
| 30 | + improvements: [ |
| 31 | + "Optimize image sizes", |
| 32 | + "Implement lazy loading", |
| 33 | + "Reduce third-party scripts", |
| 34 | + "Enable browser caching", |
| 35 | + ], |
| 36 | + }, |
| 37 | + contentStats: { |
| 38 | + wordCount: 2450, |
| 39 | + paragraphs: 32, |
| 40 | + headings: 18, |
| 41 | + images: 24, |
| 42 | + links: 47, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + let content = "" |
| 47 | + let title = "Example Website" |
| 48 | + |
| 49 | + // Try to get content from MongoDB if available |
| 50 | + if (process.env.MONGODB_URI) { |
| 51 | + try { |
| 52 | + const client = await clientPromise |
| 53 | + const db = client.db("website-analyzer") |
| 54 | + |
| 55 | + if (contentId) { |
| 56 | + const generatedContent = await db.collection("generated-content").findOne({ |
| 57 | + _id: contentId, |
| 58 | + }) |
| 59 | + |
| 60 | + if (generatedContent) { |
| 61 | + content = generatedContent.content |
| 62 | + |
| 63 | + // Get analysis for title |
| 64 | + const analysis = await db.collection("analyses").findOne({ |
| 65 | + _id: generatedContent.analysisId, |
| 66 | + }) |
| 67 | + |
| 68 | + title = analysis ? analysis.title : "Generated Content" |
| 69 | + } |
| 70 | + } else if (analysisId) { |
| 71 | + const analysis = await db.collection("analyses").findOne({ |
| 72 | + _id: analysisId, |
| 73 | + }) |
| 74 | + |
| 75 | + if (analysis) { |
| 76 | + title = analysis.title |
| 77 | + |
| 78 | + // Create a simple export of the analysis |
| 79 | + content = createAnalysisContent(analysis) |
| 80 | + } |
| 81 | + } |
| 82 | + } catch (dbError) { |
| 83 | + console.error("Database error:", dbError) |
| 84 | + // Fall back to mock data if database operations fail |
| 85 | + content = createAnalysisContent(mockAnalysis) |
| 86 | + } |
| 87 | + } else { |
| 88 | + // Use mock data for preview |
| 89 | + content = createAnalysisContent(mockAnalysis) |
| 90 | + } |
| 91 | + |
| 92 | + // Format content based on requested format |
| 93 | + let formattedContent = content |
| 94 | + |
| 95 | + switch (format) { |
| 96 | + case "html": |
| 97 | + // Simple markdown to HTML conversion |
| 98 | + formattedContent = content |
| 99 | + .replace(/^# (.*$)/gm, "<h1>$1</h1>") |
| 100 | + .replace(/^## (.*$)/gm, "<h2>$1</h2>") |
| 101 | + .replace(/^### (.*$)/gm, "<h3>$1</h3>") |
| 102 | + .replace(/\*\*(.*)\*\*/gm, "<strong>$1</strong>") |
| 103 | + .replace(/\*(.*)\*/gm, "<em>$1</em>") |
| 104 | + .replace(/- (.*)/gm, "<li>$1</li>") |
| 105 | + .replace(/\n\n/gm, "<br/><br/>") |
| 106 | + formattedContent = `<html><head><title>${title}</title></head><body>${formattedContent}</body></html>` |
| 107 | + break |
| 108 | + |
| 109 | + case "plain": |
| 110 | + // Strip markdown |
| 111 | + formattedContent = content |
| 112 | + .replace(/^# (.*$)/gm, "$1\n") |
| 113 | + .replace(/^## (.*$)/gm, "$1\n") |
| 114 | + .replace(/^### (.*$)/gm, "$1\n") |
| 115 | + .replace(/\*\*(.*)\*\*/gm, "$1") |
| 116 | + .replace(/\*(.*)\*/gm, "$1") |
| 117 | + .replace(/- (.*)/gm, "• $1") |
| 118 | + break |
| 119 | + |
| 120 | + case "markdown": |
| 121 | + default: |
| 122 | + // Keep as markdown |
| 123 | + break |
| 124 | + } |
| 125 | + |
| 126 | + // Try to log the export to MongoDB |
| 127 | + if (process.env.MONGODB_URI) { |
| 128 | + try { |
| 129 | + const client = await clientPromise |
| 130 | + const db = client.db("website-analyzer") |
| 131 | + await db.collection("exports").insertOne({ |
| 132 | + analysisId: analysisId || null, |
| 133 | + contentId: contentId || null, |
| 134 | + format, |
| 135 | + createdAt: new Date(), |
| 136 | + }) |
| 137 | + } catch (logError) { |
| 138 | + console.error("Error logging export:", logError) |
| 139 | + // Continue even if logging fails |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return NextResponse.json({ |
| 144 | + content: formattedContent, |
| 145 | + title, |
| 146 | + format, |
| 147 | + }) |
| 148 | + } catch (error) { |
| 149 | + console.error("Error exporting content:", error) |
| 150 | + return NextResponse.json({ |
| 151 | + content: "# Error Exporting Content\n\nThere was an error exporting the content. Please try again.", |
| 152 | + title: "Export Error", |
| 153 | + format: "markdown", |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// Helper function to create analysis content |
| 159 | +function createAnalysisContent(analysis: any) { |
| 160 | + return `# Analysis of ${analysis.title} |
| 161 | +
|
| 162 | +URL: ${analysis.url} |
| 163 | +
|
| 164 | +## Summary |
| 165 | +${analysis.summary} |
| 166 | +
|
| 167 | +## Key Points |
| 168 | +${analysis.keyPoints.map((point: string) => `- ${point}`).join("\n")} |
| 169 | +
|
| 170 | +## Keywords |
| 171 | +${analysis.keywords.join(", ")} |
| 172 | +
|
| 173 | +## Sustainability Score: ${analysis.sustainability.score}% |
| 174 | +
|
| 175 | +## Performance: ${analysis.sustainability.performance}% |
| 176 | +
|
| 177 | +## Script Optimization: ${analysis.sustainability.scriptOptimization}% |
| 178 | +
|
| 179 | +## Content Quality: ${analysis.sustainability.duplicateContent}% |
| 180 | +
|
| 181 | +## Recommendations for Improvement |
| 182 | +${analysis.sustainability.improvements.map((imp: string) => `- ${imp}`).join("\n")} |
| 183 | +
|
| 184 | +## Content Statistics |
| 185 | +- Word Count: ${analysis.contentStats.wordCount} |
| 186 | +- Paragraphs: ${analysis.contentStats.paragraphs} |
| 187 | +- Headings: ${analysis.contentStats.headings} |
| 188 | +- Images: ${analysis.contentStats.images} |
| 189 | +- Links: ${analysis.contentStats.links} |
| 190 | +
|
| 191 | +Generated by WebInsight |
| 192 | +` |
| 193 | +} |
0 commit comments