feat: 全面升级——后台优化 / 动态配置 / 个人中心 / OSS 存储
- OSS 文件存储(RustFS / S3 v4 兼容,零依赖手写 SigV4): 上传/列表/删除走桶 arcticnewone,/uploads 代理服务 + 本地回退; server/utils/sigv4.ts 纯函数 + AWS 测试向量验证 - 业务动态配置:settings 表 + /api/settings(公开) + /api/admin/settings(PATCH), useSiteSettings 组合式,footer/contact/tools/app.vue/侧边栏/工具页全部接入, 后台 /admin/settings 修改前台即时生效(站点/联系/社交/工具箱/页脚/SEO) - 后台全面优化:Admin* 设计系统(按钮/输入/表单/卡片/徽标/弹窗/确认/空态/骨架/ 分页/页头/Toast)14 组件;Dashboard 增强;各列表页搜索/筛选/批量删除/分页; 布局与侧边栏分组;统一 toast/确认反馈 - 个人中心全新重设计:顶部资料卡 + 统计、精致化改密/删号(二次确认)、 收藏封面网格、评论卡片、移动端适配 - deploy.sh 支持 OSS 凭据注入(NUXT_OSS_*);.env.example 更新 - 测试 17 例全过(含 SigV4 AWS 向量、settings 默认结构)
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import { getConfig } from './config'
|
||||
import { sha256Hex, signRequest } from './sigv4'
|
||||
|
||||
/**
|
||||
* 极简 S3 客户端(RustFS / S3 v4 兼容,零第三方依赖)。
|
||||
* 手写 AWS SigV4 签名:putObject / getObject / deleteObject / listObjectsV2。
|
||||
* 配置来自 env(NUXT_OSS_*),凭据绝不写入仓库。
|
||||
*/
|
||||
export interface OssConfig {
|
||||
endpoint: string
|
||||
accessKey: string
|
||||
secretKey: string
|
||||
bucket: string
|
||||
region: string
|
||||
publicBase: string
|
||||
}
|
||||
|
||||
export function getOssConfig(): OssConfig | null {
|
||||
const { oss } = getConfig()
|
||||
if (!oss.endpoint || !oss.accessKey || !oss.secretKey || !oss.bucket) return null
|
||||
return oss
|
||||
}
|
||||
|
||||
export const isOssConfigured = (): boolean => getOssConfig() !== null
|
||||
|
||||
const encodeKey = (key: string) => key.split('/').map(encodeURIComponent).join('/')
|
||||
const qs = (q: Record<string, string>) => {
|
||||
const s = Object.keys(q)
|
||||
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(String(q[k]))}`)
|
||||
.join('&')
|
||||
return s ? `?${s}` : ''
|
||||
}
|
||||
|
||||
async function s3Request(
|
||||
method: string,
|
||||
key: string,
|
||||
opts: { query?: Record<string, string>; body?: string | Buffer; contentType?: string; isList?: boolean } = {},
|
||||
): Promise<Response> {
|
||||
const cfg = getOssConfig()
|
||||
if (!cfg) throw new Error('OSS 未配置')
|
||||
|
||||
const path = opts.isList ? `/${cfg.bucket}` : `/${cfg.bucket}/${encodeKey(key)}`
|
||||
const body = opts.body ?? ''
|
||||
const bodyHash = sha256Hex(body)
|
||||
const date = new Date().toISOString()
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
host: cfg.endpoint,
|
||||
'x-amz-content-sha256': bodyHash,
|
||||
...(opts.contentType ? { 'content-type': opts.contentType } : {}),
|
||||
}
|
||||
|
||||
const { authorization, amzDate } = signRequest({
|
||||
method,
|
||||
path,
|
||||
query: opts.query ?? {},
|
||||
headers,
|
||||
bodyHash,
|
||||
date,
|
||||
accessKey: cfg.accessKey,
|
||||
secretKey: cfg.secretKey,
|
||||
region: cfg.region,
|
||||
})
|
||||
headers['x-amz-date'] = amzDate
|
||||
|
||||
const url = `https://${cfg.endpoint}${path}${qs(opts.query ?? {})}`
|
||||
return fetch(url, {
|
||||
method,
|
||||
headers: { ...headers, authorization },
|
||||
body: method === 'GET' || method === 'DELETE' ? undefined : body,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------- 对象操作 ----------
|
||||
export async function putObject(key: string, body: Buffer, contentType: string): Promise<string> {
|
||||
const res = await s3Request('PUT', key, { body, contentType })
|
||||
if (!res.ok) throw new Error(`OSS 上传失败: ${res.status}`)
|
||||
return key
|
||||
}
|
||||
|
||||
export async function getObject(key: string): Promise<Response | null> {
|
||||
const res = await s3Request('GET', key)
|
||||
if (res.status === 404) return null
|
||||
if (!res.ok) throw new Error(`OSS 读取失败: ${res.status}`)
|
||||
return res
|
||||
}
|
||||
|
||||
export async function deleteObject(key: string): Promise<boolean> {
|
||||
const res = await s3Request('DELETE', key)
|
||||
return res.ok || res.status === 204
|
||||
}
|
||||
|
||||
export interface OssFile {
|
||||
name: string
|
||||
size: number
|
||||
modified: string
|
||||
url: string
|
||||
}
|
||||
|
||||
export async function listObjectsV2(prefix = ''): Promise<OssFile[]> {
|
||||
const res = await s3Request('GET', '', {
|
||||
isList: true,
|
||||
query: { 'list-type': '2', prefix, 'max-keys': '1000' },
|
||||
})
|
||||
if (!res.ok) throw new Error(`OSS 列表失败: ${res.status}`)
|
||||
const xml = await res.text()
|
||||
|
||||
const files: OssFile[] = []
|
||||
const contentRe = /<Contents>([\s\S]*?)<\/Contents>/g
|
||||
let m: RegExpExecArray | null
|
||||
while ((m = contentRe.exec(xml))) {
|
||||
const item = m[1]
|
||||
const name = /<Key>([\s\S]*?)<\/Key>/.exec(item)?.[1]
|
||||
if (!name) continue
|
||||
const size = Number(/<Size>([\s\S]*?)<\/Size>/.exec(item)?.[1] || 0)
|
||||
const modified = /<LastModified>([\s\S]*?)<\/LastModified>/.exec(item)?.[1] || ''
|
||||
files.push({ name, size, modified, url: `/uploads/${encodeKey(name)}` })
|
||||
}
|
||||
files.sort((a, b) => b.modified.localeCompare(a.modified))
|
||||
return files
|
||||
}
|
||||
Reference in New Issue
Block a user