import fs from 'fs/promises'

export async function assertPdfMagicHeader(filePath) {
  const fh = await fs.open(filePath, 'r')
  try {
    const buf = Buffer.alloc(5)
    await fh.read(buf, 0, 5, 0)
    const header = buf.toString('utf8')
    if (header !== '%PDF-') {
      throw createError({ statusCode: 400, statusMessage: 'Ungültige Datei: kein PDF' })
    }
  } finally {
    await fh.close()
  }
}

export function clamp(n, min, max) {
  return Math.max(min, Math.min(max, n))
}


