import { d as defineEventHandler, r as requireAuth, bT as requirePermission, c as createError, G as createContentManager, bo as getStorageProvider } from '../../../../nitro/nitro.mjs';
import { g as getAuditActorFromEvent } from '../../../../_/audit-log-actor.mjs';
import { a as isProcessableImage, p as parseFocalPoint, g as generateMediaVariants, d as deleteMediaVariants, i as invalidateTransformCache } from '../../../../_/media-variants.mjs';
import { e as enrichMediaDeliveryRecord } from '../../../../_/media-delivery.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';
import 'sharp';

const regenerateVariants_post = defineEventHandler(async (event) => {
  var _a, _b;
  await requireAuth(event);
  await requirePermission(
    event.context.user,
    "media",
    "update",
    event.context.adminUser,
    event.context.apiToken
  );
  const id = Number((_a = event.context.params) == null ? void 0 : _a.id);
  if (!Number.isFinite(id) || id < 1) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Invalid media ID"
    });
  }
  const mediaManager = createContentManager("media");
  const media = await mediaManager.findOne(id);
  if (!media) {
    throw createError({
      statusCode: 404,
      statusMessage: "Not Found",
      message: `Media file with ID ${id} not found`
    });
  }
  const mimeType = typeof media.mimeType === "string" ? media.mimeType : "";
  if (!isProcessableImage(mimeType)) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Variants can be regenerated only for processable images"
    });
  }
  const path = typeof media.path === "string" ? media.path : "";
  if (!path) {
    throw createError({
      statusCode: 500,
      statusMessage: "Internal Server Error",
      message: "Media file path is invalid"
    });
  }
  const storage = getStorageProvider();
  const sourceBuffer = await storage.getFile(path);
  const focalPoint = (_b = parseFocalPoint(media.focalPoint)) != null ? _b : null;
  const previousFormats = parseFormats(media.formats);
  const nextFormats = await generateMediaVariants(sourceBuffer, path, mimeType, {
    focalPoint,
    variantSuffix: `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`
  });
  const actor = getAuditActorFromEvent(event);
  let updated;
  try {
    updated = await mediaManager.update(id, { formats: nextFormats }, void 0, { actor });
  } catch (error) {
    if (nextFormats) {
      await deleteMediaVariants(nextFormats).catch(() => {
      });
    }
    throw error;
  }
  await invalidateTransformCache(id).catch(() => {
  });
  if (previousFormats) {
    await deleteMediaVariants(previousFormats).catch(() => {
    });
  }
  const enriched = await enrichMediaDeliveryRecord(updated);
  return {
    success: true,
    data: enriched
  };
});
function parseFormats(value) {
  if (!value) {
    return null;
  }
  if (typeof value === "string") {
    try {
      return JSON.parse(value);
    } catch {
      return null;
    }
  }
  if (typeof value === "object") {
    const obj = value;
    if (obj.variants && typeof obj.variants === "object") {
      return value;
    }
  }
  return null;
}

export { regenerateVariants_post as default };
//# sourceMappingURL=regenerate-variants.post.mjs.map
