feat(draft): /draft/history 与 /draft/load?ts= 历史版本端点,save 留档20份(同秒覆盖去重)
This commit is contained in:
1 parent
7b1030b17e
commit
b8138151ec
1 file changed
+82
-7
@@ -2420,6 +2420,9 @@ class Handler(BaseHTTPRequestHandler):
|
||||
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
|
||||
@@ -3896,9 +3899,71 @@ class Handler(BaseHTTPRequestHandler):
|
||||
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 "")
|
||||
@@ -3906,15 +3971,25 @@ class Handler(BaseHTTPRequestHandler):
|
||||
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
|
||||
if os.path.isfile(path):
|
||||
items = []
|
||||
hpath = path[:-5] + ".hist.json"
|
||||
if os.path.isfile(hpath):
|
||||
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)}
|
||||
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):
|
||||
data = None
|
||||
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": data}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||
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()
|
||||
|
||||
Reference in new issue
Block a user