diff --git a/src/app/api/family-upload/route.ts b/src/app/api/family-upload/route.ts index 1eeb689..23b418c 100644 --- a/src/app/api/family-upload/route.ts +++ b/src/app/api/family-upload/route.ts @@ -3,6 +3,7 @@ import { writeFile, mkdir } from 'fs/promises' import path from 'path' import { randomUUID } from 'crypto' import { getDb } from '@/lib/db' +import sharp from 'sharp' export const runtime = 'nodejs' export const maxDuration = 60 @@ -69,12 +70,25 @@ export async function POST(req: NextRequest) { ) } - const filename = `${uploadDir}/${randomUUID()}${ext || '.bin'}` + const buffer = Buffer.from(await file.arrayBuffer()) + + // Convert HEIC/HEIF to JPEG so all browsers can display it + let finalBuffer: Buffer = buffer + let finalExt = ext + if (mimeType === 'image/heic' || mimeType === 'image/heif') { + try { + finalBuffer = Buffer.from(await sharp(buffer).jpeg({ quality: 90 }).toBuffer()) + finalExt = '.jpg' + } catch { + // Conversion failed — keep original + } + } + + const filename = `${uploadDir}/${randomUUID()}${finalExt || '.bin'}` const filePath = path.join(DATA_DIR, 'uploads', filename) await mkdir(path.dirname(filePath), { recursive: true }) - const buffer = Buffer.from(await file.arrayBuffer()) - await writeFile(filePath, buffer) + await writeFile(filePath, finalBuffer) // Build caption with uploader info let caption = `Von ${(name || 'Anonym').trim()}` diff --git a/src/app/api/files/[...path]/route.ts b/src/app/api/files/[...path]/route.ts index e63bcb1..5f0d641 100644 --- a/src/app/api/files/[...path]/route.ts +++ b/src/app/api/files/[...path]/route.ts @@ -60,7 +60,8 @@ export async function GET( fileToSendPath = jpegPath ext = '.jpg' contentType = 'image/jpeg' - } catch { + } catch (e) { + console.error('HEIC conversion failed:', e) // Conversion failed — keep original path (will likely fail to display in non-Apple browsers) } }