T-566 封面历史归档数据层
This commit is contained in:
@@ -292,6 +292,34 @@ def _now() -> str:
|
||||
return datetime.now().isoformat(timespec="seconds")
|
||||
|
||||
|
||||
def _cover_archive_timestamp() -> str:
|
||||
return datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
|
||||
|
||||
def _archive_generated_cover_file(file_path) -> Optional[str]:
|
||||
path_text = str(file_path or "").strip()
|
||||
if not path_text or not os.path.isfile(path_text):
|
||||
return None
|
||||
directory = os.path.dirname(path_text)
|
||||
filename = os.path.basename(path_text)
|
||||
stem, ext = os.path.splitext(filename)
|
||||
timestamp = _cover_archive_timestamp()
|
||||
suffix = 1
|
||||
while True:
|
||||
archive_name = f"{stem}_{timestamp}{ext}" if suffix == 1 else f"{stem}_{timestamp}_{suffix}{ext}"
|
||||
archive_path = os.path.join(directory, archive_name)
|
||||
if not os.path.exists(archive_path):
|
||||
break
|
||||
suffix += 1
|
||||
try:
|
||||
os.rename(path_text, archive_path)
|
||||
except PermissionError as exc:
|
||||
raise DbError("请先关闭正在查看的封面图片再重置") from exc
|
||||
except OSError as exc:
|
||||
raise DbError(f"归档当前封面图片失败: {exc}") from exc
|
||||
return archive_path
|
||||
|
||||
|
||||
def _db_path(path=None) -> str:
|
||||
return path or appconfig.db_path()
|
||||
|
||||
@@ -846,6 +874,38 @@ def update_generated_title(task_id, new_title, path=None, conn=None) -> None:
|
||||
(title, now, int(task_id)),
|
||||
)
|
||||
|
||||
|
||||
def update_generated_cover(task_id, new_cover_path, path=None, conn=None) -> None:
|
||||
"""Point a task at an existing local generated cover without copying the file."""
|
||||
|
||||
cover_path = str(new_cover_path or "").strip()
|
||||
if not cover_path:
|
||||
raise DbError("新封面路径不能为空")
|
||||
abs_cover_path = os.path.abspath(cover_path)
|
||||
if not os.path.isfile(abs_cover_path):
|
||||
raise DbError("新封面图片不存在")
|
||||
with _connection(conn, path) as database:
|
||||
task = get_task(task_id, conn=database)
|
||||
if task is None:
|
||||
raise DbError(f"任务不存在: {task_id}")
|
||||
if task.status == "running":
|
||||
raise DbError("任务正在运行,不能修改新封面")
|
||||
now = _now()
|
||||
with database:
|
||||
database.execute(
|
||||
"""
|
||||
UPDATE tasks
|
||||
SET new_cover_path = ?,
|
||||
stage = 'generated',
|
||||
status = 'pending',
|
||||
last_error = NULL,
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(abs_cover_path, now, int(task_id)),
|
||||
)
|
||||
|
||||
|
||||
def set_applied(task_id, committed, error=None, path=None, conn=None, step=None) -> None:
|
||||
now = _now()
|
||||
success = bool(committed) and error is None
|
||||
@@ -901,10 +961,9 @@ def reset_generated(
|
||||
if before is None:
|
||||
raise DbError(f"任务不存在: {task_id}")
|
||||
new_cover_path = before.new_cover_path
|
||||
deleted_file = None
|
||||
if delete_file and reset_cover and new_cover_path and os.path.isfile(str(new_cover_path)):
|
||||
os.remove(str(new_cover_path))
|
||||
deleted_file = new_cover_path
|
||||
archived_file = None
|
||||
if reset_cover:
|
||||
archived_file = _archive_generated_cover_file(new_cover_path)
|
||||
now = _now()
|
||||
new_title = None if reset_title else before.new_title
|
||||
new_cover = None if reset_cover else before.new_cover_path
|
||||
@@ -932,7 +991,8 @@ def reset_generated(
|
||||
"before": before,
|
||||
"after": after,
|
||||
"new_cover_path": new_cover_path,
|
||||
"deleted_file": deleted_file,
|
||||
"archived_file": archived_file,
|
||||
"deleted_file": None,
|
||||
"reset_title": bool(reset_title),
|
||||
"reset_cover": bool(reset_cover),
|
||||
}
|
||||
|
||||
+39
-1
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
from .config import make_slug
|
||||
|
||||
@@ -22,6 +23,27 @@ def task_image_path(image_root, task, account=None, suffix="old", ext=".jpg"):
|
||||
)
|
||||
|
||||
|
||||
def list_task_cover_candidates(image_root, task, account=None):
|
||||
"""Return generated cover candidates for one task from the image directory."""
|
||||
|
||||
canonical_path = task_image_path(image_root, task, account=account, suffix="new", ext=".jpg")
|
||||
directory = os.path.dirname(canonical_path)
|
||||
prefix = os.path.splitext(os.path.basename(canonical_path))[0]
|
||||
if not os.path.isdir(directory):
|
||||
return []
|
||||
candidates = []
|
||||
for filename in os.listdir(directory):
|
||||
full_path = os.path.join(directory, filename)
|
||||
if not os.path.isfile(full_path):
|
||||
continue
|
||||
stem, ext = os.path.splitext(filename)
|
||||
if ext.lower() != ".jpg":
|
||||
continue
|
||||
if stem == prefix or stem.startswith(prefix + "_"):
|
||||
candidates.append(os.path.abspath(full_path))
|
||||
return sorted(candidates, key=lambda path: _cover_candidate_sort_key(path, prefix))
|
||||
|
||||
|
||||
def _account_slug(account, task):
|
||||
slug = _get(account, "slug")
|
||||
if slug:
|
||||
@@ -32,6 +54,22 @@ def _account_slug(account, task):
|
||||
return "unknown_account"
|
||||
|
||||
|
||||
def _cover_candidate_sort_key(path, prefix):
|
||||
filename = os.path.basename(path)
|
||||
stem, _ext = os.path.splitext(filename)
|
||||
if stem == prefix:
|
||||
return (0, 0, 0, filename)
|
||||
marker = prefix + "_"
|
||||
if stem.startswith(marker):
|
||||
suffix = stem[len(marker):]
|
||||
match = re.fullmatch(r"(\d{14})(?:_(\d+))?", suffix)
|
||||
if match:
|
||||
timestamp = int(match.group(1))
|
||||
collision = int(match.group(2) or 1)
|
||||
return (1, -timestamp, -collision, filename)
|
||||
return (2, 0, 0, filename)
|
||||
|
||||
|
||||
def _get(obj, name, default=None):
|
||||
if obj is None:
|
||||
return default
|
||||
@@ -45,4 +83,4 @@ def _safe_component(value, default):
|
||||
if not text:
|
||||
text = str(default)
|
||||
safe = "".join(ch if ch.isalnum() or ch in "_-" else "_" for ch in text).strip("_")
|
||||
return safe or str(default)
|
||||
return safe or str(default)
|
||||
|
||||
Reference in New Issue
Block a user