import sharp from "sharp";
const MAX_IMAGE_SOURCE_BYTES = 25 * 1024 * 1024;
const MAX_IMAGE_SOURCE_PIXELS = 4e7;
const allowedImageMimeTypes = /* @__PURE__ */ new Set([
  "image/jpeg",
  "image/png",
  "image/gif",
  "image/webp"
]);
const mimeAliases = /* @__PURE__ */ new Map([
  ["image/jpg", "image/jpeg"],
  ["image/pjpeg", "image/jpeg"],
  ["image/x-png", "image/png"]
]);
function normalizeMimeType(mimeType) {
  if (typeof mimeType !== "string") return null;
  const normalized = mimeType.split(";")[0]?.trim().toLowerCase() ?? "";
  if (!normalized) return null;
  return mimeAliases.get(normalized) ?? normalized;
}
function detectImageMimeType(buffer) {
  if (buffer.length >= 3 && buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255) {
    return "image/jpeg";
  }
  if (buffer.length >= 8 && buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71 && buffer[4] === 13 && buffer[5] === 10 && buffer[6] === 26 && buffer[7] === 10) {
    return "image/png";
  }
  if (buffer.length >= 6 && buffer[0] === 71 && buffer[1] === 73 && buffer[2] === 70 && buffer[3] === 56 && (buffer[4] === 55 || buffer[4] === 57) && buffer[5] === 97) {
    return "image/gif";
  }
  if (buffer.length >= 12 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
    return "image/webp";
  }
  return null;
}
function validateImageMagicBytes(buffer, declaredMimeType) {
  if (buffer.length > MAX_IMAGE_SOURCE_BYTES) {
    return { ok: false, status: 413, error: "Image exceeds source byte limit" };
  }
  const detectedMimeType = detectImageMimeType(buffer);
  if (!detectedMimeType || !allowedImageMimeTypes.has(detectedMimeType)) {
    return { ok: false, status: 400, error: "Unsupported image format" };
  }
  const normalizedDeclaredMimeType = normalizeMimeType(declaredMimeType);
  if (!normalizedDeclaredMimeType || !allowedImageMimeTypes.has(normalizedDeclaredMimeType)) {
    return { ok: false, status: 400, error: "Unsupported media type" };
  }
  if (normalizedDeclaredMimeType !== detectedMimeType) {
    return { ok: false, status: 400, error: "Image MIME type does not match file content" };
  }
  return { ok: true, mimeType: detectedMimeType };
}
async function validateImageDimensions(buffer) {
  let metadata;
  try {
    metadata = await sharp(buffer, {
      failOn: "error",
      limitInputPixels: MAX_IMAGE_SOURCE_PIXELS
    }).metadata();
  } catch {
    return { ok: false, status: 400, error: "Invalid image content" };
  }
  const width = metadata.width ?? 0;
  const height = metadata.height ?? 0;
  if (width <= 0 || height <= 0) {
    return { ok: false, status: 400, error: "Invalid image dimensions" };
  }
  if (width * height > MAX_IMAGE_SOURCE_PIXELS) {
    return { ok: false, status: 413, error: "Image exceeds pixel limit" };
  }
  return { ok: true, mimeType: metadata.format ? `image/${metadata.format}` : "image/jpeg" };
}
export {
  MAX_IMAGE_SOURCE_BYTES,
  MAX_IMAGE_SOURCE_PIXELS,
  detectImageMimeType,
  validateImageDimensions,
  validateImageMagicBytes
};
//# sourceMappingURL=imageSafety.js.map
