import { c as createError } from '../nitro/nitro.mjs';

const MIME_TYPE_BY_EXTENSION = {
  txt: "text/plain",
  jpg: "image/jpeg",
  jpeg: "image/jpeg",
  png: "image/png",
  gif: "image/gif",
  webp: "image/webp",
  svg: "image/svg+xml",
  pdf: "application/pdf",
  doc: "application/msword",
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  mp4: "video/mp4",
  webm: "video/webm",
  mp3: "audio/mpeg",
  wav: "audio/wav",
  zip: "application/zip"
};
function detectUploadMimeType(filename) {
  var _a, _b;
  const ext = (_a = filename.split(".").pop()) == null ? void 0 : _a.toLowerCase();
  return ext ? (_b = MIME_TYPE_BY_EXTENSION[ext]) != null ? _b : "application/octet-stream" : "application/octet-stream";
}
function isUploadMimeTypeAllowed(mimeType, allowedMimeTypes) {
  if (allowedMimeTypes.length === 0) {
    return true;
  }
  return allowedMimeTypes.some((allowed) => {
    if (allowed.endsWith("/*")) {
      return mimeType.startsWith(allowed.slice(0, -2));
    }
    return mimeType === allowed;
  });
}
function assertUploadFileAccepted({
  allowedMimeTypes,
  filename,
  maxUploadSize,
  mimeType,
  size
}) {
  const fileLabel = filename ? `File "${filename}"` : "File";
  if (size > maxUploadSize) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: `${fileLabel} exceeds maximum size of ${Math.round(maxUploadSize / 1024 / 1024)}MB`
    });
  }
  if (!isUploadMimeTypeAllowed(mimeType, allowedMimeTypes)) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: `File type "${mimeType}" is not allowed. Allowed types: ${allowedMimeTypes.join(", ")}`
    });
  }
}

export { assertUploadFileAccepted as a, detectUploadMimeType as d };
//# sourceMappingURL=media-upload-validation.mjs.map
