Peach: Local-first video metadata scraper and media manager.
Merge branch 'master' into agent/claude/oss-init-wizard
longmeidao87b8168cf043
diff --cc README.en.mdindex e58b929b5,90737a7df..98eb8dbba--- a/README.en.md+++ b/README.en.md@@@ -100,34 -100,19 +100,35 @@@ Three steps, with no directories or con& .\.venv\Scripts\peach.exe init # macOS: ./.venv/bin/peach init```-`peach init` creates the data root, migrates the ledger to the latest schema, generates the local CA-and writes `<data root>/config.toml`. The default data root is `peach-data/` next to the repository;-`--data-root` changes it, and the `PEACH_DATA_ROOT` environment variable overrides it. After that,-`peach serve` starts the service; the listen address, port, media drive-letter mappings and-replication switches are all edited in that settings file, explained item by item in+Run without arguments, `peach init` asks five questions in the terminal; pressing Enter accepts the default shown in brackets:++| Question | Default |+| --- | --- |+| Data root (ledger, caches and the settings file live here) | `peach-data/` next to the repository |+| Local media directory (source `local`, must already exist) | `~\Videos` (`~/Movies` on macOS); required when that folder is missing |+| Listen scope: 1 = this machine only (127.0.0.1), 2 = LAN (0.0.0.0) | `1` |+| Service port | `8900` |+| LAN name (`<name>.local`, published only when listening on the LAN) | `peach` |++It then creates the data root, migrates the ledger to the latest schema, generates the local CA, writes+`<data root>/config.toml`, asks "scan <directory> now?" (default yes) to register that directory's files+in the ledger, and prints the next steps. The settings file declares only the one source you named; after+that `peach serve` starts the service and `peach scan local` rescans at any time. The listen address, port,+sources and replication switches are all edited in that settings file, explained item by item in[`docs/OPERATIONS.md`](docs/OPERATIONS.md). The tray is started with `peach-tray`, without arguments.+To skip the questions, pass arguments: `peach init --no-input` (or any single option such as `--data-root`,+`--port` or `--mount local=/mnt/media`) generates the file from the built-in defaults plus your arguments;+the same path is taken when stdin is not a terminal. A file written this way carries three example sources,+`local = R:\media`, `115 = B:/` and `pikpak = A:/`, which must be changed to your own paths under+`[media.locations]` and `[media.mounts]`; leaving them means every source is offline, not an error.+- Four facts:+ Five facts:+ - Listening on `127.0.0.1` needs no token. Reaching the service from a phone or any other device on the LAN (`--host 0.0.0.0`, and everything the tray starts) does: `peach init` has already written one to `<data root>/secrets/auth-token`, `peach token` prints it, and each device pastes it into the login page once. `peach serve` refuses to start when it binds a non-loopback address without a token, because that puts the whole collection and the write endpoints on the local network.- `-e` is a hard requirement: the wheel contains only the packages under `src/`, and the repository-root `migrations/` and `web/` are not in it, so under a non-editable install `peach init` fails outright because it cannot find the migrations directory.-- When `--data-root` points somewhere else, `peach serve` looks for the data root only via `PEACH_DATA_ROOT` and the `peach-data/` directories a few levels above the repository, so set `PEACH_DATA_ROOT` as well; the default data root needs no such step.-- The declared roots of the three sources in the settings file default to `local = R:\media`, `115 = B:/` and `pikpak = A:/`. These are example drive letters and must be changed to your own paths under `[media.locations]` and `[media.mounts]`; leaving them means every source is offline, not an error. CloudDrive is not required — any cloud drive that mounts as a local path works; 115 and PikPak are recommendations, not requirements.+- When the data root is not next to the repository, `peach serve` looks for it only via `PEACH_DATA_ROOT` and the `peach-data/` directories a few levels above the repository, so set `PEACH_DATA_ROOT` as well; the default data root needs no such step.+- Ledger paths are always in the Windows shape. On Windows the media directory goes straight into `[media.locations]`; on macOS the declared root is `R:\media` and the directory goes into `[media.mounts]`, where the local mount point does the translation. CloudDrive is not required — any cloud drive that mounts as a local path works; 115 and PikPak are recommendations, not requirements.- The interface is currently available in Chinese only.The service also starts without a settings file: `/healthz` reports `configured=false`, and the pages prompt you to run `peach init` first.diff --cc README.mdindex 5e03d61aa,0dcb32437..e72c05e93--- a/README.md+++ b/README.md@@@ -96,32 -96,17 +96,33 @@@ peach-app& .\.venv\Scripts\peach.exe init # macOS: ./.venv/bin/peach init```-`peach init` 建数据根、把账本迁到最新 schema、生成本机 CA,并写出 `<数据根>/config.toml`。-默认数据根是仓库同级的 `peach-data/`,`--data-root` 可改,环境变量 `PEACH_DATA_ROOT` 覆盖它。-之后 `peach serve` 就能起服务;监听地址、端口、媒体盘符映射和复制开关都在那个设置文件里改,-逐项说明见 [`docs/OPERATIONS.md`](docs/OPERATIONS.md)。托盘由 `peach-tray` 启动,不带参数。+不带参数的 `peach init` 在终端里问五个问题,每一题回车即接受方括号里的默认值:++| 问题 | 默认值 |+| --- | --- |+| 数据根(账本、缓存与设置文件都放这里) | 仓库同级的 `peach-data/` |+| 本地媒体目录(来源 `local`,必须已存在) | `~\Videos`(macOS 为 `~/Movies`),不存在则必填 |+| 监听范围:1 = 仅本机(127.0.0.1),2 = 局域网(0.0.0.0) | `1` |+| 服务端口 | `8900` |+| 局域网名字(`<名字>.local`,只在监听局域网时发布) | `peach` |++回答完它建数据根、把账本迁到最新 schema、生成本机 CA、写出 `<数据根>/config.toml`,然后问+「现在扫描 <目录>?」(默认是)把那个目录的文件登记进账本,最后打印下一步。设置文件里只有你+声明过的那一个来源;之后 `peach serve` 就能起服务,`peach scan local` 可以随时重扫。监听地址、+端口、来源与复制开关都在那个设置文件里改,逐项说明见 [`docs/OPERATIONS.md`](docs/OPERATIONS.md)。+托盘由 `peach-tray` 启动,不带参数。++不想问答就给参数:`peach init --no-input`(或任何一个参数,例如 `--data-root`、`--port`、+`--mount local=/mnt/media`)按内建默认与参数直接生成;stdin 不是终端时也走这条路。这条路写出的+设置文件带三个示例来源 `local = R:\media`、`115 = B:/`、`pikpak = A:/`,必须在 `[media.locations]`+与 `[media.mounts]` 里改成自己的路径,不改的结果是全部来源脱盘,而不是报错。- 四个事实:+ 五个事实:+ - 只监听 `127.0.0.1` 不需要口令。要让手机或局域网里别的设备访问(`--host 0.0.0.0`、托盘起的服务都算),就必须有访问口令:`peach init` 已经在 `<数据根>/secrets/auth-token` 生成一份,`peach token` 打印出来,设备第一次访问时贴进登录页。绑非回环地址却读不到口令时 `peach serve` 拒绝启动,因为那等于把整个馆藏和写接口摆在同网段上。- `-e` 是硬性要求:wheel 只含 `src/` 下的包,仓库根的 `migrations/` 与 `web/` 不在里面,非可编辑安装下 `peach init` 找不到迁移目录会直接报错。-- `--data-root` 指到别处时,`peach serve` 只按 `PEACH_DATA_ROOT` 和仓库上方几层的 `peach-data/` 找数据根,所以要同时设 `PEACH_DATA_ROOT`;用默认数据根没有这一步。-- 设置文件里三个来源的声明根默认是 `local = R:\media`、`115 = B:/`、`pikpak = A:/`,这只是示例盘符,必须在 `[media.locations]` 与 `[media.mounts]` 里改成自己的路径;不改的结果是全部来源脱盘,而不是报错。CloudDrive 不是必需,任何能挂成本地路径的网盘都行;115 与 PikPak 是推荐项,不是要求。+- 数据根不在仓库同级时,`peach serve` 只按 `PEACH_DATA_ROOT` 和仓库上方几层的 `peach-data/` 找数据根,所以要同时设 `PEACH_DATA_ROOT`;用默认数据根没有这一步。+- 账本里的路径一律是 Windows 形态。Windows 上媒体目录直接写进 `[media.locations]`;macOS 上声明根写 `R:\media`、目录写进 `[media.mounts]`,由本机挂载点负责翻译。CloudDrive 不是必需,任何能挂成本地路径的网盘都行;115 与 PikPak 是推荐项,不是要求。- 界面目前只有中文。没有设置文件也能启动:`/healthz` 报 `configured=false`,页面提示先跑 `peach init`。diff --cc docs/OPERATIONS.mdindex 6feece80c,75886ab7d..cf8edaa04--- a/docs/OPERATIONS.md+++ b/docs/OPERATIONS.md@@@ -9,24 -9,10 +9,24 @@@- 设置文件固定是 `<数据根>/config.toml`。数据根按三步找:环境变量 `PEACH_DATA_ROOT`、项目根同级(含上溯四层,覆盖主检出、`peach-worktrees/<任务>` 和打包后的 `dist/Peach/_internal`)的 `peach-data/`、都没有就是「未配置」。优先级是环境变量 > 设置文件 > 内建默认。-- 全新机器只跑 `peach init`:建齐 `database`/`generated`/`sources`/`state`/`secrets`/- `logs`/`tools`/`review` 八个目录、把账本迁到最新 schema、生成本机 CA、写出设置文件,- 最后打印下一步。可用 `--data-root`/`--host`/`--port`/`--mdns-name`/- `--mount local=/mnt/media` 预置值;已有账本不会被重建,它会提示改用 `--from-existing`。+- 全新机器只跑 `peach init`。不带参数且 stdin 是终端时进问答(题目、默认值与落盘逻辑在+ `src/peach/onboarding.py`,托盘设置页复用同一组函数):问数据根、一个已存在的本地媒体+ 目录、监听范围(仅本机/局域网)、端口、局域网名字,每题回车取默认,连续三次无效即退出且+ 不写任何文件。然后建齐 `database`/`generated`/`sources`/`state`/`secrets`/`logs`/- `tools`/`review` 八个目录、把账本迁到最新 schema、生成本机 CA、写出设置文件,问一句++ `tools`/`review` 八个目录、把账本迁到最新 schema、生成本机 CA 与访问口令、写出设置文件,问一句+ 「现在扫描 <目录>?」(默认是)调 `peach.scan.scan_location` 登记文件,最后打印下一步与+ 扫描摘要。写出的 `[media.locations]` 只有 `local`:Windows 上直接是那个目录、+ `[media.mounts]` 为空;macOS 上声明根是 `R:\media`、目录写进 `[media.mounts] local`。+ 复制、writer 镜像、SMB 一律不问,保持关闭或留空。+- 给了任何参数、加了 `--no-input`、或 stdin 不是终端,`peach init` 走非交互路径:按内建默认+ 与 `--data-root`/`--host`/`--port`/`--mdns-name`/`--mount local=/mnt/media` 直接生成,+ 写出的文件带 `local`/`115`/`pikpak` 三个示例声明根。已有账本不会被重建,它会提示改用+ `--from-existing`。+- `peach scan <来源ID> [根目录]` 把一个目录的文件元数据 upsert 进账本,只新增行与刷新+ `size`/`mtime`/`last_seen`,不改真相字段、不删行。根目录省略时取该来源在+ `[media.locations]` 的声明根,本机目录按 `[media.mounts]` 取;给了目录则必须落在声明根+ (macOS 上是挂载点)之内,否则拒绝——写进去的行否则翻译不回本机路径。+ `scripts/ledger.py scan` 是同一实现的薄委托。- 已经在跑的机器用 `peach init --from-existing`:只写设置文件,不建库、不动 `peach-data/`下任何现有文件。它把当前实际生效的配置原样落盘,猜不出来的坐标(局域网 writer 地址、SMB 主机与账号)留空并逐条打印出来,用 `--writer-origin`/`--smb-host`/`--smb-user`diff --cc scripts/test_runner.pyindex dfd8a948e,0774f0afa..f24fc51f6--- a/scripts/test_runner.py+++ b/scripts/test_runner.py@@@ -50,8 -50,7 +50,8 @@@ SCOPES: dict[str, tuple[str, ...]] ="test_mgstage_maker_harvest.py", "test_studio_name_variant_merge.py","test_javdb_cn_names.py","test_link_rediscovery.py", "test_link_label_owner.py"),- "tooling": ("test_scripts.py", "test_cli.py", "test_script_policy.py",+ "tooling": ("test_scripts.py", "test_auth.py", "test_cli.py", "test_script_policy.py",+ "test_scan.py", "test_onboarding.py","test_agent_worktree.py", "test_dependency_policy.py","test_restart_windows_tray.py","test_versioning.py",diff --cc src/peach/cli.pyindex af4e9aeba,1ca027055..fda9f939a--- a/src/peach/cli.py+++ b/src/peach/cli.py@@@ -6,7 -5,7 +6,7 @@@ import syfrom datetime import datetimefrom pathlib import Path- from . import certs, onboarding, scan, settings_file-from . import auth, certs, settings_file++from . import auth, certs, onboarding, scan, settings_filefrom .api import create_appfrom .config import (DATABASE_PATH,@@@ -430,6 -362,40 +452,40 @@@ def _create_data_tree(config: settings_# 没有 openssl 不该挡住初始化:HTTP 本地访问照样能用,装 openssl 后重跑即可。print(f"未生成本机 CA({exc})。装好 openssl 后重跑 `peach init --force` 补上。")+ _announce_token(config)+++ def _announce_token(config: settings_file.PeachConfig) -> str:+ """确保口令文件存在,并把路径报出来。口令本身不打印,用 `peach token` 单独看。++ 终端输出会进日志、截图和 issue,口令不该顺着这条路走出去。+ """+ secrets_dir = config.directory("secrets")- _token, created = auth.ensure_token(secrets_dir)++ token, created = auth.ensure_token(secrets_dir)+ path = auth.token_path(secrets_dir)+ print(f"访问口令:{path}" + ("" if created else "(沿用已有的那份)"))+ print("绑局域网地址的服务必须读得到它,`peach token` 看内容。")- return _token++ return token+++ def _token(args: argparse.Namespace) -> int:+ """`peach token` 打印口令,没有就补一份;`--rotate` 换一个。++ 手机和别的设备第一次访问会跳登录页,把这里打印的那串贴进去,之后走 cookie。+ """+ _require_readable_settings()+ secrets_dir = settings_file.active().directory("secrets")+ if args.rotate:+ token, fresh = auth.write_token(secrets_dir), True+ else:+ token, fresh = auth.ensure_token(secrets_dir)+ print(f"口令文件:{auth.token_path(secrets_dir)}")+ print(f"口令:{token}")+ if fresh:+ print("已经登录过的浏览器要再登录一次;正在跑的服务要重启才会读到这份口令。")+ print("两台机器互取复核结果时发的是自己的口令,reader 要用和 writer 相同的这份文件。")+ return 0+#: 留空就会改变行为的坐标。进程猜不出来,只能让人在命令行给。_BLANK_HINTS = (@@@ -463,10 -429,10 +519,17 @@@ def _print_next_steps(config: settings_if from_existing:print(" 1. 打开上面那个文件核对一遍,尤其是 [media.mounts] 和 [replication]。")print(" 2. 重启服务让它生效。这一步之前,运行行为和现在完全一样。")++ print(" 3. `peach token` 取口令:绑局域网地址的服务读不到它就起不来,"++ "已经在用的设备也要重新登录一次。")returnprint(f" 1. peach serve --host {config.server.host} --port {config.server.port}")print(" 2. 浏览器打开 http://127.0.0.1:%d/ 确认页面可用。" % config.server.port)-- print(" 3. 要在局域网访问就改 --host 0.0.0.0,并把本机 CA 装进各设备的信任列表。")++ if _is_loopback(config.server.host):++ print(" 3. 要在局域网访问就改 --host 0.0.0.0:把本机 CA 装进各设备的信任列表,"++ "再用 `peach token` 取口令,设备第一次访问时贴进登录页。")++ else:++ print(" 3. 局域网设备要装本机 CA,并用 `peach token` 取口令,"++ "第一次访问时贴进登录页。")def build_parser() -> argparse.ArgumentParser:@@@ -511,14 -474,11 +574,19 @@@serve.add_argument("--ssl-keyfile", type=Path)serve.set_defaults(handler=_serve)+ scan_parser = commands.add_parser(+ "scan", help="扫描一个来源的目录,把文件元数据写进账本")+ scan_parser.add_argument("location", help="来源 ID(asset.location),要在 [media.locations] 里声明过")+ scan_parser.add_argument("root", nargs="?",+ help="要扫的目录;省略时取该来源的声明根(本机目录按 [media.mounts] 取)")+ scan_parser.add_argument("--db", type=Path, default=DEFAULT_DB)+ scan_parser.set_defaults(handler=_scan)++ token = commands.add_parser("token", help="查看局域网访问口令,没有就生成一份")+ token.add_argument("--rotate", action="store_true",+ help="换一个新口令,已签发的 cookie 立即失效")+ token.set_defaults(handler=_token)+migrate = commands.add_parser("migrate", help="inspect or apply SQLite migrations")migrate.add_argument("action", choices=("status", "upgrade"), nargs="?", default="status")migrate.add_argument("--db", type=Path, default=DEFAULT_DB)