feat: 完成后清理进度文件,并支持迁移数据目录
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
"""把数据目录迁到用户选的位置。真正搬文件夹由桌面壳在停掉后台之后做。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from app.features.download import jobs as job_module
|
||||
from app.features.download.journal import explain_os_error
|
||||
from app.platform.client import NqiError
|
||||
from app.shared.files import write_atomic
|
||||
from app.shared.state import state
|
||||
|
||||
RESUME_NAME = "resume-after-move.json"
|
||||
|
||||
|
||||
def data_root() -> Path:
|
||||
return job_module.DATA_DIR.parent
|
||||
|
||||
|
||||
def resume_path() -> Path:
|
||||
return data_root() / RESUME_NAME
|
||||
|
||||
|
||||
def current_dir() -> dict:
|
||||
return {"path": str(data_root())}
|
||||
|
||||
|
||||
def _same(left: Path, right: Path) -> bool:
|
||||
if left.exists() and right.exists():
|
||||
try:
|
||||
return left.samefile(right)
|
||||
except OSError:
|
||||
pass
|
||||
return os.path.normcase(os.path.abspath(left)) == os.path.normcase(os.path.abspath(right))
|
||||
|
||||
|
||||
def _within(child: Path, parent: Path) -> bool:
|
||||
if _same(child, parent):
|
||||
return False
|
||||
child_text = os.path.normcase(os.path.abspath(child))
|
||||
parent_text = os.path.normcase(os.path.abspath(parent)).rstrip("\\/")
|
||||
return child_text.startswith(parent_text + os.sep)
|
||||
|
||||
|
||||
def validate_target(raw: str) -> Path:
|
||||
text = (raw or "").strip()
|
||||
if not text:
|
||||
raise NqiError("请选择数据目录")
|
||||
target = Path(text)
|
||||
if not target.is_absolute():
|
||||
raise NqiError("数据目录必须是绝对路径")
|
||||
current = data_root().resolve()
|
||||
chosen = target.resolve()
|
||||
if _same(chosen, current):
|
||||
raise NqiError("已经是当前数据目录")
|
||||
if _within(chosen, current) or _within(current, chosen):
|
||||
raise NqiError("不能选在当前数据目录里面,也不能选它的上级")
|
||||
if target.exists() and not target.is_dir():
|
||||
raise NqiError("请选择文件夹")
|
||||
if target.exists() and any(target.iterdir()):
|
||||
raise NqiError("请选择一个空文件夹")
|
||||
job_module.ensure_output_dir(target)
|
||||
return target.resolve()
|
||||
|
||||
|
||||
async def prepare(raw: str) -> dict:
|
||||
target = validate_target(raw)
|
||||
ids = await state.jobs.shelve_for_move()
|
||||
payload = json.dumps({"ids": ids}, ensure_ascii=False).encode("utf-8")
|
||||
try:
|
||||
write_atomic(resume_path(), payload)
|
||||
except OSError as exc:
|
||||
message = explain_os_error(exc) or "迁移记录写不进去"
|
||||
raise NqiError(message) from exc
|
||||
return {"path": str(data_root()), "target": str(target), "resumeIds": ids}
|
||||
|
||||
|
||||
async def resume_moved() -> dict:
|
||||
path = resume_path()
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
except FileNotFoundError:
|
||||
return {"resumed": []}
|
||||
except (OSError, json.JSONDecodeError, UnicodeError, TypeError, ValueError) as exc:
|
||||
raise NqiError("迁移记录读不出来") from exc
|
||||
try:
|
||||
path.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
except OSError as exc:
|
||||
message = explain_os_error(exc) or "迁移记录暂时删不掉"
|
||||
raise NqiError(message) from exc
|
||||
raw_ids = data.get("ids") if isinstance(data, dict) else []
|
||||
if not isinstance(raw_ids, list):
|
||||
raw_ids = []
|
||||
resumed: list[str] = []
|
||||
for job_id in raw_ids:
|
||||
text = str(job_id)
|
||||
try:
|
||||
job = state.jobs.get(text)
|
||||
except NqiError:
|
||||
continue
|
||||
if job.status == "done":
|
||||
continue
|
||||
job.paused_by = ""
|
||||
if job.status != "queued":
|
||||
job.status = "queued"
|
||||
job.message = "等待中"
|
||||
job.save()
|
||||
resumed.append(text)
|
||||
from app.features.download.router import fill_slots
|
||||
|
||||
fill_slots()
|
||||
return {"resumed": resumed}
|
||||
Reference in New Issue
Block a user