Files
html/api/ambre-doc-gen.php
opus 511b5dcb6f
Some checks failed
WEVAL NonReg / nonreg (push) Has been cancelled
auto-sync via WEVIA git_sync_all intent 2026-04-21T15:24:47+02:00
2026-04-21 15:24:48 +02:00

89 lines
2.9 KiB
PHP

<?php
/**
* ambre-doc-gen.php · AMBRE · real file generation via pandoc
* Usage: POST {type: pdf|docx|pptx|html, title: "...", content: "markdown..."}
* Output: {ok:true, url:"/generated/xxx.ext", size:B, type:..., ts:...}
* Doctrine: zero fake (réelle génération), zero écrasement (unique timestamp per file)
*/
header("Content-Type: application/json");
// === Input parsing ===
$raw = file_get_contents("php://input");
$in = json_decode($raw, true);
if (!$in && !empty($_POST)) $in = $_POST;
$type = strtolower(trim($in["type"] ?? "pdf"));
$title = trim($in["title"] ?? "Document WEVIA");
$content = $in["content"] ?? "";
if (!$content) {
http_response_code(400);
echo json_encode(["ok"=>false, "error"=>"content required"]);
exit;
}
// === Type validation ===
$allowed = ["pdf","docx","pptx","html","odt","epub"];
if (!in_array($type, $allowed)) {
http_response_code(400);
echo json_encode(["ok"=>false, "error"=>"invalid type", "allowed"=>$allowed]);
exit;
}
// === Prepare output dir ===
$outdir = "/var/www/html/generated";
if (!is_dir($outdir)) @mkdir($outdir, 0755, true);
$ts = date("Ymd-His");
$rand = substr(md5(random_bytes(8)), 0, 6);
$safe_title = preg_replace("/[^a-zA-Z0-9\-_]/", "-", substr($title, 0, 40));
$basename = "wevia-{$safe_title}-{$ts}-{$rand}";
$md_path = "$outdir/$basename.md";
$out_path = "$outdir/$basename.$type";
// === Write markdown input ===
$md_content = "# $title\n\n" . $content;
file_put_contents($md_path, $md_content);
// === Generate via pandoc ===
$start = microtime(true);
if ($type === "pdf") {
// Use wkhtmltopdf via pandoc for better rendering
$cmd = "pandoc " . escapeshellarg($md_path) . " --pdf-engine=wkhtmltopdf -o " . escapeshellarg($out_path) . " 2>&1";
} else if ($type === "pptx" || $type === "docx" || $type === "odt" || $type === "html" || $type === "epub") {
$cmd = "pandoc " . escapeshellarg($md_path) . " -o " . escapeshellarg($out_path) . " 2>&1";
}
$cmd_output = @shell_exec("timeout 30 $cmd");
$elapsed = round((microtime(true) - $start) * 1000);
// === Check result ===
if (!file_exists($out_path) || filesize($out_path) === 0) {
@unlink($md_path);
echo json_encode([
"ok" => false,
"error" => "pandoc failed",
"pandoc_output" => $cmd_output,
"cmd" => $cmd,
"elapsed_ms" => $elapsed,
]);
exit;
}
$size = filesize($out_path);
$url = "/generated/$basename.$type";
// Keep md source for provenance
echo json_encode([
"ok" => true,
"url" => $url,
"full_url" => "https://weval-consulting.com$url",
"size" => $size,
"size_human" => $size > 1024 ? round($size/1024, 1) . "KB" : "$size B",
"type" => $type,
"title" => $title,
"elapsed_ms" => $elapsed,
"md_source" => "/generated/$basename.md",
"ts" => date("c"),
"engine" => $type === "pdf" ? "pandoc+wkhtmltopdf" : "pandoc",
], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);