Skip to content

Commit

Permalink
Debugging powerpoint still
Browse files Browse the repository at this point in the history
  • Loading branch information
jongan69 committed Jan 13, 2025
1 parent 724fb00 commit 892ec0a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 57 deletions.
91 changes: 73 additions & 18 deletions src/pages/api/pptx/generate-powerpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { NextApiRequest, NextApiResponse } from 'next';
import PptxGenJS from 'pptxgenjs';
import OpenAI from 'openai';
import { isSolanaAddress } from '../../../utils/isSolanaAddress';
import { PowerPoint } from 'src/models/PowerPoint';
import mongoose from 'mongoose';
import { connectDB } from '../../../utils/mongooseDb';




const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
Expand Down Expand Up @@ -34,34 +40,71 @@ function calculateFontSize(text: string, maxLength: number = 500): number {
// Base font size is 15
const baseSize = 15;
const minSize = 11;

if (text.length <= maxLength) {
return baseSize;
}

// Gradually reduce font size based on text length
const reduction = Math.floor((text.length - maxLength) / 100);
return Math.max(baseSize - reduction, minSize);
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log('Request received:', {
method: req.method,
headers: req.headers,
body: typeof req.body === 'string' ? 'String body detected' : 'Parsed body',
contentType: req.headers['content-type']
});

if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}

try {
await connectDB();
// Log raw request body if it's a string
if (typeof req.body === 'string') {
console.log('Raw request body (string):', req.body);
try {
req.body = JSON.parse(req.body);
console.log('Successfully parsed body:', req.body);
} catch (parseError) {
console.error('JSON Parse Error:', parseError);
console.log('Failed to parse body. First 100 characters:', req.body.substring(0, 100));
return res.status(400).json({ error: 'Invalid JSON in request body' });
}
}

// Validate request body
console.log('Request body type:', typeof req.body);
console.log('Request body:', req.body);

if (!req.body || typeof req.body !== 'object') {
console.error('Invalid body structure:', req.body);
throw new Error('Invalid request body');
}

const { tokens, thesis } = req.body;

console.log('Extracted tokens:', tokens ? `Array of ${tokens.length} items` : 'undefined');
console.log('Extracted thesis:', thesis ? 'Present' : 'undefined');

if (!tokens || !Array.isArray(tokens)) {
console.error('Invalid tokens structure:', {
tokens,
type: typeof tokens,
isArray: Array.isArray(tokens)
});
throw new Error('Invalid tokens data');
}

if (!thesis || typeof thesis !== 'string') {
console.error('Invalid thesis structure:', {
thesis,
type: typeof thesis
});
throw new Error('Invalid thesis data');
}

Expand Down Expand Up @@ -246,7 +289,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});

// Ensure no empty slides
return result.map(slideTexts =>
return result.map(slideTexts =>
slideTexts.length ? slideTexts : [sentences[0] || '']);
}

Expand Down Expand Up @@ -457,7 +500,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

if (formattedText) {
const dynamicFontSize = calculateFontSize(formattedText);

slide.addText(`${sparkles} ${formatSlideText(formattedText)} ${sparkles}`, {
x: 0.2,
y: 2.3,
Expand Down Expand Up @@ -659,33 +702,45 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
});

// Generate PowerPoint as a base64 string
// Add logging before PowerPoint generation
console.log('Starting PowerPoint generation with:', {
tokenCount: filteredTokens.length,
regularTokenCount: regularTokens.length,
nftTokenCount: nftTokens.length
});

// Add logging for PowerPoint base64 generation
console.log('Generating PowerPoint base64...');
const pptxBase64 = await pptx.write({ outputType: 'base64' });
console.log('PowerPoint base64 generated:', pptxBase64 ? 'Success' : 'Failed');

if (!pptxBase64) {
console.error('No base64 data received from PowerPoint generation');
throw new Error('No base64 data received from API');
}

// Store PowerPoint data and get temporary URL
const storeResponse = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/pptx/serve-powerpoint`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ base64Data: pptxBase64 }),
// Log before database save
console.log('Attempting to save PowerPoint to database...');
const powerPoint = await PowerPoint.create({
pptxBase64,
_id: new mongoose.Types.ObjectId()
});
console.log('PowerPoint saved successfully with ID:', powerPoint._id.toString());

const data = await storeResponse.json();

// Return the ID directly instead of the full response
res.status(200).json({ id: data.id });
res.status(200).json({ id: powerPoint._id.toString() });
} catch (error) {
console.error('Error generating PowerPoint:', error);
res.status(500).json({ error: 'Failed to generate PowerPoint presentation' });
}
} catch (error: any) {
console.error('Error generating PowerPoint:', error);
return res.status(500).json({
console.error('Detailed error information:', {
error: error.message,
stack: error.stack,
name: error.name,
code: error.code
});

return res.status(500).json({
error: error.message || 'Failed to generate PowerPoint presentation',
details: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
Expand Down
40 changes: 1 addition & 39 deletions src/pages/api/pptx/serve-powerpoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import mongoose from 'mongoose';
import { PowerPoint } from '../../../models/PowerPoint';
import { connectDB } from '../../../utils/mongooseDb';

export const config = {
api: {
Expand All @@ -12,19 +13,6 @@ export const config = {
},
}

// MongoDB connection
const connectDB = async () => {
if (mongoose.connections[0].readyState) return;

try {
await mongoose.connect(process.env.MONGODB_URI!);
console.log('MongoDB connected successfully');
} catch (error) {
console.error('MongoDB connection error:', error);
throw error;
}
};

export default async function handler(req: NextApiRequest, res: NextApiResponse) {

try {
Expand All @@ -34,32 +22,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
console.error('Failed to connect to MongoDB:', error);
return res.status(500).json({ error: 'Database connection failed' });
}

if (req.method === 'POST') {
try {
console.log('Processing POST request');
const { base64Data } = req.body;

if (!base64Data) {
console.error('No base64Data provided in request body');
return res.status(400).json({ error: 'No base64Data provided' });
}

console.log('Creating new PowerPoint document, base64 length:', base64Data.length);

// Create new PowerPoint document
const powerPoint = await PowerPoint.create({
base64Data,
_id: new mongoose.Types.ObjectId() // Explicitly create a MongoDB ObjectId
});

console.log('PowerPoint document created with ID:', powerPoint._id.toString());
return res.status(200).json({ id: powerPoint._id.toString() });
} catch (error) {
console.error('Error storing PowerPoint:', error);
return res.status(500).json({ error: 'Failed to store PowerPoint' });
}
}

if (req.method === 'GET') {
try {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/mongooseDb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// MongoDB connection
import mongoose from 'mongoose';

export const connectDB = async () => {
if (mongoose.connections[0].readyState) return;

try {
await mongoose.connect(process.env.MONGODB_URI!);
console.log('MongoDB connected successfully');
} catch (error) {
console.error('MongoDB connection error:', error);
throw error;
}
};

0 comments on commit 892ec0a

Please sign in to comment.