import { d as defineEventHandler, r as requireAuth, bT as requirePermission, c as createError, bo as getStorageProvider, G as createContentManager, j as getQuery } from '../../../../nitro/nitro.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';

const MAX_EXPIRY = 86400;
const DEFAULT_EXPIRY = 3600;
const signedUrl_get = defineEventHandler(async (event) => {
  var _a;
  await requireAuth(event);
  await requirePermission(
    event.context.user,
    "media",
    "read",
    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 storage = getStorageProvider();
  if (!storage.supportsSignedUrls()) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Current storage provider does not support signed URLs. Signed URLs are available with S3, Cloudflare R2, and Google Cloud Storage."
    });
  }
  const media = await createContentManager("media").findOne(id);
  if (!media) {
    throw createError({
      statusCode: 404,
      statusMessage: "Not Found",
      message: `Media file with ID ${id} not found`
    });
  }
  const query = getQuery(event);
  let expiresIn = DEFAULT_EXPIRY;
  if (query.expiresIn) {
    const parsed = Number(query.expiresIn);
    if (!Number.isFinite(parsed) || parsed < 1 || parsed > MAX_EXPIRY) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: `expiresIn must be between 1 and ${MAX_EXPIRY} seconds`
      });
    }
    expiresIn = Math.round(parsed);
  }
  const signedUrlOptions = {
    expiresIn
  };
  if (query.download === "true") {
    const originalName = media.originalName || media.filename || "file";
    signedUrlOptions.responseContentDisposition = `attachment; filename="${encodeURIComponent(originalName)}"`;
  }
  const url = await storage.getSignedUrl(media.path, signedUrlOptions);
  return {
    url,
    expiresIn,
    expiresAt: new Date(Date.now() + expiresIn * 1e3).toISOString()
  };
});

export { signedUrl_get as default };
//# sourceMappingURL=signed-url.get.mjs.map
