@insapp-dev/inslog 0.0.0-stage → 0.1.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.
- package/LICENSE +26 -0
- package/README.md +70 -2
- package/config/database.example.json +5 -0
- package/config/locale.example.json +3 -0
- package/locales/en.json +476 -0
- package/locales/zh-CN.json +1 -0
- package/next-env.d.ts +7 -0
- package/next.config.ts +10 -0
- package/package.json +62 -4
- package/public/apple-touch-icon.png +0 -0
- package/public/favicon.ico +0 -0
- package/public/icon-color.png +0 -0
- package/public/icon-color.svg +5 -0
- package/public/icon-dark.svg +5 -0
- package/public/icon.svg +5 -0
- package/scripts/_db.mjs +67 -0
- package/scripts/export-backup.mjs +104 -0
- package/scripts/release.sh +70 -0
- package/scripts/seed-demo.mjs +184 -0
- package/scripts/test-connection.mjs +26 -0
- package/src/app/api/auth/logout/route.ts +9 -0
- package/src/app/api/auth/passkey/add-request/route.ts +35 -0
- package/src/app/api/auth/passkey/login/options/route.ts +40 -0
- package/src/app/api/auth/passkey/login/verify/route.ts +58 -0
- package/src/app/api/auth/passkey/register/options/route.ts +46 -0
- package/src/app/api/auth/passkey/register/verify/route.ts +63 -0
- package/src/app/api/auth/passkey/remove-request/route.ts +44 -0
- package/src/app/api/auth/recovery/route.ts +45 -0
- package/src/app/api/auth/totp/disable/route.ts +42 -0
- package/src/app/api/auth/totp/enable/route.ts +75 -0
- package/src/app/api/auth/totp/login/route.ts +47 -0
- package/src/app/api/auth/totp/setup/route.ts +20 -0
- package/src/app/api/setup/complete/route.ts +27 -0
- package/src/app/api/setup/email/route.ts +41 -0
- package/src/app/api/upload/local/route.ts +73 -0
- package/src/app/api/upload/route.ts +73 -0
- package/src/app/api/upload/s3/route.ts +73 -0
- package/src/app/confirm/[token]/page.tsx +181 -0
- package/src/app/globals.css +952 -0
- package/src/app/layout.tsx +107 -0
- package/src/app/login/login-form.tsx +325 -0
- package/src/app/login/page.tsx +76 -0
- package/src/app/not-found.tsx +26 -0
- package/src/app/page.tsx +179 -0
- package/src/app/posts/[slug]/page.tsx +100 -0
- package/src/app/robots.txt/route.ts +25 -0
- package/src/app/rss.xml/route.ts +60 -0
- package/src/app/search/page.tsx +94 -0
- package/src/app/settings/actions.ts +92 -0
- package/src/app/settings/image-field.tsx +143 -0
- package/src/app/settings/language-actions.ts +28 -0
- package/src/app/settings/language-select.tsx +56 -0
- package/src/app/settings/page.tsx +97 -0
- package/src/app/settings/passkey-manager.tsx +194 -0
- package/src/app/settings/site-form.tsx +323 -0
- package/src/app/settings/theme-toggle.tsx +51 -0
- package/src/app/settings/totp-manager.tsx +239 -0
- package/src/app/setup/auth-config-panel.tsx +132 -0
- package/src/app/setup/config-actions.ts +90 -0
- package/src/app/setup/page.tsx +80 -0
- package/src/app/setup/secrets-actions.ts +49 -0
- package/src/app/setup/setup-data.ts +133 -0
- package/src/app/setup/setup-wizard.tsx +338 -0
- package/src/app/sitemap.xml/route.ts +46 -0
- package/src/app/uploads/[...path]/route.ts +64 -0
- package/src/app/write/actions.ts +85 -0
- package/src/app/write/page.tsx +48 -0
- package/src/app/write/write-client.tsx +254 -0
- package/src/components/BlockEditor.tsx +102 -0
- package/src/components/PostBody.tsx +51 -0
- package/src/components/SiteFooter.tsx +41 -0
- package/src/components/WriteFab.tsx +38 -0
- package/src/components/i18n-provider.tsx +22 -0
- package/src/components/install-hint.tsx +31 -0
- package/src/components/service-config-fields.tsx +110 -0
- package/src/lib/auth.ts +41 -0
- package/src/lib/change-request.ts +184 -0
- package/src/lib/config-keys.ts +300 -0
- package/src/lib/credentials.ts +171 -0
- package/src/lib/env-config.ts +39 -0
- package/src/lib/env-store.ts +72 -0
- package/src/lib/i18n-core.ts +27 -0
- package/src/lib/i18n.ts +72 -0
- package/src/lib/image-compress.ts +59 -0
- package/src/lib/mail.ts +121 -0
- package/src/lib/mongodb.ts +236 -0
- package/src/lib/passkey.ts +73 -0
- package/src/lib/pending.ts +142 -0
- package/src/lib/post-utils.ts +56 -0
- package/src/lib/posts.ts +283 -0
- package/src/lib/rate-limit.ts +91 -0
- package/src/lib/recovery-hints.ts +16 -0
- package/src/lib/safe-path.ts +0 -0
- package/src/lib/secret-box.ts +46 -0
- package/src/lib/service-config.ts +25 -0
- package/src/lib/service-features.ts +72 -0
- package/src/lib/session-constants.ts +5 -0
- package/src/lib/session.ts +51 -0
- package/src/lib/settings-file.ts +120 -0
- package/src/lib/settings.ts +199 -0
- package/src/lib/site-url.ts +15 -0
- package/src/lib/slug.ts +53 -0
- package/src/lib/storage.ts +226 -0
- package/src/lib/time.ts +15 -0
- package/src/lib/totp.ts +88 -0
- package/src/lib/types.ts +72 -0
- package/src/lib/upload-client.ts +83 -0
- package/src/lib/xml.ts +12 -0
- package/src/proxy.ts +24 -0
- package/tsconfig.json +41 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 安装向导:只有一步——配置服务器环境变量。
|
|
5
|
+
*
|
|
6
|
+
* 配置齐全就算安装完成:「检查配置,完成安装」先校验服务端实时配置,
|
|
7
|
+
* 通过则标记初始化完成,跳到登录页。邮箱通知、Passkey、验证器都不在安装流程里,
|
|
8
|
+
* 首次登录后到站点设置里配。
|
|
9
|
+
*/
|
|
10
|
+
import { useState, useTransition } from "react";
|
|
11
|
+
import { useRouter } from "next/navigation";
|
|
12
|
+
import type { ConfigField } from "@/lib/config-keys";
|
|
13
|
+
import type { DatabaseStatus } from "@/lib/mongodb";
|
|
14
|
+
import { useT } from "@/components/i18n-provider";
|
|
15
|
+
import type { ServiceFeature } from "@/components/service-config-fields";
|
|
16
|
+
import AuthConfigPanel from "./auth-config-panel";
|
|
17
|
+
import { checkSetupReady } from "./config-actions";
|
|
18
|
+
import { generateSecretsAction } from "./secrets-actions";
|
|
19
|
+
import { RECOVERY_DELETE_HINT_BRIEF } from "@/lib/recovery-hints";
|
|
20
|
+
|
|
21
|
+
type Props = {
|
|
22
|
+
/** 身份验证与启动必需的变量(手填的那些) */
|
|
23
|
+
authItems: ConfigField[];
|
|
24
|
+
/** 邮件与文件存储的配置(按方案分组) */
|
|
25
|
+
serviceFeatures: ServiceFeature[];
|
|
26
|
+
/** 能不能写配置文件(写不了就不提供「配置文件」这个选项) */
|
|
27
|
+
fileWritable: boolean;
|
|
28
|
+
onVercel: boolean;
|
|
29
|
+
/** 当前库的状态:存在还是刚建的 */
|
|
30
|
+
dbStatus: DatabaseStatus | null;
|
|
31
|
+
/** 有没有配数据库地址 */
|
|
32
|
+
dbConfigured: boolean;
|
|
33
|
+
/** 服务器实际读到的 Passkey 域名主体 */
|
|
34
|
+
rpId: string;
|
|
35
|
+
/** 服务器实际读到的站点地址 */
|
|
36
|
+
origin: string;
|
|
37
|
+
/** 这次请求是从哪个域名进来的 */
|
|
38
|
+
requestHost: string;
|
|
39
|
+
/** RP_ID 与当前域名是否对得上(浏览器的要求:相等或是它的可注册域后缀) */
|
|
40
|
+
rpIdMatchesHost: boolean;
|
|
41
|
+
/** ORIGIN 里的主机名 */
|
|
42
|
+
originHost: string;
|
|
43
|
+
/** 数据库连上了但读不到凭据:这时不能免登录,得跟站长说清楚 */
|
|
44
|
+
credentialProbeFailed: boolean;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type Secrets = { authSecret: string; recoveryToken: string };
|
|
48
|
+
|
|
49
|
+
/** 值里有空格、# 或引号就加引号,保证生成的 .env 能正确解析 */
|
|
50
|
+
function formatValue(value: string): string {
|
|
51
|
+
return /[\s#"'`\\]/.test(value) || value === ""
|
|
52
|
+
? `"${value.replace(/"/g, '\\"')}"`
|
|
53
|
+
: value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default function SetupWizard({
|
|
57
|
+
authItems,
|
|
58
|
+
serviceFeatures,
|
|
59
|
+
fileWritable,
|
|
60
|
+
onVercel,
|
|
61
|
+
dbStatus,
|
|
62
|
+
dbConfigured,
|
|
63
|
+
rpId,
|
|
64
|
+
origin,
|
|
65
|
+
requestHost,
|
|
66
|
+
rpIdMatchesHost,
|
|
67
|
+
originHost,
|
|
68
|
+
credentialProbeFailed,
|
|
69
|
+
}: Props) {
|
|
70
|
+
const t = useT();
|
|
71
|
+
const router = useRouter();
|
|
72
|
+
const [busy, setBusy] = useState(false);
|
|
73
|
+
const [error, setError] = useState("");
|
|
74
|
+
const [checkDone, setCheckDone] = useState(false);
|
|
75
|
+
|
|
76
|
+
const [values, setValues] = useState<Record<string, string>>({});
|
|
77
|
+
const [serviceValues, setServiceValues] = useState<Record<string, string>>(
|
|
78
|
+
() =>
|
|
79
|
+
Object.fromEntries(
|
|
80
|
+
serviceFeatures.map((feature) => [
|
|
81
|
+
feature.choiceKey,
|
|
82
|
+
feature.current || feature.providers[0]?.value || "",
|
|
83
|
+
])
|
|
84
|
+
)
|
|
85
|
+
);
|
|
86
|
+
const [pending, startTransition] = useTransition();
|
|
87
|
+
const [envError, setEnvError] = useState("");
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 底部「生成 .env 文件」的必填判断:和 checkSetupReady 的服务端口径对齐。
|
|
91
|
+
* - 手填:数据库串、库名、域名主体、站点地址(已配置的算填过)
|
|
92
|
+
* - 发信:按当前选的 provider 要它的必填字段;文件存储不算必填
|
|
93
|
+
* 只用来决定按钮能不能点,真正的放行还是服务端检查配置那一步。
|
|
94
|
+
*/
|
|
95
|
+
const fieldFilled = (key: string): boolean => {
|
|
96
|
+
const item = authItems.find((entry) => entry.key === key);
|
|
97
|
+
return Boolean(item?.configured || (values[key] ?? "").trim());
|
|
98
|
+
};
|
|
99
|
+
const serviceFieldFilled = (key: string): boolean => {
|
|
100
|
+
const feature = serviceFeatures.find((entry) =>
|
|
101
|
+
entry.fields.some((field) => field.key === key)
|
|
102
|
+
);
|
|
103
|
+
const field = feature?.fields.find((entry) => entry.key === key);
|
|
104
|
+
return Boolean(field?.configured || (serviceValues[key] ?? "").trim());
|
|
105
|
+
};
|
|
106
|
+
const mailFeature = serviceFeatures.find((feature) => feature.id === "mail");
|
|
107
|
+
const mailChoice = mailFeature
|
|
108
|
+
? serviceValues[mailFeature.choiceKey] ||
|
|
109
|
+
mailFeature.current ||
|
|
110
|
+
mailFeature.providers[0]?.value ||
|
|
111
|
+
""
|
|
112
|
+
: "";
|
|
113
|
+
const mailRequired =
|
|
114
|
+
mailChoice === "smtp"
|
|
115
|
+
? ["SMTP_HOST", "SMTP_FROM"]
|
|
116
|
+
: ["RESEND_API_KEY", "RESEND_FROM"];
|
|
117
|
+
const canGenerate =
|
|
118
|
+
["MONGODB_URI", "MONGODB_DB_NAME", "RP_ID", "ORIGIN"].every(fieldFilled) &&
|
|
119
|
+
mailRequired.every(serviceFieldFilled);
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 所有手填字段的当前值。
|
|
123
|
+
* 下拉项没手动改过时用服务器上生效的值;写不了文件的平台上退到第一个选项。
|
|
124
|
+
*/
|
|
125
|
+
const currentValues = (): Record<string, string> => {
|
|
126
|
+
const out: Record<string, string> = {};
|
|
127
|
+
for (const item of authItems) {
|
|
128
|
+
const fallback = item.options
|
|
129
|
+
? item.value || item.options[0]?.value || ""
|
|
130
|
+
: "";
|
|
131
|
+
out[item.key] = values[item.key] ?? fallback;
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** 把所有值拼成 .env 的内容,密钥现生成、不回显 */
|
|
137
|
+
const buildEnvFile = async (): Promise<string> => {
|
|
138
|
+
let result: Awaited<ReturnType<typeof generateSecretsAction>>;
|
|
139
|
+
try {
|
|
140
|
+
result = await generateSecretsAction();
|
|
141
|
+
} catch {
|
|
142
|
+
throw new Error(t("生成失败"));
|
|
143
|
+
}
|
|
144
|
+
const sec: Secrets = {
|
|
145
|
+
authSecret: result.authSecret,
|
|
146
|
+
recoveryToken: result.recoveryToken,
|
|
147
|
+
};
|
|
148
|
+
const lines: string[] = [
|
|
149
|
+
"# inslog 的服务器配置",
|
|
150
|
+
"# 请保存到服务器项目根目录,或导入到 Vercel → Settings → Environment Variables",
|
|
151
|
+
"",
|
|
152
|
+
];
|
|
153
|
+
const all: [string, string][] = [
|
|
154
|
+
...Object.entries(currentValues()),
|
|
155
|
+
["AUTH_SECRET", sec.authSecret],
|
|
156
|
+
["RECOVERY_TOKEN", sec.recoveryToken],
|
|
157
|
+
...Object.entries(serviceValues),
|
|
158
|
+
];
|
|
159
|
+
for (const [key, value] of all) {
|
|
160
|
+
if (!value) continue;
|
|
161
|
+
lines.push(`${key}=${formatValue(value)}`);
|
|
162
|
+
}
|
|
163
|
+
return lines.join("\n") + "\n";
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** 底部按钮:现生成密钥并下载 .env,不回显、绝不写服务器环境变量 */
|
|
167
|
+
const generateEnv = () => {
|
|
168
|
+
if (!canGenerate) return;
|
|
169
|
+
setEnvError("");
|
|
170
|
+
startTransition(async () => {
|
|
171
|
+
try {
|
|
172
|
+
const content = await buildEnvFile();
|
|
173
|
+
const blob = new Blob([content], { type: "text/plain;charset=utf-8" });
|
|
174
|
+
const url = URL.createObjectURL(blob);
|
|
175
|
+
const link = document.createElement("a");
|
|
176
|
+
link.href = url;
|
|
177
|
+
link.download = ".env";
|
|
178
|
+
document.body.appendChild(link);
|
|
179
|
+
link.click();
|
|
180
|
+
document.body.removeChild(link);
|
|
181
|
+
URL.revokeObjectURL(url);
|
|
182
|
+
} catch (e) {
|
|
183
|
+
setEnvError(e instanceof Error ? e.message : t("生成失败"));
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* 「检查配置,完成安装」:读服务端实时配置(不是输入框里的草稿)。
|
|
190
|
+
* 齐全则标记安装完成,跳登录页——邮箱、Passkey、验证器都留到登录后去设置页配。
|
|
191
|
+
*/
|
|
192
|
+
const checkConfig = async () => {
|
|
193
|
+
setBusy(true);
|
|
194
|
+
setError("");
|
|
195
|
+
setCheckDone(false);
|
|
196
|
+
try {
|
|
197
|
+
const res = await checkSetupReady();
|
|
198
|
+
if (!res.ok) {
|
|
199
|
+
setError(
|
|
200
|
+
t("配置未生效:{items}。请保存 .env 并重启 / 重新部署。", {
|
|
201
|
+
items: res.missing.join("、"),
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const done = await fetch("/api/setup/complete", { method: "POST" });
|
|
207
|
+
const data = await done.json();
|
|
208
|
+
if (!done.ok) throw new Error(data.error ?? t("保存失败"));
|
|
209
|
+
// 先给出结果,再跳转,让用户看清检查通过。
|
|
210
|
+
setCheckDone(true);
|
|
211
|
+
setTimeout(() => {
|
|
212
|
+
// 首次没有钥匙,用紧急密钥进门;登录后直达设置页配邮箱和身份验证。
|
|
213
|
+
router.push("/login?recovery=1&next=/settings&welcome=1");
|
|
214
|
+
router.refresh();
|
|
215
|
+
}, 1200);
|
|
216
|
+
} catch (e) {
|
|
217
|
+
setError(e instanceof Error ? e.message : t("检查配置失败"));
|
|
218
|
+
} finally {
|
|
219
|
+
setBusy(false);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
return (
|
|
224
|
+
<section className="section">
|
|
225
|
+
{credentialProbeFailed && (
|
|
226
|
+
<p className="form-error">
|
|
227
|
+
{t(
|
|
228
|
+
"数据库连上了但读不到登录凭据:可能是数据库用户的权限不够。为了安全起见,这时不能免登录修改配置——先修好数据库权限再回来。"
|
|
229
|
+
)}
|
|
230
|
+
</p>
|
|
231
|
+
)}
|
|
232
|
+
<AuthConfigPanel
|
|
233
|
+
manualItems={authItems}
|
|
234
|
+
fileWritable={fileWritable}
|
|
235
|
+
serviceFeatures={serviceFeatures}
|
|
236
|
+
values={values}
|
|
237
|
+
onValuesChange={setValues}
|
|
238
|
+
serviceValues={serviceValues}
|
|
239
|
+
onServiceValuesChange={setServiceValues}
|
|
240
|
+
/>
|
|
241
|
+
|
|
242
|
+
<ol className="reset-steps">
|
|
243
|
+
<li>{t("填好各项,下载生成的 .env。")}</li>
|
|
244
|
+
<li>
|
|
245
|
+
{onVercel
|
|
246
|
+
? t("到 Vercel 项目 → Settings → Environment Variables,上传这个 .env 文件。")
|
|
247
|
+
: t("把下载的 .env 保存到服务器项目根目录,不要提交 Git。")}
|
|
248
|
+
</li>
|
|
249
|
+
<li>
|
|
250
|
+
{onVercel
|
|
251
|
+
? t("重新部署并等待完成,然后刷新本页。")
|
|
252
|
+
: t("重启服务并刷新本页。")}
|
|
253
|
+
</li>
|
|
254
|
+
<li>
|
|
255
|
+
{t("刷新后的页面点「检查配置,完成安装」,通过后会跳到登录页。")}
|
|
256
|
+
</li>
|
|
257
|
+
</ol>
|
|
258
|
+
|
|
259
|
+
{(rpId || origin) && (
|
|
260
|
+
<dl className="confirm-meta row gx-4 gy-1 my-4">
|
|
261
|
+
<dt className="col-12 col-sm-3">{t("当前域名")}</dt>
|
|
262
|
+
<dd className="col-12 col-sm-9">{requestHost || t("(未知)")}</dd>
|
|
263
|
+
<dt className="col-12 col-sm-3">RP_ID</dt>
|
|
264
|
+
<dd className="col-12 col-sm-9">{rpId || t("(未配置)")}</dd>
|
|
265
|
+
<dt className="col-12 col-sm-3">ORIGIN</dt>
|
|
266
|
+
<dd className="col-12 col-sm-9">{origin || t("(未配置)")}</dd>
|
|
267
|
+
</dl>
|
|
268
|
+
)}
|
|
269
|
+
|
|
270
|
+
{!!rpId && !rpIdMatchesHost && (
|
|
271
|
+
<p className="form-error">
|
|
272
|
+
{t(
|
|
273
|
+
"RP_ID 和当前域名对不上,浏览器会拒绝绑定 Passkey。RP_ID 要填当前域名本身,或者它的上级可注册域,不能带 https://、端口或路径。"
|
|
274
|
+
)}
|
|
275
|
+
{" "}
|
|
276
|
+
{requestHost
|
|
277
|
+
? t("按当前域名,应填:{host}", { host: requestHost })
|
|
278
|
+
: t("改成当前站点的域名,不带协议。")}
|
|
279
|
+
</p>
|
|
280
|
+
)}
|
|
281
|
+
|
|
282
|
+
{!!originHost && requestHost && originHost !== requestHost && (
|
|
283
|
+
<p className="form-error">
|
|
284
|
+
{t(
|
|
285
|
+
"ORIGIN 与当前域名不符,确认邮件链接会指向别处。应填 https://{host}",
|
|
286
|
+
{ host: requestHost }
|
|
287
|
+
)}
|
|
288
|
+
</p>
|
|
289
|
+
)}
|
|
290
|
+
|
|
291
|
+
{dbStatus ? (
|
|
292
|
+
<p className="notice">
|
|
293
|
+
{dbStatus.created
|
|
294
|
+
? t("数据库 {name} 已创建,连得上。", { name: dbStatus.dbName })
|
|
295
|
+
: t("数据库 {name} 连得上,里面有 {n} 篇文章。", {
|
|
296
|
+
name: dbStatus.dbName,
|
|
297
|
+
n: String(dbStatus.postCount),
|
|
298
|
+
})}
|
|
299
|
+
</p>
|
|
300
|
+
) : (
|
|
301
|
+
dbConfigured && (
|
|
302
|
+
<p className="form-error">
|
|
303
|
+
{t(
|
|
304
|
+
"地址填了,但连不上。查两处:连接串里的用户名密码对不对,Atlas 的 Network Access 里有没有放行当前出口 IP(加上 0.0.0.0/0 最省事)。改完 .env 后要重新部署或重启,然后再刷新本页。"
|
|
305
|
+
)}
|
|
306
|
+
</p>
|
|
307
|
+
)
|
|
308
|
+
)}
|
|
309
|
+
|
|
310
|
+
{envError && <p className="form-error">{envError}</p>}
|
|
311
|
+
{error && <p className="form-error">{error}</p>}
|
|
312
|
+
{checkDone && <p className="notice">{t("检查通过,正在跳转到登录页…")}</p>}
|
|
313
|
+
|
|
314
|
+
<div className="setup-nav d-flex flex-wrap align-items-center">
|
|
315
|
+
<button
|
|
316
|
+
className="btn btn-outline-light btn-plain"
|
|
317
|
+
type="button"
|
|
318
|
+
disabled={!canGenerate || pending || busy || checkDone}
|
|
319
|
+
onClick={generateEnv}
|
|
320
|
+
>
|
|
321
|
+
{pending ? t("生成中…") : t("生成 .env 文件")}
|
|
322
|
+
</button>
|
|
323
|
+
{dbConfigured && (
|
|
324
|
+
<button
|
|
325
|
+
className="btn btn-outline-light btn-plain"
|
|
326
|
+
type="button"
|
|
327
|
+
disabled={busy || pending || checkDone}
|
|
328
|
+
onClick={() => void checkConfig()}
|
|
329
|
+
>
|
|
330
|
+
{busy ? t("检查中…") : t("检查配置,完成安装")}
|
|
331
|
+
</button>
|
|
332
|
+
)}
|
|
333
|
+
</div>
|
|
334
|
+
|
|
335
|
+
<p className="field-hint">{t(RECOVERY_DELETE_HINT_BRIEF)}</p>
|
|
336
|
+
</section>
|
|
337
|
+
);
|
|
338
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// 站点地图:首页 + 搜索 + 全部 public 文章
|
|
2
|
+
import { listPosts } from "@/lib/posts";
|
|
3
|
+
import { isDbConfigured } from "@/lib/mongodb";
|
|
4
|
+
import { siteOrigin } from "@/lib/site-url";
|
|
5
|
+
import { xmlEscape } from "@/lib/xml";
|
|
6
|
+
|
|
7
|
+
export const dynamic = "force-dynamic";
|
|
8
|
+
|
|
9
|
+
export async function GET() {
|
|
10
|
+
// 数据库还没配好时(新部署)返回 503,而不是 500
|
|
11
|
+
if (!(await isDbConfigured())) {
|
|
12
|
+
return new Response("Site is not configured yet.", {
|
|
13
|
+
status: 503,
|
|
14
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const { items } = await listPosts({ limit: 1000 });
|
|
18
|
+
const origin = siteOrigin();
|
|
19
|
+
|
|
20
|
+
const urls = [
|
|
21
|
+
{ loc: origin, lastmod: "" },
|
|
22
|
+
{ loc: `${origin}/search`, lastmod: "" },
|
|
23
|
+
...items.map((post) => ({
|
|
24
|
+
loc: `${origin}/posts/${post.slug}`,
|
|
25
|
+
lastmod: (post.updatedAt ?? post.createdAt).toISOString().slice(0, 10),
|
|
26
|
+
})),
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
30
|
+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
31
|
+
${urls
|
|
32
|
+
.map(
|
|
33
|
+
(u) => ` <url>
|
|
34
|
+
<loc>${xmlEscape(u.loc)}</loc>${u.lastmod ? `\n <lastmod>${u.lastmod}</lastmod>` : ""}
|
|
35
|
+
</url>`
|
|
36
|
+
)
|
|
37
|
+
.join("\n")}
|
|
38
|
+
</urlset>`;
|
|
39
|
+
|
|
40
|
+
return new Response(xml, {
|
|
41
|
+
headers: {
|
|
42
|
+
"content-type": "application/xml; charset=utf-8",
|
|
43
|
+
"cache-control": "public, max-age=3600",
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// 本地磁盘存储的文件访问路由(仅 local 方案用到)
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { contentTypeByExt, localUploadDir, resolveLocalUploadPath } from "@/lib/storage";
|
|
4
|
+
|
|
5
|
+
export const dynamic = "force-dynamic";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 拼一个安全的 Content-Disposition。
|
|
9
|
+
*
|
|
10
|
+
* 文件名是用户给的,直接塞进响应头会被换行注入(响应头拆分),
|
|
11
|
+
* 也可能被浏览器当成路径。所以:
|
|
12
|
+
* - filename 给一份清洗过的 ASCII 版(浏览器的老式回退)
|
|
13
|
+
* - filename* 给原始名(RFC 5987 的 URI 编码,中文名靠它才不乱码)
|
|
14
|
+
*/
|
|
15
|
+
function contentDisposition(fileName: string): string {
|
|
16
|
+
const ascii = fileName
|
|
17
|
+
// 去掉控制字符和引号,防止响应头被拆坏
|
|
18
|
+
.replace(/[\r\n"\\]/g, "_")
|
|
19
|
+
.replace(/[^\x20-\x7e]/g, "_")
|
|
20
|
+
.slice(0, 120);
|
|
21
|
+
const encoded = encodeURIComponent(fileName).slice(0, 300);
|
|
22
|
+
return `attachment; filename="${ascii || "download"}"; filename*=UTF-8''${encoded}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function GET(
|
|
26
|
+
req: Request,
|
|
27
|
+
{ params }: { params: Promise<{ path: string[] }> }
|
|
28
|
+
) {
|
|
29
|
+
const { path } = await params;
|
|
30
|
+
const relative = (path ?? []).join("/");
|
|
31
|
+
if (!relative) return new Response("Not found", { status: 404 });
|
|
32
|
+
|
|
33
|
+
const base = await localUploadDir();
|
|
34
|
+
const target = resolveLocalUploadPath(relative, base);
|
|
35
|
+
// 越界(../)直接拒绝
|
|
36
|
+
if (!target) return new Response("Not found", { status: 404 });
|
|
37
|
+
|
|
38
|
+
const url = new URL(req.url);
|
|
39
|
+
// 附件链接带 ?download=1:强制下载,不让浏览器拿你域名去渲染它
|
|
40
|
+
const wantsDownload = url.searchParams.get("download") === "1";
|
|
41
|
+
// 原始文件名由调用方给(附件列表里存着),只是显示用
|
|
42
|
+
const rawName = url.searchParams.get("filename") ?? "";
|
|
43
|
+
const originalName = rawName.slice(0, 200);
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const data = await readFile(target);
|
|
47
|
+
const type = contentTypeByExt(target);
|
|
48
|
+
const headers: Record<string, string> = {
|
|
49
|
+
"content-type": type,
|
|
50
|
+
// 文件名带随机串,内容不会变,可以长期缓存
|
|
51
|
+
"cache-control": "public, max-age=31536000, immutable",
|
|
52
|
+
// 别让浏览器自己猜类型——猜错了就能绕过上面的下载限制
|
|
53
|
+
"x-content-type-options": "nosniff",
|
|
54
|
+
};
|
|
55
|
+
if (wantsDownload) {
|
|
56
|
+
headers["content-disposition"] = contentDisposition(
|
|
57
|
+
originalName || target.split("/").pop() || "download"
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return new Response(new Uint8Array(data), { headers });
|
|
61
|
+
} catch {
|
|
62
|
+
return new Response("Not found", { status: 404 });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
|
|
3
|
+
import { revalidatePath } from "next/cache";
|
|
4
|
+
import type { Block } from "@blocknote/core";
|
|
5
|
+
import { isLoggedIn } from "@/lib/auth";
|
|
6
|
+
import { createPost, deletePost, updatePost, SlugConflictError } from "@/lib/posts";
|
|
7
|
+
import { normalizeCustomSlug } from "@/lib/slug";
|
|
8
|
+
import type { Visibility } from "@/lib/types";
|
|
9
|
+
import { getT } from "@/lib/i18n";
|
|
10
|
+
|
|
11
|
+
export type SaveResult = { ok: true; slug: string } | { ok: false; error: string };
|
|
12
|
+
|
|
13
|
+
// 写作页的保存:有 slug 走更新,没有就新建。
|
|
14
|
+
// proxy.ts 只挡页面请求,server action 是另一个入口,这里必须自己再校验一次。
|
|
15
|
+
export async function savePost(input: {
|
|
16
|
+
slug: string | null;
|
|
17
|
+
customSlug: string;
|
|
18
|
+
title: string;
|
|
19
|
+
blocks: Block[];
|
|
20
|
+
tags: string[];
|
|
21
|
+
visibility: Visibility;
|
|
22
|
+
pinned: boolean;
|
|
23
|
+
}): Promise<SaveResult> {
|
|
24
|
+
const t = await getT();
|
|
25
|
+
if (!(await isLoggedIn())) {
|
|
26
|
+
return { ok: false, error: t("登录已过期,请重新登录后再保存") };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const title = input.title.trim();
|
|
30
|
+
if (!title) return { ok: false, error: t("先写个标题吧") };
|
|
31
|
+
|
|
32
|
+
// 用户填的地址统一规范化;留空则由数据库自动生成(新建)或保持原样(编辑)
|
|
33
|
+
const customSlug = normalizeCustomSlug(input.customSlug);
|
|
34
|
+
|
|
35
|
+
const payload = {
|
|
36
|
+
title,
|
|
37
|
+
blocks: input.blocks,
|
|
38
|
+
tags: input.tags,
|
|
39
|
+
visibility: input.visibility,
|
|
40
|
+
pinned: input.pinned,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
if (input.slug) {
|
|
45
|
+
const updated = await updatePost(input.slug, {
|
|
46
|
+
...payload,
|
|
47
|
+
newSlug: customSlug,
|
|
48
|
+
});
|
|
49
|
+
if (!updated) return { ok: false, error: t("文章不存在,可能已被删除") };
|
|
50
|
+
revalidatePath("/");
|
|
51
|
+
// 地址改了,旧地址也要清缓存,免得访客还能从旧链接看到内容
|
|
52
|
+
if (updated.slug !== input.slug) revalidatePath(`/posts/${input.slug}`);
|
|
53
|
+
revalidatePath(`/posts/${updated.slug}`);
|
|
54
|
+
return { ok: true, slug: updated.slug };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const created = await createPost({ ...payload, customSlug });
|
|
58
|
+
revalidatePath("/");
|
|
59
|
+
return { ok: true, slug: created.slug };
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err instanceof SlugConflictError) {
|
|
62
|
+
return { ok: false, error: t("这个地址已被占用,换一个吧") };
|
|
63
|
+
}
|
|
64
|
+
return { ok: false, error: t("保存失败:数据库连接异常") };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 删除文章。二次确认在客户端做,服务端只负责校验登录与执行。 */
|
|
69
|
+
export async function deletePostAction(
|
|
70
|
+
slug: string
|
|
71
|
+
): Promise<{ ok: true } | { ok: false; error: string }> {
|
|
72
|
+
const t = await getT();
|
|
73
|
+
if (!(await isLoggedIn())) {
|
|
74
|
+
return { ok: false, error: t("登录已过期,请重新登录") };
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const removed = await deletePost(slug);
|
|
78
|
+
if (!removed) return { ok: false, error: t("文章不存在,可能已被删除") };
|
|
79
|
+
revalidatePath("/");
|
|
80
|
+
revalidatePath(`/posts/${slug}`);
|
|
81
|
+
return { ok: true };
|
|
82
|
+
} catch {
|
|
83
|
+
return { ok: false, error: t("删除失败:数据库连接异常") };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// 写作页:新建(无参数)或编辑(?slug=xxx),内容与标签都从数据库读
|
|
2
|
+
import { redirect } from "next/navigation";
|
|
3
|
+
import SiteFooter from "@/components/SiteFooter";
|
|
4
|
+
import { isLoggedIn } from "@/lib/auth";
|
|
5
|
+
import { getPostBySlug, listTags } from "@/lib/posts";
|
|
6
|
+
import { getStorageProvider } from "@/lib/storage";
|
|
7
|
+
import { siteOrigin } from "@/lib/site-url";
|
|
8
|
+
import WriteClient from "./write-client";
|
|
9
|
+
|
|
10
|
+
export const dynamic = "force-dynamic";
|
|
11
|
+
|
|
12
|
+
type Props = {
|
|
13
|
+
searchParams: Promise<{ slug?: string }>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default async function WritePage({ searchParams }: Props) {
|
|
17
|
+
const { slug } = await searchParams;
|
|
18
|
+
|
|
19
|
+
// proxy.ts 只粗筛 cookie,会话版本失效(换过验证器)的旧 cookie 要在这里挡掉
|
|
20
|
+
if (!(await isLoggedIn())) {
|
|
21
|
+
const next = slug ? `/write?slug=${encodeURIComponent(slug)}` : "/write";
|
|
22
|
+
redirect(`/login?next=${encodeURIComponent(next)}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 这个路由只对登录者开放,取文章时一并包含私有和草稿
|
|
26
|
+
const [post, tags, storageProvider] = await Promise.all([
|
|
27
|
+
slug ? getPostBySlug(slug, true) : Promise.resolve(null),
|
|
28
|
+
listTags({ includeHidden: true }),
|
|
29
|
+
getStorageProvider(),
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<main className="site write-page mx-auto">
|
|
34
|
+
<WriteClient
|
|
35
|
+
slug={post?.slug ?? null}
|
|
36
|
+
initialTitle={post?.title ?? ""}
|
|
37
|
+
initialTags={post?.tags ?? []}
|
|
38
|
+
initialBlocks={post?.blocks ?? []}
|
|
39
|
+
initialVisibility={post?.visibility ?? "draft"}
|
|
40
|
+
initialPinned={post?.pinned ?? false}
|
|
41
|
+
availableTags={tags}
|
|
42
|
+
storageProvider={storageProvider}
|
|
43
|
+
siteOrigin={siteOrigin()}
|
|
44
|
+
/>
|
|
45
|
+
<SiteFooter />
|
|
46
|
+
</main>
|
|
47
|
+
);
|
|
48
|
+
}
|