rename page

This commit is contained in:
2026-05-18 12:45:57 +08:00
parent 18fc9849a3
commit f3527c889b
12 changed files with 1212 additions and 18 deletions

66
main.py
View File

@@ -263,12 +263,78 @@ def api_save() -> tuple[Response, int] | Response:
body = {k: (v[0] if isinstance(v, list) else v) for k, v in form.items()}
slug: str = str(body.get("slug", "")).strip()
if not slug:
# Fallback to query parameter if not in body (e.g. for some raw requests)
slug = request.args.get("slug", "").strip()
if not slug:
return jsonify({"error": "缺少專案識別碼 (slug)"}), 400
if not _project_dir(slug).exists():
return jsonify({"error": "專案不存在"}), 404
action = request.args.get("action", "").strip()
# ── 處理重新命名 / 複製頁面 ──
if action == "rename":
file = str(body.get("file", "")).strip()
newfile = str(body.get("newfile", "")).strip()
duplicate_str = str(body.get("duplicate", "")).strip().lower()
is_duplicate = (duplicate_str == "true")
if not file or not newfile:
return jsonify({"error": "缺少參數 file 或 newfile"}), 400
old_path = _sanitize_file_path(slug, file)
new_path = _sanitize_file_path(slug, newfile)
if old_path is None or new_path is None:
return jsonify({"error": "不合法的頁面名稱"}), 400
if not old_path.exists():
return jsonify({"error": "來源頁面不存在"}), 404
if new_path.exists() and old_path != new_path:
return jsonify({"error": "目標頁面已存在"}), 409
if is_duplicate:
shutil.copy(old_path, new_path)
msg = "頁面複製成功"
else:
old_path.rename(new_path)
msg = "頁面重新命名成功"
return jsonify({
"success": True,
"ok": True,
"message": msg,
"newfile": new_path.name,
"url": f"/sites/{slug}/{new_path.name}"
})
# ── 處理刪除頁面 ──
elif action == "delete":
file = str(body.get("file", "")).strip()
if not file:
return jsonify({"error": "缺少參數 file"}), 400
if file.lower() == "index.html":
return jsonify({"error": "無法刪除主頁 index.html"}), 400
safe_path = _sanitize_file_path(slug, file)
if safe_path is None:
return jsonify({"error": "不合法的頁面名稱"}), 400
if not safe_path.exists():
return jsonify({"error": "頁面不存在"}), 404
safe_path.unlink()
return jsonify({
"success": True,
"ok": True,
"message": "頁面已成功刪除"
})
# 1. 判斷是否為新增頁面請求 (含有 startTemplateUrl)
start_template_url = str(body.get("startTemplateUrl", "")).strip()
if start_template_url: