feat: 完善媒体管理与备份迁移流程
- 视频上传生成缩略图并提供占位封面和失败原因展示 - 支持 HLS 按需重新封装 MP4,并限制临时文件生命周期与访问权限 - 完善视频、文件 API 文档、搜索、批量删除和单次转码质量参数 - 修复备份恢复与跨后端迁移遗漏缩略图、HLS 清单和分片的问题 - 将备份任务、归档和下载权限按用户隔离 - 增加仅清理当前用户数据的高危存储清空流程和二次文字确认 - 修复 S3 中文文件名元数据及 CLI 数据库连接问题 - 补充备份恢复、用户隔离、媒体资源和转码相关测试
This commit is contained in:
1 parent
9ebab8270c
commit
a64be50b73
60 files changed
+2703
-325
No files matched your search
@@ -1,12 +1,23 @@
|
||||
import { requireAdminAuth } from '../../../utils/access'
|
||||
import { getBackupExportEstimate } from '../../../utils/backup/stats'
|
||||
import { getBackupsDir, isSafeBackupFilename } from '../../../utils/backup/paths'
|
||||
import { listAllBackupJobsForUser } from '../../../utils/backup/jobs'
|
||||
import { basename, join } from 'node:path'
|
||||
import { promises as fs } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
const estimate = await getBackupExportEstimate()
|
||||
const allowedArchives = new Set(
|
||||
listAllBackupJobsForUser(user.id).flatMap((job) => {
|
||||
const names: string[] = []
|
||||
if (job.outputPath) names.push(basename(job.outputPath))
|
||||
if (typeof job.payload.archivePath === 'string') {
|
||||
names.push(basename(job.payload.archivePath))
|
||||
}
|
||||
return names
|
||||
})
|
||||
)
|
||||
|
||||
const packages = await (async () => {
|
||||
try {
|
||||
@@ -15,6 +26,7 @@ export default defineEventHandler(async (event) => {
|
||||
const items = await Promise.all(
|
||||
entries
|
||||
.filter(entry => entry.isFile() && isSafeBackupFilename(entry.name))
|
||||
.filter(entry => allowedArchives.has(entry.name))
|
||||
.map(async (entry) => {
|
||||
const full = join(getBackupsDir(), entry.name)
|
||||
const fileStat = await fs.stat(full)
|
||||
|
||||
@@ -5,14 +5,14 @@ import { getBackupExportEstimate } from '../../../utils/backup/stats'
|
||||
import { startFullExportJob } from '../../../utils/backup/runner'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
const config = useRuntimeConfig(event)
|
||||
const appVersion = String(config.appVersion ?? '0.0.0')
|
||||
|
||||
const estimate = await getBackupExportEstimate()
|
||||
|
||||
try {
|
||||
const job = startFullExportJob(appVersion)
|
||||
const job = startFullExportJob(appVersion, user.id)
|
||||
return {
|
||||
job: serializeBackupJob(job),
|
||||
estimate
|
||||
|
||||
@@ -4,13 +4,13 @@ import { serializeBackupJob } from '../../../../utils/backup/api'
|
||||
import { getBackupJob } from '../../../../utils/backup/jobs'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
createApiError(event, 'INVALID_REQUEST', '缺少任务 ID', 400)
|
||||
}
|
||||
|
||||
const job = getBackupJob(id)
|
||||
const job = getBackupJob(id, user.id)
|
||||
if (!job) {
|
||||
createApiError(event, 'INVALID_REQUEST', '任务不存在', 404)
|
||||
}
|
||||
|
||||
@@ -5,16 +5,19 @@ import { getBackupJob } from '../../../../../utils/backup/jobs'
|
||||
import { cancelBackupJob } from '../../../../../utils/backup/runner'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
createApiError(event, 'INVALID_REQUEST', '缺少任务 ID', 400)
|
||||
}
|
||||
|
||||
if (!getBackupJob(id, user.id)) {
|
||||
createApiError(event, 'INVALID_REQUEST', '任务不存在', 404)
|
||||
}
|
||||
if (!cancelBackupJob(id)) {
|
||||
createApiError(event, 'INVALID_REQUEST', '只能取消进行中的任务', 400)
|
||||
}
|
||||
|
||||
const job = getBackupJob(id)
|
||||
const job = getBackupJob(id, user.id)
|
||||
return { job: job ? serializeBackupJob(job) : null }
|
||||
})
|
||||
@@ -6,13 +6,13 @@ import { getBackupJob } from '../../../../../utils/backup/jobs'
|
||||
import { BACKUP_ARCHIVE_EXT } from '../../../../../utils/backup/paths'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
createApiError(event, 'INVALID_REQUEST', '缺少任务 ID', 400)
|
||||
}
|
||||
|
||||
const job = getBackupJob(id)
|
||||
const job = getBackupJob(id, user.id)
|
||||
if (!job || job.type !== 'full_export' || job.status !== 'done' || !job.outputPath) {
|
||||
createApiError(event, 'INVALID_REQUEST', '可下载的备份不存在', 404)
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ import { serializeBackupJob } from '../../../../../utils/backup/api'
|
||||
import { getBackupJob } from '../../../../../utils/backup/jobs'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
createApiError(event, 'INVALID_REQUEST', '缺少任务 ID', 400)
|
||||
}
|
||||
|
||||
const job = getBackupJob(id)
|
||||
const job = getBackupJob(id, user.id)
|
||||
if (!job || job.type !== 'backend_sync') {
|
||||
createApiError(event, 'INVALID_REQUEST', '任务不存在', 404)
|
||||
}
|
||||
@@ -23,6 +23,6 @@ export default defineEventHandler(async (event) => {
|
||||
createApiError(event, 'INVALID_REQUEST', message, 400)
|
||||
}
|
||||
|
||||
const updated = getBackupJob(id)
|
||||
const updated = getBackupJob(id, user.id)
|
||||
return { job: updated ? serializeBackupJob(updated) : null }
|
||||
})
|
||||
@@ -9,21 +9,21 @@ import {
|
||||
import { isBackupRunnerBusy } from '../../../../utils/backup/runner'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
if (getRunningBackupJob() && !isBackupRunnerBusy()) {
|
||||
recoverInterruptedBackupJobs()
|
||||
}
|
||||
const query = getQuery(event)
|
||||
const pageSize = Math.min(50, Math.max(1, Number(query.pageSize) || 10))
|
||||
const pageNum = Math.max(1, Number(query.pageNum) || Number(query.page) || 1)
|
||||
const { jobs, total } = listBackupJobsPage(pageNum, pageSize)
|
||||
const { jobs, total } = listBackupJobsPage(pageNum, pageSize, user.id)
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize))
|
||||
const page = total > 0 ? Math.min(pageNum, totalPages) : 1
|
||||
const slice = page === pageNum
|
||||
? jobs
|
||||
: listBackupJobsPage(page, pageSize).jobs
|
||||
: listBackupJobsPage(page, pageSize, user.id).jobs
|
||||
|
||||
const activeJob = getActiveBackupJob()
|
||||
const activeJob = getActiveBackupJob(user.id)
|
||||
|
||||
return {
|
||||
jobs: slice.map(serializeBackupJob),
|
||||
|
||||
@@ -4,13 +4,14 @@ import { createApiError } from '../../../utils/api-error'
|
||||
import { serializeBackupJob } from '../../../utils/backup/api'
|
||||
import { previewFullRestore } from '../../../utils/backup/full-restore'
|
||||
import { startFullRestoreJob } from '../../../utils/backup/runner'
|
||||
import { listAllBackupJobsForUser } from '../../../utils/backup/jobs'
|
||||
import {
|
||||
BACKUP_ARCHIVE_EXT,
|
||||
getBackupsDir,
|
||||
getBackupStagingDir,
|
||||
isSafeBackupFilename
|
||||
} from '../../../utils/backup/paths'
|
||||
import { join } from 'node:path'
|
||||
import { basename, join } from 'node:path'
|
||||
import { promises as fs } from 'node:fs'
|
||||
|
||||
interface RestoreBody {
|
||||
@@ -19,7 +20,7 @@ interface RestoreBody {
|
||||
dryRun?: boolean
|
||||
}
|
||||
|
||||
async function resolveRestoreInput(event: H3Event): Promise<{
|
||||
async function resolveRestoreInput(event: H3Event, userId: number): Promise<{
|
||||
archivePath: string
|
||||
body: RestoreBody
|
||||
cleanup?: () => Promise<void>
|
||||
@@ -71,6 +72,19 @@ async function resolveRestoreInput(event: H3Event): Promise<{
|
||||
if (!body?.archiveName || !isSafeBackupFilename(body.archiveName)) {
|
||||
createApiError(event, 'INVALID_REQUEST', '请选择有效的备份文件', 400)
|
||||
}
|
||||
const allowedArchives = new Set(
|
||||
listAllBackupJobsForUser(userId).flatMap((job) => {
|
||||
const names: string[] = []
|
||||
if (job.outputPath) names.push(basename(job.outputPath))
|
||||
if (typeof job.payload.archivePath === 'string') {
|
||||
names.push(basename(job.payload.archivePath))
|
||||
}
|
||||
return names
|
||||
})
|
||||
)
|
||||
if (!allowedArchives.has(body.archiveName)) {
|
||||
createApiError(event, 'INVALID_REQUEST', '备份文件不存在', 404)
|
||||
}
|
||||
const archivePath = join(getBackupsDir(), body.archiveName)
|
||||
try {
|
||||
await fs.access(archivePath)
|
||||
@@ -81,9 +95,9 @@ async function resolveRestoreInput(event: H3Event): Promise<{
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
|
||||
const { archivePath, body, cleanup } = await resolveRestoreInput(event)
|
||||
const { archivePath, body, cleanup } = await resolveRestoreInput(event, user.id)
|
||||
|
||||
const conflictMode = body.conflictMode === 'skip' ? 'skip' : 'overwrite'
|
||||
|
||||
@@ -97,7 +111,7 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const job = startFullRestoreJob(archivePath, conflictMode)
|
||||
const job = startFullRestoreJob(archivePath, conflictMode, user.id)
|
||||
return { job: serializeBackupJob(job) }
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'BACKUP_JOB_RUNNING') {
|
||||
|
||||
@@ -13,7 +13,7 @@ interface SyncBody {
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
const user = await requireAdminAuth(event)
|
||||
const body = await readBody<SyncBody>(event)
|
||||
|
||||
const fromBackendId = body?.fromBackendId?.trim() ?? ''
|
||||
@@ -36,7 +36,7 @@ export default defineEventHandler(async (event) => {
|
||||
return { preview }
|
||||
}
|
||||
|
||||
const job = startBackendSyncJob(options)
|
||||
const job = startBackendSyncJob(options, user.id)
|
||||
return { job: serializeBackupJob(job) }
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
|
||||
Reference in new issue
Block a user