feat(autosave): 找回历史版本——「找回」面板一键恢复任意保存档
- AutosaveBar 新增「找回」按钮与历史面板:列出该工具近20份保存留档(时间精确到秒),点任意一条立即恢复成该版继续编辑 - autosave.ts 导出 loadHistory/restoreVersion - relay 新增 /draft/history 与 /draft/load?ts= 端点;save 落盘留档20份,同秒连续保存只覆盖不重复占档 - README 自动保存章节补「找回历史版本」;Home NEW_FEATURES 置顶条目 - 同步 in-repo relay 副本 server.py
This commit is contained in:
1 parent
ce512f1966
commit
9cbec90347
5 files changed
+352
-13
No files matched your search
@@ -2417,6 +2417,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if parsed.path == "/ai/img-history":
|
||||
self._handle_img_history()
|
||||
return
|
||||
if parsed.path == "/draft/load":
|
||||
self._handle_draft_load(parsed)
|
||||
return
|
||||
if parsed.path == "/draft/history":
|
||||
self._handle_draft_history(parsed)
|
||||
return
|
||||
if parsed.path.startswith("/ai/img/"):
|
||||
self._handle_img_file(parsed)
|
||||
return
|
||||
@@ -3435,6 +3441,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if parsed.path == "/ai/img-save":
|
||||
self._handle_img_save()
|
||||
return
|
||||
if parsed.path == "/draft/save":
|
||||
self._handle_draft_save()
|
||||
return
|
||||
if parsed.path == "/draft/delete":
|
||||
self._handle_draft_delete()
|
||||
return
|
||||
if parsed.path == "/ai/edit-image":
|
||||
self._handle_edit_image()
|
||||
return
|
||||
@@ -3850,6 +3862,150 @@ class Handler(BaseHTTPRequestHandler):
|
||||
log_usage(machine, tool)
|
||||
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"img_url": img_url}}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
|
||||
# ---------- 全站草稿自动保存(每 1 分钟 / 立即保存按钮 / 离开拦截)----------
|
||||
@staticmethod
|
||||
def _draft_path(machine: str, tool: str) -> str:
|
||||
"""校验并映射到 DATA_DIR/drafts/<machine>/<tool>.json;非法返回空串。"""
|
||||
m = MACHINE_RE.match(machine or "")
|
||||
t = re.sub(r"[^A-Za-z0-9_-]", "", tool or "")[:64]
|
||||
if not m or not t:
|
||||
return ""
|
||||
return os.path.join(DATA_DIR, "drafts", m.group(0), t + ".json")
|
||||
|
||||
def _handle_draft_save(self):
|
||||
payload = self._read_payload()
|
||||
machine = str(payload.get("machine") or "")
|
||||
tool = str(payload.get("tool") or "")
|
||||
path = self._draft_path(machine, tool)
|
||||
if not path:
|
||||
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
return
|
||||
state = payload.get("state")
|
||||
try:
|
||||
raw = json.dumps(state, ensure_ascii=False)
|
||||
except (TypeError, ValueError):
|
||||
self._reply(400, json.dumps({"code": 400, "message": "bad state"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
return
|
||||
if len(raw.encode("utf-8")) > 300 * 1024:
|
||||
self._reply(413, json.dumps({"code": 413, "message": "草稿超过 300KB 限制"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
return
|
||||
try:
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
rec = {"tool": tool[:64], "machine": machine, "ts": int(time.time()), "state": state}
|
||||
tmp = path + ".tmp"
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(rec, f, ensure_ascii=False)
|
||||
os.replace(tmp, path)
|
||||
except OSError:
|
||||
self._reply(500, json.dumps({"code": 500, "message": "保存失败"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
return
|
||||
# 追加历史版本(保留最近 20 版,供前端「找回」面板选择回填)
|
||||
try:
|
||||
hpath = path[:-5] + ".hist.json"
|
||||
hist = []
|
||||
if os.path.isfile(hpath):
|
||||
with open(hpath, "r", encoding="utf-8") as f:
|
||||
loaded = json.load(f)
|
||||
if isinstance(loaded, list):
|
||||
hist = [h for h in loaded if isinstance(h, dict)]
|
||||
# 连续保存相同内容不重复入档(只更新最新一条时间戳)
|
||||
if hist and hist[0].get("state") == state:
|
||||
hist[0]["ts"] = rec["ts"]
|
||||
elif hist and hist[0].get("ts") == rec["ts"]:
|
||||
# 同一秒内的连续保存:按最新内容覆盖,避免出现两条同时间条目
|
||||
hist[0] = {"ts": rec["ts"], "state": state}
|
||||
else:
|
||||
hist.insert(0, {"ts": rec["ts"], "state": state})
|
||||
hist = hist[:20]
|
||||
tmp2 = hpath + ".tmp"
|
||||
with open(tmp2, "w", encoding="utf-8") as f:
|
||||
json.dump(hist, f, ensure_ascii=False)
|
||||
os.replace(tmp2, hpath)
|
||||
except (OSError, ValueError, TypeError):
|
||||
pass # 历史写入失败不影响主保存
|
||||
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"ts": rec["ts"]}}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
|
||||
def _handle_draft_load(self, parsed):
|
||||
qs = parse.parse_qs(parsed.query)
|
||||
machine = str((qs.get("machine") or [""])[0] or "")
|
||||
tool = str((qs.get("tool") or [""])[0] or "")
|
||||
try:
|
||||
want_ts = int((qs.get("ts") or ["0"])[0] or 0)
|
||||
except ValueError:
|
||||
want_ts = 0
|
||||
path = self._draft_path(machine, tool)
|
||||
if not path:
|
||||
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
return
|
||||
data = None
|
||||
# 指定 ts:从历史版本里找回该条
|
||||
if want_ts > 0:
|
||||
hpath = path[:-5] + ".hist.json"
|
||||
if os.path.isfile(hpath):
|
||||
try:
|
||||
with open(hpath, "r", encoding="utf-8") as f:
|
||||
hist = json.load(f)
|
||||
if isinstance(hist, list):
|
||||
for h in hist:
|
||||
if isinstance(h, dict) and int(h.get("ts") or 0) == want_ts:
|
||||
data = {"state": h.get("state"), "ts": want_ts}
|
||||
break
|
||||
except (OSError, ValueError, TypeError):
|
||||
data = None
|
||||
else:
|
||||
if os.path.isfile(path):
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
rec = json.load(f)
|
||||
data = {"state": rec.get("state"), "ts": int(rec.get("ts") or 0)}
|
||||
except (OSError, ValueError):
|
||||
data = None
|
||||
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": data}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
|
||||
def _handle_draft_history(self, parsed):
|
||||
"""历史版本清单(只回 ts/bytes,不回 state,面板按需逐条取)。"""
|
||||
qs = parse.parse_qs(parsed.query)
|
||||
machine = str((qs.get("machine") or [""])[0] or "")
|
||||
tool = str((qs.get("tool") or [""])[0] or "")
|
||||
path = self._draft_path(machine, tool)
|
||||
if not path:
|
||||
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
return
|
||||
items = []
|
||||
hpath = path[:-5] + ".hist.json"
|
||||
if os.path.isfile(hpath):
|
||||
try:
|
||||
with open(hpath, "r", encoding="utf-8") as f:
|
||||
hist = json.load(f)
|
||||
if isinstance(hist, list):
|
||||
for h in hist:
|
||||
if not isinstance(h, dict):
|
||||
continue
|
||||
st = h.get("state")
|
||||
try:
|
||||
b = len(json.dumps(st, ensure_ascii=False).encode("utf-8")) if st is not None else 0
|
||||
except (TypeError, ValueError):
|
||||
b = 0
|
||||
items.append({"ts": int(h.get("ts") or 0), "bytes": b})
|
||||
except (OSError, ValueError):
|
||||
items = []
|
||||
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"items": items}}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
|
||||
def _handle_draft_delete(self):
|
||||
payload = self._read_payload()
|
||||
machine = str(payload.get("machine") or "")
|
||||
tool = str(payload.get("tool") or "")
|
||||
path = self._draft_path(machine, tool)
|
||||
if not path:
|
||||
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
return
|
||||
try:
|
||||
if os.path.isfile(path):
|
||||
os.remove(path)
|
||||
except OSError:
|
||||
pass
|
||||
self._reply(200, json.dumps({"code": 200, "message": "ok"}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
|
||||
def _handle_img_history(self):
|
||||
parsed = parse.urlparse(self.path)
|
||||
qs = parse.parse_qs(parsed.query)
|
||||
|
||||
Reference in new issue
Block a user