d67849a311
- 内容迁入数据库:articles/works 表(中英双语 + 内容块), server/utils/seed.ts 灌入现有 6 动态 + 6 作品,站点初始内容不变 - 前台内容 API:/api/news、/api/work(列表/详情,仅已发布), 页面/首页组件改为 useFetch 拉取并按 locale 选 zh/en 字段 - 后台管理 /admin(admin 中间件守卫): - Dashboard 统计、动态/作品增删改(块编辑器、图片上传 /api/admin/upload) - 用户管理(角色切换/删除)、评论审核、联系/订阅管理(导出 CSV) - 预置管理员:NUXT_ADMIN_INITIAL_PASSWORD 首次初始化创建 admin 账号 - 图片自托管:上传存 PVC /data/uploads,/uploads/[...] 路由服务 - sitemap 改查 DB;i18n 移除内容键(305 键对齐);删除 app/data 冗余模块 - 角色以数据库为准(后台可切换);admin 中间件 + 客户端会话恢复插件
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
import { readMultipartFormData, createError } from 'h3'
|
||
import { writeFileSync, mkdirSync } from 'node:fs'
|
||
import { dirname, join } from 'node:path'
|
||
import { randomBytes } from 'node:crypto'
|
||
import { requireAdmin } from '../../utils/auth'
|
||
import { getConfig } from '../../utils/config'
|
||
|
||
const MAX_SIZE = 8 * 1024 * 1024 // 8MB
|
||
const EXT_BY_TYPE: Record<string, string> = {
|
||
'image/png': 'png',
|
||
'image/jpeg': 'jpg',
|
||
'image/gif': 'gif',
|
||
'image/webp': 'webp',
|
||
'image/svg+xml': 'svg',
|
||
'image/avif': 'avif',
|
||
}
|
||
|
||
/**
|
||
* 后台图片上传 -> 保存到 PVC 的 uploads 目录(/data/uploads),
|
||
* 通过 /uploads/<name> 对外提供。
|
||
*/
|
||
export default defineEventHandler(async (event) => {
|
||
requireAdmin(event)
|
||
|
||
const parts = await readMultipartFormData(event)
|
||
const file = parts?.find((p) => p.name === 'file' && p.data?.length)
|
||
if (!file || !file.data) {
|
||
throw createError({ statusCode: 400, message: '缺少文件', data: { code: 'VALIDATION_ERROR' } })
|
||
}
|
||
|
||
const type = file.type || 'application/octet-stream'
|
||
const ext = EXT_BY_TYPE[type]
|
||
if (!ext) {
|
||
throw createError({ statusCode: 400, message: '仅支持图片(png/jpg/gif/webp/svg/avif)', data: { code: 'VALIDATION_ERROR' } })
|
||
}
|
||
if (file.data.length > MAX_SIZE) {
|
||
throw createError({ statusCode: 400, message: '文件过大(上限 8MB)', data: { code: 'VALIDATION_ERROR' } })
|
||
}
|
||
|
||
const name = `${Date.now()}-${randomBytes(6).toString('hex')}.${ext}`
|
||
const dir = join(dirname(getConfig().dbPath), 'uploads')
|
||
mkdirSync(dir, { recursive: true })
|
||
writeFileSync(join(dir, name), file.data)
|
||
|
||
return { ok: true, url: `/uploads/${name}` }
|
||
})
|