import { v as validateAuth } from './auth-DuunT7Cg.js';
import { t as toErrorMessage } from './error-DDXB3duW.js';
import { j as json } from './index-CoD1IJuy.js';
import { mkdirSync, writeFileSync } from 'fs';
import './shared-server-DaWdgxVh.js';
import 'crypto';

const POST = async ({ params, request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  const terminalId = params.id;
  if (!/^[A-Za-z0-9_-]+$/.test(terminalId)) {
    return json({ error: "Invalid terminal ID" }, { status: 400 });
  }
  let body;
  try {
    body = await request.json();
  } catch {
    return json({ error: "Invalid JSON" }, { status: 400 });
  }
  if (!body.image || typeof body.image !== "string") {
    return json({ error: "image (base64) is required" }, { status: 400 });
  }
  let imageBuffer;
  try {
    const base64Data = body.image.replace(/^data:image\/\w+;base64,/, "");
    const decoded = Buffer.from(base64Data, "base64");
    if (decoded.length === 0 || decoded.toString("base64") !== base64Data) {
      return json({ error: "Invalid base64 image data" }, { status: 400 });
    }
    imageBuffer = decoded;
  } catch {
    return json({ error: "Invalid base64 image data" }, { status: 400 });
  }
  const clipboardDir = `/tmp/shooter-clipboard-${terminalId}`;
  const imagePath = `${clipboardDir}/image.png`;
  try {
    mkdirSync(clipboardDir, { recursive: true });
    writeFileSync(imagePath, imageBuffer);
  } catch (err) {
    console.error("[paste-image] Failed to write image:", toErrorMessage(err));
    return json({ error: "Failed to write image" }, { status: 500 });
  }
  return json({ size: imageBuffer.length, success: true });
};

export { POST };
//# sourceMappingURL=_server.ts-Bjbr7glm.js.map
