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),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user