fix: 修复本地图片上传与发布进程切换
This commit is contained in:
1 parent
7f67f4d685
commit
dcf1d76944
7 files changed
+121
-25
No files matched your search
@@ -52,7 +52,12 @@ export function ImageUrlField({ id, value, onChange, placeholder, scope }: Image
|
||||
});
|
||||
const response = await fetch(presign.uploadUrl, {
|
||||
method: "PUT",
|
||||
headers: { "content-type": file.type },
|
||||
headers: {
|
||||
"content-type": file.type,
|
||||
...(presign.uploadUrl.startsWith("/api/")
|
||||
? { authorization: `Bearer ${localStorage.getItem("adminToken") ?? ""}` }
|
||||
: {}),
|
||||
},
|
||||
body: file,
|
||||
});
|
||||
if (!response.ok) throw new Error(`upload failed: ${response.status}`);
|
||||
|
||||
@@ -136,7 +136,7 @@ export class AdminUploadController {
|
||||
const target = path.join(this.localMediaRoot, key);
|
||||
await mkdir(path.dirname(target), { recursive: true });
|
||||
return {
|
||||
uploadUrl: `/admin/uploads/local?key=${encodeURIComponent(key)}`,
|
||||
uploadUrl: `/api/admin/uploads/local?key=${encodeURIComponent(key)}`,
|
||||
key,
|
||||
publicUrl: `${this.localMediaBaseUrl}/${key}`,
|
||||
mode: "local",
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
|
||||
test("upload presign DTO rejects unsupported media type and oversized names", async () => {
|
||||
@@ -16,3 +19,38 @@ test("upload presign DTO rejects unsupported media type and oversized names", as
|
||||
valid.contentType = "image/jpeg";
|
||||
assert.deepEqual(await validator.validate(valid), []);
|
||||
});
|
||||
|
||||
test("local upload presign targets the API route and requires brand access", async () => {
|
||||
const { AdminUploadController } =
|
||||
await import("../dist/modules/admin/admin-upload.controller.js");
|
||||
const mediaRoot = await mkdtemp(path.join(os.tmpdir(), "personal-brand-upload-"));
|
||||
const previousRoot = process.env.LOCAL_MEDIA_ROOT;
|
||||
const previousBucket = process.env.OBJECT_STORAGE_BUCKET;
|
||||
const previousAccessKey = process.env.OBJECT_STORAGE_ACCESS_KEY;
|
||||
const previousSecretKey = process.env.OBJECT_STORAGE_SECRET_KEY;
|
||||
const access = { requireBrand: async () => ({ brandId: "brand-test" }) };
|
||||
process.env.LOCAL_MEDIA_ROOT = mediaRoot;
|
||||
delete process.env.OBJECT_STORAGE_BUCKET;
|
||||
delete process.env.OBJECT_STORAGE_ACCESS_KEY;
|
||||
delete process.env.OBJECT_STORAGE_SECRET_KEY;
|
||||
try {
|
||||
const controller = new AdminUploadController(access);
|
||||
const result = await controller.presign(
|
||||
{},
|
||||
{ fileName: "portrait.jpg", contentType: "image/jpeg" },
|
||||
);
|
||||
assert.match(result.uploadUrl, /^\/api\/admin\/uploads\/local\?key=\d{4}%2F[a-f0-9]{16}\.jpg$/);
|
||||
assert.equal(result.mode, "local");
|
||||
} finally {
|
||||
for (const [name, value] of [
|
||||
["LOCAL_MEDIA_ROOT", previousRoot],
|
||||
["OBJECT_STORAGE_BUCKET", previousBucket],
|
||||
["OBJECT_STORAGE_ACCESS_KEY", previousAccessKey],
|
||||
["OBJECT_STORAGE_SECRET_KEY", previousSecretKey],
|
||||
]) {
|
||||
if (value === undefined) delete process.env[name];
|
||||
else process.env[name] = value;
|
||||
}
|
||||
await rm(mediaRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
+61
-19
@@ -61,6 +61,7 @@ if (!/^[a-zA-Z0-9.-]+$/.test(traefikHost) || !/^[a-zA-Z0-9.:-]+$/.test(traefikGa
|
||||
}
|
||||
const routeFile = path.join(traefikDirectory, "personal-brand-managed.yml");
|
||||
const adminServiceName = "personal-brand-admin-static.service";
|
||||
const pm2ServiceName = "personal-brand-pm2.service";
|
||||
const adminServiceDirectory = path.join(
|
||||
process.env.XDG_CONFIG_HOME ?? path.join(process.env.HOME ?? "/home/ubuntu", ".config"),
|
||||
"systemd/user",
|
||||
@@ -72,6 +73,11 @@ function run(command, args, cwd = root) {
|
||||
if (result.status !== 0) throw new Error(command + " failed with exit " + result.status);
|
||||
}
|
||||
|
||||
function restartApi(release) {
|
||||
run("pm2", ["delete", "personal-brand-api"]);
|
||||
run("pm2", ["start", path.join(release, "deploy/ecosystem.config.cjs"), "--update-env"]);
|
||||
}
|
||||
|
||||
async function installAdminService(release) {
|
||||
const stopped = spawnSync("systemctl", ["--user", "stop", adminServiceName], {
|
||||
stdio: "ignore",
|
||||
@@ -91,6 +97,19 @@ async function installAdminService(release) {
|
||||
run("systemctl", ["--user", "restart", adminServiceName]);
|
||||
}
|
||||
|
||||
async function installPm2Service() {
|
||||
const directory = path.dirname(adminServiceFile);
|
||||
const file = path.join(directory, pm2ServiceName);
|
||||
await mkdir(directory, { recursive: true });
|
||||
await writeFile(
|
||||
file,
|
||||
`[Unit]\nDescription=Personal Brand API PM2 processes\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=oneshot\nRemainAfterExit=yes\nEnvironment=HOME=${process.env.HOME ?? "/home/ubuntu"}\nEnvironment=PATH=${process.env.PATH}\nExecStart=${process.env.HOME ?? "/home/ubuntu"}/.nvm/versions/node/v${process.versions.node}/bin/pm2 resurrect\nExecStop=${process.env.HOME ?? "/home/ubuntu"}/.nvm/versions/node/v${process.versions.node}/bin/pm2 save\n\n[Install]\nWantedBy=default.target\n`,
|
||||
{ mode: 0o644 },
|
||||
);
|
||||
run("systemctl", ["--user", "daemon-reload"]);
|
||||
run("systemctl", ["--user", "enable", pm2ServiceName]);
|
||||
}
|
||||
|
||||
async function waitForAdminHealth() {
|
||||
const url = "http://127.0.0.1:" + (process.env.ADMIN_STATIC_PORT ?? "3010") + "/admin/";
|
||||
for (let attempt = 0; attempt < 20; attempt++) {
|
||||
@@ -142,19 +161,24 @@ async function backupDatabase(destination) {
|
||||
);
|
||||
const output = createWriteStream(temporary, { flags: "wx", mode: 0o600 });
|
||||
child.stdout.pipe(output);
|
||||
await Promise.all([
|
||||
new Promise((resolve, reject) => {
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code) =>
|
||||
code === 0 ? resolve() : reject(new Error("Database backup failed")),
|
||||
);
|
||||
}),
|
||||
new Promise((resolve, reject) => {
|
||||
output.on("finish", resolve);
|
||||
output.on("error", reject);
|
||||
}),
|
||||
]);
|
||||
await rename(temporary, destination);
|
||||
try {
|
||||
await Promise.all([
|
||||
new Promise((resolve, reject) => {
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code) =>
|
||||
code === 0 ? resolve() : reject(new Error("Database backup failed")),
|
||||
);
|
||||
}),
|
||||
new Promise((resolve, reject) => {
|
||||
output.on("finish", resolve);
|
||||
output.on("error", reject);
|
||||
}),
|
||||
]);
|
||||
await rename(temporary, destination);
|
||||
} catch (error) {
|
||||
await rm(temporary, { force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function pruneBackups(directory) {
|
||||
@@ -164,6 +188,26 @@ async function pruneBackups(directory) {
|
||||
.sort()
|
||||
.reverse();
|
||||
for (const backup of backups.slice(7)) await rm(path.join(directory, backup));
|
||||
for (const entry of await readdir(directory)) {
|
||||
if (entry.endsWith(".dump.tmp")) await rm(path.join(directory, entry), { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function pruneReleases(directory, currentRelease, previousRelease) {
|
||||
const releases = (await readdir(directory, { withFileTypes: true }))
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map((entry) => entry.name)
|
||||
.sort()
|
||||
.reverse();
|
||||
const keep = new Set([
|
||||
path.basename(currentRelease),
|
||||
path.basename(previousRelease),
|
||||
...releases.slice(0, 5),
|
||||
]);
|
||||
for (const release of releases) {
|
||||
if (!keep.has(release))
|
||||
await rm(path.join(directory, release), { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function copyMissingFiles(sourceDirectory, destinationDirectory) {
|
||||
@@ -333,7 +377,8 @@ try {
|
||||
await copyMissingFiles(path.join(release, "seed-media"), seedDirectory);
|
||||
await switchCurrent(release);
|
||||
switched = true;
|
||||
run("pm2", ["startOrRestart", path.join(release, "deploy/ecosystem.config.cjs"), "--update-env"]);
|
||||
restartApi(release);
|
||||
await installPm2Service();
|
||||
await installAdminService(release);
|
||||
await waitForHealth();
|
||||
await waitForAdminHealth();
|
||||
@@ -345,6 +390,7 @@ try {
|
||||
await waitForPublicRelease(release);
|
||||
await writeFile(path.join(root, "shared/previous-release"), previous + "\n");
|
||||
run("pm2", ["save"]);
|
||||
await pruneReleases(path.join(root, "releases"), release, previous);
|
||||
console.log("release installed: " + manifest.version);
|
||||
} catch (error) {
|
||||
if (routeUpdated) {
|
||||
@@ -354,11 +400,7 @@ try {
|
||||
if (switched) {
|
||||
await switchCurrent(previous);
|
||||
await installAdminService(previous);
|
||||
run("pm2", [
|
||||
"startOrRestart",
|
||||
path.join(source, "deploy/ecosystem.config.cjs"),
|
||||
"--update-env",
|
||||
]);
|
||||
restartApi(path.resolve(root, previous));
|
||||
await waitForHealth();
|
||||
await waitForAdminHealth();
|
||||
console.error(
|
||||
|
||||
+4
-2
@@ -6,7 +6,7 @@ API 默认监听 `http://localhost:3001`,管理后台默认监听 `http://loca
|
||||
|
||||
## 生产架构
|
||||
|
||||
生产环境只使用 Docker Compose 运行 PostgreSQL;API 和 Admin 运行在 VPS 宿主机上。API 由 Node.js 24 + PM2 运行在 `3001`,Admin 静态服务由 user-level `systemd` 运行在 `3010`,Traefik file provider 负责路由。Traefik 所在 Docker 网段必须允许访问宿主机 `3010`,但不需要向公网开放该端口。用户媒体目录位于发布目录之外,发布时只补充缺失的种子图片,不覆盖上传文件。
|
||||
生产环境只使用 Docker Compose 运行 PostgreSQL;API 和 Admin 运行在 VPS 宿主机上。API 由 Node.js 24 + PM2 运行在 `3001`,并由 user-level `systemd` unit 在登录会话之外恢复 PM2 进程;Admin 静态服务由 user-level `systemd` 运行在 `3010`,Traefik file provider 负责路由。Traefik 所在 Docker 网段必须允许访问宿主机 `3010`,但不需要向公网开放该端口。用户媒体目录位于发布目录之外,发布时只补充缺失的种子图片,不覆盖上传文件。
|
||||
|
||||
每次发布先创建 PostgreSQL `pg_dump` 备份,再执行 Prisma migration 和公网健康检查;检查失败时恢复应用版本和 Traefik 路由。数据库 migration 不自动回滚,必须使用备份恢复或人工处理。
|
||||
|
||||
@@ -30,7 +30,9 @@ DEPLOY_TARGET=oracle_arm PUBLIC_BASE_URL=https://wechat.app.que01.top pnpm deplo
|
||||
pnpm deploy:status
|
||||
```
|
||||
|
||||
数据库备份自动保留最近 7 份,发布失败时会保留备份文件,不自动删除数据库数据。
|
||||
数据库备份自动保留最近 7 份;备份归档在状态检查中通过 `pg_restore -l` 校验可读性。备份失败会清理未完成的临时文件,不自动删除数据库数据。发布成功后保留当前版本、上一版本和最近 5 个发布目录。状态检查同时报告 PM2 自启、磁盘空间、备份时间/可读性及发布目录数量。
|
||||
|
||||
备份当前仍在同一台 VPS 上,不等同于灾难恢复副本。应将 `shared/backups/*.dump` 定期复制到独立主机或对象存储,并至少每季度在隔离数据库执行一次恢复演练;不得在生产数据库上测试恢复。整机重启演练需在维护窗口执行,确认 Admin、API、PostgreSQL 和公网路由恢复后再结束维护。
|
||||
|
||||
## Nginx 反向代理
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ if (process.argv.includes("--status")) {
|
||||
process.exit(result.status ?? 1);
|
||||
}
|
||||
const target = process.env.DEPLOY_TARGET ?? "oracle_arm";
|
||||
const sshOptions = process.env.DEPLOY_SSH_OPTIONS
|
||||
? process.env.DEPLOY_SSH_OPTIONS.split(" ").filter(Boolean)
|
||||
: [];
|
||||
const remoteRoot = process.env.DEPLOY_ROOT ?? "/home/ubuntu/persional-brand";
|
||||
const publicBaseUrl = process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top";
|
||||
const timestamp = new Date().toISOString().replace(/\D/g, "").slice(0, 14);
|
||||
@@ -43,7 +46,7 @@ if (packageResult.status !== 0) process.exit(packageResult.status ?? 1);
|
||||
|
||||
await access(archive);
|
||||
await access(archive + ".sha256");
|
||||
run("scp", [archive, archive + ".sha256", target + ":/tmp/"]);
|
||||
run("scp", [...sshOptions, archive, archive + ".sha256", target + ":/tmp/"]);
|
||||
|
||||
const remoteScript = [
|
||||
"set -eu",
|
||||
@@ -67,7 +70,7 @@ const remoteScript = [
|
||||
"node deploy/install-release.mjs .",
|
||||
].join("; ");
|
||||
|
||||
run("ssh", [target, "bash -s"], { input: remoteScript + "\n" });
|
||||
run("ssh", [...sshOptions, target, "bash -s"], { input: remoteScript + "\n" });
|
||||
|
||||
const checks = ["/admin/", "/template-preview/", "/api/health", "/media/seed/food-package-02.jpg"];
|
||||
for (const route of checks) {
|
||||
|
||||
@@ -20,13 +20,19 @@ run("ssh", [
|
||||
"bash",
|
||||
"-lc",
|
||||
[
|
||||
"set -euo pipefail",
|
||||
'source "$HOME/.nvm/nvm.sh"',
|
||||
"export DEPLOY_ROOT=/home/ubuntu/persional-brand",
|
||||
'printf "current=%s\\n" "$(readlink /home/ubuntu/persional-brand/current)"',
|
||||
'printf "api="; test "$(pm2 pid personal-brand-api)" != "0"; echo online',
|
||||
'printf "pm2-autostart="; systemctl --user is-enabled personal-brand-pm2.service',
|
||||
'printf "admin="; systemctl --user is-active personal-brand-admin-static.service',
|
||||
'printf "database="; docker inspect -f "{{.State.Status}}" personal-brand-postgres-1',
|
||||
'printf "backups="; find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -name "*.dump" | wc -l',
|
||||
'printf "latest-backup="; newest=$(find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -type f -name "*.dump" -printf "%T@ %p\\n" | sort -nr | head -1 | cut -d" " -f2-); test -n "$newest"; stat -c "%y %s bytes" "$newest"; docker exec -i personal-brand-postgres-1 pg_restore -l < "$newest" >/dev/null; echo readable',
|
||||
'printf "backup-temp-files="; find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -type f -name "*.tmp" | wc -l',
|
||||
'printf "disk="; df -h / | awk "NR==2 {print \\$5 \" used, \" \\$4 \" free\"}"',
|
||||
'printf "release-count="; find /home/ubuntu/persional-brand/releases -mindepth 1 -maxdepth 1 -type d | wc -l',
|
||||
].join("; "),
|
||||
]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user