89 lines
2.9 KiB
PHP
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);
|