lumigather 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +104 -2
  2. package/bin/lumigather.js +54 -0
  3. package/package.json +32 -4
  4. package/server/app.js +243 -0
  5. package/server/assemble/fragment-assembler.js +258 -0
  6. package/server/auth/auth.js +145 -0
  7. package/server/boot.cjs +22 -0
  8. package/server/config/brand.js +157 -0
  9. package/server/config/config-loader.js +70 -0
  10. package/server/config.schema.json +12 -0
  11. package/server/core/app-log.js +59 -0
  12. package/server/core/app.js +270 -0
  13. package/server/core/index.js +47 -0
  14. package/server/http/fs-async.js +74 -0
  15. package/server/http/html-utils.js +12 -0
  16. package/server/http/http-utils.js +146 -0
  17. package/server/http/path-safe.js +96 -0
  18. package/server/http/require-sibling.js +79 -0
  19. package/server/lib/archive.js +414 -0
  20. package/server/lib/auto-update-params.json +12 -0
  21. package/server/lib/auto-update.js +27 -0
  22. package/server/lib/cjs-bootstrap.cjs +27 -0
  23. package/server/lib/config.js +298 -0
  24. package/server/lib/env.js +73 -0
  25. package/server/lib/gif.js +363 -0
  26. package/server/lib/index-cache.js +169 -0
  27. package/server/lib/index-store.js +566 -0
  28. package/server/lib/io-monitor.js +219 -0
  29. package/server/lib/paginate.js +49 -0
  30. package/server/lib/paths.js +33 -0
  31. package/server/lib/rule/flip-rule.js +88 -0
  32. package/server/lib/rule/gif-rule.js +151 -0
  33. package/server/lib/scan.js +128 -0
  34. package/server/lib/util.js +220 -0
  35. package/server/public/api-client.js +156 -0
  36. package/server/public/app.js +2492 -0
  37. package/server/public/auto-update-card.js +159 -0
  38. package/server/public/favicon.png +0 -0
  39. package/server/public/favicon.svg +18 -0
  40. package/server/public/fragments/styles/base.css +56 -0
  41. package/server/public/fragments/styles/gallery.css +203 -0
  42. package/server/public/fragments/styles/lightbox.css +81 -0
  43. package/server/public/fragments/styles/settings.css +176 -0
  44. package/server/public/fragments/styles/sidebar.css +48 -0
  45. package/server/public/fragments/styles/themes/dark.css +16 -0
  46. package/server/public/fragments/styles/toast.css +22 -0
  47. package/server/public/fragments/styles/topbar.css +55 -0
  48. package/server/public/fragments/styles/upload.css +29 -0
  49. package/server/public/index.html +204 -0
  50. package/server/public/locales/en.json +79 -0
  51. package/server/public/locales/zh-CN.json +79 -0
  52. package/server/public/style.css +13 -0
  53. package/server/public/theme-init.js +7 -0
  54. package/server/route/route-core.js +81 -0
  55. package/server/route/route-factory.js +98 -0
  56. package/server/route/route-registry.js +94 -0
  57. package/server/route/routes-adapter.js +65 -0
  58. package/server/route/routes-auth.js +81 -0
  59. package/server/route/routes-auto-update.js +87 -0
  60. package/server/routes/archive.js +96 -0
  61. package/server/routes/auth.js +44 -0
  62. package/server/routes/auto-update.js +37 -0
  63. package/server/routes/browse.js +326 -0
  64. package/server/routes/favorites.js +63 -0
  65. package/server/routes/lumigather.js +65 -0
  66. package/server/routes/upload.js +123 -0
  67. package/server/server.log +100 -0
  68. package/server/store/data-backup.js +214 -0
  69. package/server/store/json-dir.js +61 -0
  70. package/server/store/marker-manifest.js +220 -0
  71. package/server/tool/tool-detect.js +198 -0
  72. package/server/update/apply-staged-update.cjs +88 -0
  73. package/server/update/auto-update.js +812 -0
  74. package/server/update/tree-copy.js +93 -0
package/README.md CHANGED
@@ -1,3 +1,105 @@
1
- # Temporary Holding Version
1
+ # Lumigather · 拾光集
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 局域网里的 Mod 图片画廊:按目录浏览图片,压缩包内图片直出,序列帧自动合成 GIF。
4
+
5
+ ## 功能
6
+
7
+ | 功能 | 做什么 | 入口 |
8
+ | --- | --- | --- |
9
+ | 目录浏览 | 按文件系统层级浏览图片,侧边栏目录树、面包屑导航 | 页面 |
10
+ | 分类筛选 | 按配置的分类过滤图片;支持搜索与排序(名称/数量/时间) | 页面 |
11
+ | 压缩包直出 | 不解压,直接显示 zip/7z/rar 内的图片 | `/api/zip/img` |
12
+ | 贴图翻转 | 压缩包含 ini 时,被 ini 声明的贴图自动 180° 翻转 | `/api/zip/img` |
13
+ | 序列帧合成 | 按包名间隔或 ini 的 `$speed` 换算帧率,合成 GIF | `/api/zip/gif`、`/api/gif` |
14
+ | 上传 | 上传图片到指定目录 | `/api/upload` |
15
+ | 收藏 | 收藏图片与目录,按分类归组 | `/api/favorites` |
16
+ | 多语言 | 中英切换,语言包随页面加载 | 页面 |
17
+ | 设置页 | 标题、图标、分类、目录、访问密码、自动更新 | 页面 |
18
+ | 自动更新 | 定时从 GitHub 拉取新版并重启 | `/api/auto-update/status` |
19
+ | 目录索引缓存 | 目录索引常驻内存、按 mtime 失效;命中率可查 | `/api/index-cache` |
20
+ | 结果分页 | 图片列表支持 `limit`/`offset`,默认返回全量 | `/api/images` |
21
+
22
+ ## 环境要求
23
+
24
+ | 项目 | 要求 |
25
+ | --- | --- |
26
+ | 运行时 | Node.js 18 或更高;零第三方依赖,不需要安装依赖 |
27
+ | 平台 | Linux / macOS / Windows |
28
+ | 外部工具 | `ffmpeg`(GIF 合成)、`7z`(压缩包读取)——运行时自动探测,不随程序分发 |
29
+
30
+ ## 用法
31
+
32
+ ### 启动
33
+
34
+ ```bash
35
+ ./start.sh start # 后台启动
36
+ ./start.sh status # 查看运行状态
37
+ ./start.sh restart # 重启
38
+ ./start.sh stop # 停止
39
+ node server/boot.cjs # 前台直接启动(调试用)
40
+ ```
41
+
42
+ 启动后访问 `http://<NAS-IP>:<端口>/`,端口见启动日志与设置页。
43
+
44
+ ### 面板与设置页
45
+
46
+ | 操作 | 说明 |
47
+ | --- | --- |
48
+ | 分类按钮 | 切换分类;「全部」显示未分类内容 |
49
+ | 侧边栏 | 目录树,按目录跳转 |
50
+ | 搜索框 | 在当前分类内按文件名搜索 |
51
+ | 排序按钮 | 名称 / 数量 / 时间 |
52
+ | 设置 → 标题与图标 | 修改站点标题、favicon(Emoji 或图片 URL);**设置值优先**,没设时用品牌默认(`BRAND_*` 环境变量 → `.env` → `assemble.json` 的 `brand` 段 → 内置默认)。Emoji 只用于顶栏图标,不写进 `<link rel="icon">` |
53
+ | 设置 → 分类 | 增删分类,设置颜色与显示名 |
54
+ | 设置 → 目录 | 添加图片根目录,指定所属分类 |
55
+ | 设置 → 密码 | 设置或修改访问密码(设置页需登录) |
56
+ | 设置 → 自动更新 | 开关、模式、间隔,可立即检查更新 |
57
+
58
+ ### HTTP API
59
+
60
+ | 端点 | 方法 | 说明 |
61
+ | --- | --- | --- |
62
+ | `/api/status` | GET | 服务状态与登录态 |
63
+ | `/api/lumigather` | GET/POST | 读取或保存站点配置(`/api/gallery` 为兼容别名) |
64
+ | `/api/directories` | GET | 列出已配置的根目录 |
65
+ | `/api/dir` | GET | 列单层目录与图片 |
66
+ | `/api/images` | GET | 列图片;`root`、`path`、`recursive`、`limit`、`offset` 均可空 |
67
+ | `/api/media` | GET | 输出单个图片文件 |
68
+ | `/api/zip/scan` | GET | 列目录下的压缩包 |
69
+ | `/api/zip/img` | GET | 输出压缩包内单张图片;`path`、`file` 必填 |
70
+ | `/api/zip/gif` | GET | 压缩包内序列帧合成 GIF;`path` 必填,`dur_ms`、`fps` 可空 |
71
+ | `/api/gif` | GET | 目录内序列帧合成 GIF;`dir` 必填,`dur_ms` 可空 |
72
+ | `/api/favorites` | GET/POST/DELETE | 收藏的读取、新增、删除 |
73
+ | `/api/upload` | POST | 上传图片;`targetDir` 可空(空则落到默认上传目录) |
74
+ | `/api/index-cache` | GET | 目录索引缓存统计(命中率、驻留字节) |
75
+ | `/api/auto-update/status` | GET | 自动更新状态 |
76
+ | `/api/auto-update/check` | POST | 立即检查更新 |
77
+ | `/api/auto-update/config` | POST | 保存自动更新配置 |
78
+ | `/api/auto-update/restart` | POST | 重启服务 |
79
+ | `/api/login`、`/api/logout` | POST | 登录、登出 |
80
+ | `/api/change-password` | POST | 设置或修改访问密码 |
81
+
82
+ ```bash
83
+ # 列某个根目录下的全部图片(不传 limit = 全量)
84
+ curl 'http://<NAS-IP>:<端口>/api/images?root=/path/to/lumigather&recursive=true'
85
+
86
+ # 只取前 200 条(响应里的 count 仍是总数,returned 是本页数量)
87
+ curl 'http://<NAS-IP>:<端口>/api/images?root=/path/to/lumigather&recursive=true&limit=200&offset=0'
88
+ ```
89
+
90
+ ## 文档索引
91
+
92
+ | 文档 | 内容 |
93
+ | --- | --- |
94
+ | [架构与数据流](https://github.com/EIGHTfs/Lumigather/blob/main/docs/架构与数据流.md) | 进程角色、请求链路、数据落点 |
95
+ | [目录与索引机制](https://github.com/EIGHTfs/Lumigather/blob/main/docs/目录与索引机制.md) | 索引何时建、如何失效、分页字段 |
96
+ | [索引缓存与命中率](https://github.com/EIGHTfs/Lumigather/blob/main/docs/索引缓存与命中率.md) | 缓存上限与开关、命中率怎么查 |
97
+ | [图片扫描与IO归因](https://github.com/EIGHTfs/Lumigather/blob/main/docs/图片扫描与IO归因.md) | 哪些请求读盘、分批执行、耗时归因 |
98
+ | [图片缓存与HTTP缓存头](https://github.com/EIGHTfs/Lumigather/blob/main/docs/图片缓存与HTTP缓存头.md) | 缓存头怎么发、怎么验证生效 |
99
+ | [压缩包与GIF规则](https://github.com/EIGHTfs/Lumigather/blob/main/docs/压缩包与GIF规则.md) | 直出、翻转判定、序列帧合成判定 |
100
+ | [根目录与排除规则](https://github.com/EIGHTfs/Lumigather/blob/main/docs/根目录与排除规则.md) | 黑名单模式、排除目录、扫描范围 |
101
+ | [部署与运维](https://github.com/EIGHTfs/Lumigather/blob/main/docs/部署与运维.md) | 首次部署、日常操作、迁移核对、已知限制 |
102
+ | [开发与排障](https://github.com/EIGHTfs/Lumigather/blob/main/docs/开发与排障.md) | 改代码自检步骤、常见现象处理 |
103
+ | [代码组织与文件归属](https://github.com/EIGHTfs/Lumigather/blob/main/docs/代码组织与文件归属.md) | 各目录职责、通用件与业务代码 |
104
+ | [索引与性能实现](https://github.com/EIGHTfs/Lumigather/blob/main/docs/索引与性能实现.md) | 性能优化的实现细节、实测数据与踩坑 |
105
+ | [版本记录](https://github.com/EIGHTfs/Lumigather/blob/main/docs/CHANGELOG.md) | 各版本改动 |
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env node
2
+ // ============================================================
3
+ // npx / 全局安装入口
4
+ //
5
+ // 判据必须走 realpath:npx 与全局安装都会把 bin 变成指向 node_modules 的符号链接。
6
+ // 若用 __filename 的字面路径去推断项目根,链接场景下会指向 bin 的父目录而失败,
7
+ // 且失败方式极隐蔽 —— 进程直接退出、用户只看到"跑完什么都没发生"。
8
+ //
9
+ // 本文件只做三件事:定位项目根、补数据目录与端口、载入服务端入口。
10
+ // 不复制任何服务端逻辑,仓库态用 start.sh 启动的行为完全不受影响。
11
+ // ============================================================
12
+ "use strict";
13
+
14
+ const fs = require("fs");
15
+ const os = require("os");
16
+ const path = require("path");
17
+
18
+ // 解析符号链接后再定位项目根:npx 缓存目录、全局安装目录都是链接
19
+ const SELF = fs.realpathSync(__filename);
20
+ const PROJECT_ROOT = path.resolve(path.dirname(SELF), "..");
21
+
22
+ const argv = process.argv.slice(2);
23
+
24
+ if (argv.includes("--help") || argv.includes("-h")) {
25
+ console.log([
26
+ "用法: lumigather [--port <端口>]",
27
+ "",
28
+ " --port <端口> 监听端口(也可用环境变量 PORT,缺省由服务端决定)",
29
+ " --help 显示本帮助",
30
+ "",
31
+ "数据目录: " + (process.env.GALLERY_DATA_DIR || process.env.DATA_DIR || path.join(os.homedir(), ".lumigather")),
32
+ " 可用环境变量 GALLERY_DATA_DIR 覆盖",
33
+ ].join("\n"));
34
+ process.exit(0);
35
+ }
36
+
37
+ // 数据根:包方式安装时默认落到用户目录,避免升级时连带覆盖缓存与用户上传内容。
38
+ // 显式设置了环境变量则尊重设置(与 server/lib/paths.js 的优先级保持一致)。
39
+ if (!process.env.GALLERY_DATA_DIR && !process.env.DATA_DIR) {
40
+ process.env.GALLERY_DATA_DIR = path.join(os.homedir(), ".lumigather");
41
+ }
42
+
43
+ // 端口:--port N / --port=N 优先;未给则保留已有 PORT 环境变量,交给服务端取缺省
44
+ for (let i = 0; i < argv.length; i++) {
45
+ const a = argv[i];
46
+ if (a === "--port" && argv[i + 1]) { process.env.PORT = argv[i + 1]; break; }
47
+ if (a.startsWith("--port=")) { process.env.PORT = a.slice("--port=".length); break; }
48
+ }
49
+
50
+ // 数据目录必须存在:缓存与上传都写在它下面
51
+ fs.mkdirSync(process.env.GALLERY_DATA_DIR, { recursive: true });
52
+
53
+ // 交给服务端入口,由它负责监听、日志与优雅退出
54
+ require(path.join(PROJECT_ROOT, "server", "boot.cjs"));
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "lumigather",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "局域网 Mod 图片画廊:目录浏览、压缩包直出、序列帧合成 GIF",
5
+ "bin": {
6
+ "lumigather": "bin/lumigather.js"
7
+ },
8
+ "files": [
9
+ "bin/",
10
+ "server/",
11
+ "README.md"
12
+ ],
13
+ "engines": {
14
+ "node": ">=18"
15
+ },
16
+ "scripts": {
17
+ "start": "node bin/lumigather.js",
18
+ "prepublishOnly": "bash scripts/prepublish-guard.sh"
19
+ },
20
+ "keywords": [
21
+ "gallery",
22
+ "image-viewer",
23
+ "nas",
24
+ "mod",
25
+ "zip",
26
+ "gif"
27
+ ],
28
+ "author": "EIGHTfs",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/EIGHTfs/Lumigather/issues"
32
+ },
33
+ "homepage": "https://github.com/EIGHTfs/Lumigather#readme"
34
+ }
package/server/app.js ADDED
@@ -0,0 +1,243 @@
1
+ // ============================================================
2
+ // 拾光集 - 入口(装配层)
3
+ //
4
+ // 零依赖 HTTP 画廊服务。本文件只做「装配」,不写业务逻辑:
5
+ // ① 建配置(框架 createConfig)+ 初始化鉴权(框架 auth)
6
+ // ② 声明路由表(业务实现分布在 routes/*.js)
7
+ // ③ createServer 启动(框架负责 HTTP/静态/登录门/MIME)
8
+ //
9
+ // 目录约定:
10
+ // server/framework/ 框架(组装生成,勿手改)
11
+ // server/lib/ 本项目工具与领域逻辑(util/config/scan/archive/gif)
12
+ // server/routes/ 本项目业务路由(browse/archive/lumigather/upload/favorites)
13
+ // server/public/ 前端静态页
14
+ // tools/ ffmpeg + 7zz(外部二进制,不入库)
15
+ //
16
+ // 鉴权模型(保留原有语义,与框架默认不同):
17
+ // 浏览开放、改设置需登录。框架的登录门默认「未登录全部重定向」,
18
+ // 故把浏览类 API 与登录页放进 publicRoutes 放行,写操作用 ctx.requireAuth 单独把守。
19
+ // ============================================================
20
+ "use strict";
21
+
22
+ const path = require("path");
23
+ const fs = require("fs");
24
+
25
+ const {
26
+ createConfig,
27
+ createServer,
28
+ createRoute,
29
+ createAutoUpdate,
30
+ sendJson,
31
+ readBody,
32
+ appLog,
33
+ auth: fwAuth,
34
+ } = require("./core/index.js");
35
+
36
+
37
+
38
+ const { jsonRes } = require("./lib/util");
39
+ const { CONFIG } = require("./lib/config");
40
+ const authRoutes = require("./routes/auth");
41
+ const autoUpdateRoutes = require("./routes/auto-update");
42
+ // 自动更新实例:定义在 lib/auto-update.js(路由模块自行 require,不经 ctx)
43
+ const autoUpdate = require("./lib/auto-update");
44
+ const browse = require("./routes/browse");
45
+ const archive = require("./routes/archive");
46
+ // 站点配置路由(原 gallery.js,随项目更名改为 lumigather.js)
47
+ const lumigather = require("./routes/lumigather");
48
+ // 2026-10-12:IO 监测(读/写速率 + 按 label/调用点归因),见 lib/io-monitor.js
49
+ const ioMonitor = require("./lib/io-monitor");
50
+ const upload = require("./routes/upload");
51
+ const favorites = require("./routes/favorites");
52
+ // 品牌配置:@brand:key 占位符的取值,四级兜底(环境变量 → 项目根 .env → 清单 brand 段 → 内置默认),恒有值
53
+ const { readBrand } = require("./config/brand");
54
+
55
+ const PUBLIC = path.join(__dirname, "public");
56
+ const SESSION_HOURS = 72;
57
+
58
+ // ============================================================
59
+ // ① 配置与鉴权
60
+ // ============================================================
61
+ appLog.install();
62
+
63
+ const config = createConfig({
64
+ configFile: CONFIG,
65
+ schema: require("./config.schema.json"),
66
+ });
67
+
68
+ // 会话持久化:重启后免登录
69
+ fwAuth.init({
70
+ sessionFile: path.join(__dirname, "sessions.json"),
71
+ cookieName: "session",
72
+ });
73
+
74
+ // 鉴权路由:复用框架通用件 routes-auth.js,经 adapter 转表式
75
+ // (需 cfg,故在此装配而非 require 时)
76
+ const authTable = authRoutes.init({ cfg: config });
77
+ const authPublicTable = authTable.public;
78
+ // 自动更新路由:同样复用框架通用件(转表式)
79
+ const autoUpdateTable = autoUpdateRoutes.init({ cfg: config });
80
+
81
+ // ─── CLI:--set-password "xx" ───
82
+ const _pwdIdx = process.argv.indexOf("--set-password");
83
+ if (_pwdIdx !== -1) {
84
+ const pwd = process.argv[_pwdIdx + 1];
85
+ if (!pwd || String(pwd).length < 4) {
86
+ console.error('❌ 密码至少 4 位: node server/app.js --set-password "新密码"');
87
+ process.exit(1);
88
+ }
89
+ config.setPassword(pwd);
90
+ console.log("✓ 密码已设置(scrypt 哈希存入 config.json)");
91
+ process.exit(0);
92
+ }
93
+
94
+ // 是否已设密码:未设则浏览全开放,写操作也放行(与旧行为一致)
95
+ function needsSetup() {
96
+ const raw = config.readConfig();
97
+ return !raw.passwordHash || !raw.passwordSalt;
98
+ }
99
+
100
+ // 写操作鉴权:未设密码视为开放;已设密码则校验框架会话
101
+ // (会话签发/校验统一走框架 auth;cookie 名 session)
102
+ function requireAuth(req, res) {
103
+ if (needsSetup()) return true;
104
+ const token = fwAuth.extractToken(req);
105
+ if (token && fwAuth.isValidSession(token)) return true;
106
+ jsonRes(res, 401, { error: "unauthorized", needsLogin: true });
107
+ return false;
108
+ }
109
+
110
+ // 只判定登录态、不写响应。
111
+ // 与 requireAuth 的区别:requireAuth 失败时会直接写 401 响应,
112
+ // 不能用于「已登录则放宽、未登录则收紧」这类需要自行决定策略的场景(如目录浏览器向上浏览)。
113
+ function isAuthed(req) {
114
+ if (needsSetup()) return true;
115
+ const token = fwAuth.extractToken(req);
116
+ return !!(token && fwAuth.isValidSession(token));
117
+ }
118
+
119
+ // 所有业务路由共用的上下文(供 routes/ 下的模块取用)
120
+ const ctx = { cfg: config, auth: fwAuth, sendJson, requireAuth, SESSION_HOURS };
121
+
122
+ // ============================================================
123
+ // ② 路由表
124
+ // ============================================================
125
+ const browseRoutes = createRoute({
126
+ "GET /directories": (req, res, c) => browse.apiDirectories(c.query, res, { authed: isAuthed(req) }),
127
+ "GET /images": (req, res, c) => browse.apiImages(c.query, res),
128
+ "GET /dir": (req, res, c) => browse.apiDir(c.query, res),
129
+ "GET /media": (req, res, c) => browse.apiMedia(c.query, res, req.headers),
130
+ });
131
+
132
+ const archiveRoutes = createRoute({
133
+ "GET /zip/scan": (req, res, c) => archive.apiZipScan(c.query, res),
134
+ "GET /zip/img": (req, res, c) => archive.apiZipImg(c.query, res),
135
+ "GET /gif": (req, res, c) => archive.apiGif(c.query, res),
136
+ "GET /zip/gif": (req, res, c) => archive.apiZipGif(c.query, res),
137
+ });
138
+
139
+ const lumigatherRoutes = createRoute({
140
+ "GET /io": (req, res, c) => lumigather.apiIo(req, res, ctx),
141
+ // 站点配置:主名 /api/lumigather
142
+ "GET /lumigather": (req, res) => lumigather.apiGetConfig(req, res, ctx),
143
+ "POST /lumigather": (req, res) => lumigather.apiPostConfig(req, res, ctx),
144
+ // 兼容别名 /api/gallery:外部脚本、监控与尚未重跑的旧部署仍在调,内部转调同一处理器。
145
+ // 新代码只用 /api/lumigather;这条别名属过渡期兼容,不要在上面加新逻辑。
146
+ "GET /gallery": (req, res) => lumigather.apiGetConfig(req, res, ctx),
147
+ "POST /gallery": (req, res) => lumigather.apiPostConfig(req, res, ctx),
148
+ // 索引缓存统计:本系专属端点(index-cache 这个 lib 只有本系才有),免鉴权由下方 publicRoutes 放行
149
+ "GET /index-cache": (req, res) => lumigather.apiIndexCache(req, res, ctx),
150
+ });
151
+
152
+ // 上传路由不经 createRoute:它需要原始请求流做 multipart 解析,
153
+ // 而 createRoute 会预读 body(utf8 + JSON.parse + 10MB 上限),会破坏二进制上传。
154
+ // 故作为原生 handler 直接挂到 createServer.routes(见下方 routes 配置)。
155
+ const favRoutes = createRoute({
156
+ "GET /favorites": (req, res) => favorites.apiGetFavorites(req, res, ctx),
157
+ "POST /favorites": (req, res) => favorites.apiPostFavorite(req, res, ctx),
158
+ "DELETE /favorites": (req, res) => favorites.apiDeleteFavorite(req, res, ctx),
159
+ });
160
+
161
+ // ============================================================
162
+ // ③ 启动
163
+ // ============================================================
164
+ // schema 缺失时不静默降级:真跑到这一步说明组装没做,明确报错优于神秘 500
165
+ if (!fs.existsSync(path.join(__dirname, "config.schema.json"))) {
166
+ console.error("❌ 未找到 server/config.schema.json —— 请先在模板仓库运行 bash setup.sh --to <项目>/assemble.json 组装");
167
+ process.exit(1);
168
+ }
169
+
170
+ // HTML/CSS 片段组装:public/ 下的页面写 <!-- @frag:名称 -->、style.css 写 /* @frag:名称 */,
171
+ // 运行时由组装器从 public/fragments/ 取对应片段替换。缺这段,页面会原样输出 @frag 注释
172
+ // (页面看着「没坏」但内容全是空 / 样式全丢 —— 2026-09-28 实测踩坑:style.css 拆成
173
+ // @frag 骨架后 gallery 未接入组装器,CSS 全部丢失)。
174
+ // pages:页面名 → 框架文件;style.css 也走片段。brand:@brand:key 占位符的取值(见 ./config/brand)。
175
+ const FRAGMENT_PAGES = ["index.html", "style.css"];
176
+ const fragmentPages = {};
177
+ for (const name of FRAGMENT_PAGES) {
178
+ const f = path.join(PUBLIC, name);
179
+ if (fs.existsSync(f) && fs.statSync(f).isFile()) fragmentPages[name] = f;
180
+ }
181
+
182
+ createServer({
183
+ config,
184
+ auth: fwAuth,
185
+ publicDir: PUBLIC,
186
+ fragments: Object.keys(fragmentPages).length
187
+ ? { dir: path.join(PUBLIC, "fragments"), pages: fragmentPages, watch: true, brand: readBrand(__dirname) }
188
+ : null,
189
+ routes: [
190
+ // 鉴权路由(登录/登出/状态/改密):表内写的是【全路径】/api/login,
191
+ // 故 prefix 必须为空 —— createServer 会切掉 prefix 再进路由表匹配,
192
+ // 用 "/api" 会把 /api/login 切成 /login 导致 404(gbmd 同此写法)。
193
+ { prefix: "", handler: authPublicTable },
194
+ { prefix: "", handler: authTable },
195
+ { prefix: "/api", handler: browseRoutes },
196
+ { prefix: "/api", handler: archiveRoutes },
197
+ { prefix: "/api", handler: lumigatherRoutes },
198
+ // 上传:原生 handler(保留原始流做 multipart,勿改用 createRoute)
199
+ { prefix: "/api", handler: upload.uploadRouter },
200
+ { prefix: "/api", handler: favRoutes },
201
+ // 自动更新:表内写的是【全路径】/api/auto-update/xxx,故 prefix 为空
202
+ { prefix: "", handler: autoUpdateTable },
203
+ ],
204
+ // 浏览开放:登录门只挡写操作,这些读路径与登录页放行
205
+ publicRoutes: [
206
+ // 鉴权端点:登录/登出/状态必须免鉴权,否则未登录无法登录(死循环)
207
+ "/api/login",
208
+ "/api/logout",
209
+ "/api/status",
210
+ // 索引缓存统计:只读计数(不含路径与条目内容),浏览开放语义下也应可读
211
+ // 漏登记的表现:路由里写了 routePublic 仍返回 401——登录白名单是这一处的硬清单,两处都要加
212
+ "/api/index-cache",
213
+ // 浏览类:拾光集语义是「浏览开放、改设置需登录」
214
+ "/api/directories",
215
+ "/api/images",
216
+ "/api/dir",
217
+ "/api/media",
218
+ "/api/uploaded",
219
+ // 上传:无 targetDir 时游客可传(写入项目 uploads/),带 targetDir 才要求登录;
220
+ // 该判定由 routes/upload.js 的 requireAuth 负责,故此处须先放行框架登录门。
221
+ "/api/upload",
222
+ "/api/favorites",
223
+ // 站点配置:主名 + 兼容别名(两条都要登记,漏一条会让对应路径 401)
224
+ "/api/lumigather",
225
+ "/api/gallery",
226
+ "/api/zip/",
227
+ "/api/gif",
228
+ ],
229
+ needsSetup,
230
+ // 画廊没有登录页:登录走页面内的 🔒 弹窗(index.html 的 loginModal + POST /api/login),
231
+ // 故不声明 loginPath —— 留空表示「本服务无独立登录页」,框架不会 302 到不存在的地址。
232
+ loginPath: "",
233
+ // 页面不设登录门:游客直接进画廊看图,也能上传到「游客」分类;
234
+ // 设置 / 收藏 / 改密等写操作由各路由的 requireAuth 把关。
235
+ guestPages: true,
236
+ port: parseInt(process.env.PORT, 10) || 8081,
237
+ onReady(port) {
238
+ autoUpdate.start(config.readConfig().autoUpdate || { enabled: false });
239
+ // 2026-10-12:IO 探针(每 10s 采样 /proc/self/io,超 2MB/s 打日志并带最热调用点)
240
+ ioMonitor.start({ intervalMs: 10000, logThresholdMBps: 2 });
241
+ console.log(`🎮 Gallery running at http://localhost:${port} (局域网: http://<本机IP>:${port})`);
242
+ },
243
+ });