Fix HEIC photos not displaying on non-Apple devices
- Add HEIC-to-JPEG conversion in family-upload route (was missing, unlike admin upload) - Add error logging for HEIC conversion failures in file serving route Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -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()}`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user