import { r as resolveAccess } from './share-auth-BS7JuiHf.js';
import { t as toErrorMessage } from './error-DDXB3duW.js';
import { j as json } from './index-CoD1IJuy.js';
import { mkdirSync, writeFileSync } from 'fs';
import './auth-DuunT7Cg.js';
import './shared-server-DaWdgxVh.js';
import 'crypto';
import './share-store-B9jMpVg0.js';
import 'better-sqlite3';
import 'path';

const POST = async ({ params, request }) => {
  const access = resolveAccess(request, params.id);
  if (!access) {
    return json({ error: "Unauthorized" }, { status: 401 });
  }
  if (access.level === "guest" && access.mode !== "control") {
    return json({ error: "View-only access" }, { status: 403 });
  }
  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-QN-Bo5ql.js.map
