← back to Dear Bubbe Nextjs
app/api/dating-photo/route.ts
91 lines
import { NextRequest, NextResponse } from 'next/server'
import { writeFile, mkdir } from 'fs/promises'
import { join } from 'path'
import { existsSync } from 'fs'
export async function POST(request: NextRequest) {
try {
const formData = await request.formData()
const file = formData.get('image') as File
const userId = formData.get('userId') as string
const description = formData.get('description') as string || 'No description provided'
if (!file) {
return NextResponse.json(
{ error: 'No file provided' },
{ status: 400 }
)
}
// Validate file type
if (!file.type.startsWith('image/')) {
return NextResponse.json(
{ error: 'File must be an image' },
{ status: 400 }
)
}
// Create uploads directory if it doesn't exist
const uploadsDir = join(process.cwd(), 'uploads', 'dating')
if (!existsSync(uploadsDir)) {
await mkdir(uploadsDir, { recursive: true })
}
// Convert file to buffer
const bytes = await file.arrayBuffer()
const buffer = Buffer.from(bytes)
// Save with unique filename
const timestamp = Date.now()
const filename = `dating_${userId}_${timestamp}.jpg`
const path = join(uploadsDir, filename)
await writeFile(path, buffer)
console.log('[DATING_PHOTO] Image uploaded:', filename, 'Description:', description)
// Generate Bubbe's response based on the upload
const responses = [
"Oy vey, let me get my glasses... So THIS is who you're dating? Nu, tell me EVERYTHING!",
"A picture! Finally! Now, let's talk... How long have you been seeing this one?",
"Hmm, interesting choice, bubbeleh. Tell me, what does this person do for a living?",
"So this is the one keeping you from calling your Bubbe? They better be worth it!",
"Nice looking, I suppose. But can they cook? Do they treat you right? THESE are the important questions!"
]
const questions = [
"What's their name and how did you meet?",
"How old are they? What do they do for work?",
"Have they met your family yet? What are their intentions?",
"Are they Jewish? Not that it matters - love is love - but I'm curious!",
"Do they want kids? Marriage? What's the timeline here?",
"How do they treat you? Do they make you laugh?",
"What do YOUR friends think? Be honest!",
"Are they good to their mother? This tells you everything!",
"Do they have their life together? Stable job? Good credit?",
"Most importantly - do they make you happy?"
]
const randomResponse = responses[Math.floor(Math.random() * responses.length)]
const randomQuestions = questions
.sort(() => Math.random() - 0.5)
.slice(0, 3)
.join(' ')
const bubbeResponse = `${randomResponse} ${randomQuestions} And remember, bubbeleh - love is love! Jewish, not Jewish, converted, allied with our people - what matters is that they respect you and our culture. If they're good to you and support who you are, that's what counts!`
return NextResponse.json({
success: true,
filename,
message: 'Dating photo uploaded successfully',
bubbeResponse,
questions: randomQuestions
})
} catch (error) {
console.error('[DATING_PHOTO] Error uploading:', error)
return NextResponse.json(
{ error: 'Upload failed: ' + (error as Error).message },
{ status: 500 }
)
}
}