release: v1.1.0 — S3 multi-backend storage and admin UI
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
GitHub Release / release (push) Canceled after 0s
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
GitHub Release / release (push) Canceled after 0s
This commit is contained in:
1 parent
7c45a78572
commit
acc64751ef
46 files changed
+4005
-480
No files matched your search
@@ -0,0 +1,39 @@
|
||||
import { requireAdminAuth } from '../../../utils/access'
|
||||
import { createApiError } from '../../../utils/api-error'
|
||||
import {
|
||||
deleteStorageBackend,
|
||||
isStorageEnvConfigured,
|
||||
LOCAL_BACKEND_ID
|
||||
} from '../../../utils/storage-backends'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
|
||||
if (isStorageEnvConfigured()) {
|
||||
createApiError(event, 'FORBIDDEN', '存储配置已由环境变量覆盖,无法在后台修改', 409)
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
createApiError(event, 'INVALID_REQUEST', '缺少存储后端 ID', 400)
|
||||
}
|
||||
|
||||
if (id === LOCAL_BACKEND_ID) {
|
||||
createApiError(event, 'INVALID_REQUEST', '本地磁盘后端不可删除', 400)
|
||||
}
|
||||
|
||||
try {
|
||||
deleteStorageBackend(id)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '删除失败'
|
||||
if (message.includes('stored images')) {
|
||||
createApiError(event, 'INVALID_REQUEST', '该后端仍有图片,无法删除', 400)
|
||||
}
|
||||
if (message.includes('default backend')) {
|
||||
createApiError(event, 'INVALID_REQUEST', '请先切换默认后端后再删除', 400)
|
||||
}
|
||||
createApiError(event, 'INVALID_REQUEST', message, 400)
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
})
|
||||
@@ -0,0 +1,129 @@
|
||||
import { requireAdminAuth } from '../../../utils/access'
|
||||
import { createApiError } from '../../../utils/api-error'
|
||||
import {
|
||||
isStorageEnvConfigured,
|
||||
LOCAL_BACKEND_ID,
|
||||
updateStorageBackend,
|
||||
getStorageBackendRow,
|
||||
setDefaultBackend,
|
||||
type StorageBackendPatch
|
||||
} from '../../../utils/storage-backends'
|
||||
import { parseS3Config, validateS3Config } from '../../../utils/storage/s3'
|
||||
import { parseQuotaBytes } from '../../../utils/storage-capacity'
|
||||
import type { ServingMode } from '../../../utils/storage/types'
|
||||
|
||||
interface StoragePatchBody {
|
||||
name?: string
|
||||
config?: {
|
||||
endpoint?: string
|
||||
region?: string
|
||||
bucket?: string
|
||||
prefix?: string
|
||||
forcePathStyle?: boolean
|
||||
}
|
||||
secrets?: {
|
||||
accessKeyId?: string
|
||||
secretAccessKey?: string
|
||||
}
|
||||
servingMode?: ServingMode
|
||||
publicUrl?: string
|
||||
quotaBytes?: number | null
|
||||
enabled?: boolean
|
||||
isDefault?: boolean
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
|
||||
if (isStorageEnvConfigured()) {
|
||||
createApiError(event, 'FORBIDDEN', '存储配置已由环境变量覆盖,无法在后台修改', 409)
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
createApiError(event, 'INVALID_REQUEST', '缺少存储后端 ID', 400)
|
||||
}
|
||||
|
||||
const row = getStorageBackendRow(id)
|
||||
if (!row) {
|
||||
createApiError(event, 'INVALID_REQUEST', '存储后端不存在', 404)
|
||||
}
|
||||
|
||||
const body = await readBody<StoragePatchBody>(event).catch(
|
||||
(): StoragePatchBody => ({})
|
||||
)
|
||||
|
||||
if (id === LOCAL_BACKEND_ID && body.isDefault === false) {
|
||||
createApiError(event, 'INVALID_REQUEST', '必须保留一个默认存储后端', 400)
|
||||
}
|
||||
|
||||
const patch: StorageBackendPatch = {}
|
||||
|
||||
if (body.name !== undefined) {
|
||||
patch.name = body.name.trim() || row.name
|
||||
}
|
||||
|
||||
if (body.config !== undefined && row.type === 's3') {
|
||||
const current = parseS3Config(row.config_json) ?? {
|
||||
endpoint: '',
|
||||
region: 'auto',
|
||||
bucket: ''
|
||||
}
|
||||
const next = {
|
||||
endpoint: body.config.endpoint?.trim() ?? current.endpoint,
|
||||
region: body.config.region?.trim() ?? current.region,
|
||||
bucket: body.config.bucket?.trim() ?? current.bucket,
|
||||
prefix: body.config.prefix?.trim() || undefined,
|
||||
forcePathStyle: body.config.forcePathStyle ?? current.forcePathStyle
|
||||
}
|
||||
const error = validateS3Config(next)
|
||||
if (error) {
|
||||
createApiError(event, 'INVALID_REQUEST', error, 400)
|
||||
}
|
||||
patch.config = next
|
||||
}
|
||||
|
||||
if (body.secrets !== undefined && row.type === 's3') {
|
||||
patch.secrets = body.secrets
|
||||
}
|
||||
|
||||
if (body.servingMode !== undefined) {
|
||||
if (body.servingMode !== 'proxy' && body.servingMode !== 'public') {
|
||||
createApiError(event, 'INVALID_REQUEST', '访问方式无效', 400)
|
||||
}
|
||||
patch.servingMode = body.servingMode
|
||||
}
|
||||
|
||||
if (body.publicUrl !== undefined) {
|
||||
patch.publicUrl = body.publicUrl.trim()
|
||||
}
|
||||
|
||||
if (body.quotaBytes !== undefined) {
|
||||
if (row.type === 's3') {
|
||||
const quotaBytes = parseQuotaBytes(body.quotaBytes)
|
||||
if (!quotaBytes) {
|
||||
createApiError(event, 'INVALID_REQUEST', '请填写有效的存储套餐总量', 400)
|
||||
}
|
||||
patch.quotaBytes = quotaBytes
|
||||
}
|
||||
}
|
||||
|
||||
if (body.enabled !== undefined) {
|
||||
if (id === LOCAL_BACKEND_ID && !body.enabled) {
|
||||
createApiError(event, 'INVALID_REQUEST', '本地磁盘后端不可停用', 400)
|
||||
}
|
||||
patch.enabled = body.enabled
|
||||
}
|
||||
|
||||
if (body.isDefault !== undefined) {
|
||||
patch.isDefault = body.isDefault
|
||||
}
|
||||
|
||||
updateStorageBackend(id, patch)
|
||||
|
||||
if (body.isDefault) {
|
||||
setDefaultBackend(id)
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
})
|
||||
@@ -0,0 +1,34 @@
|
||||
import { requireAdminAuth } from '../../../../utils/access'
|
||||
import { createApiError } from '../../../../utils/api-error'
|
||||
import {
|
||||
createBackendInstance,
|
||||
getStorageBackendRow,
|
||||
isStorageEnvConfigured
|
||||
} from '../../../../utils/storage-backends'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
|
||||
if (isStorageEnvConfigured()) {
|
||||
createApiError(event, 'FORBIDDEN', '存储配置已由环境变量覆盖,无法在后台测试', 409)
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
createApiError(event, 'INVALID_REQUEST', '缺少存储后端 ID', 400)
|
||||
}
|
||||
|
||||
const row = getStorageBackendRow(id)
|
||||
if (!row) {
|
||||
createApiError(event, 'INVALID_REQUEST', '存储后端不存在', 404)
|
||||
}
|
||||
|
||||
try {
|
||||
const backend = createBackendInstance(row)
|
||||
const result = await backend.testConnection()
|
||||
return result
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '连接失败'
|
||||
return { ok: false, message }
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,39 @@
|
||||
import { requireAdminAuth } from '../../../utils/access'
|
||||
import {
|
||||
getBackendUsageStats,
|
||||
isStorageEnvConfigured,
|
||||
listStorageBackends
|
||||
} from '../../../utils/storage-backends'
|
||||
import { buildBackendCapacity } from '../../../utils/storage-capacity'
|
||||
import { getActiveBackendRow } from '../../../utils/storage/resolver'
|
||||
import type { StorageBackendInfo } from '../../../utils/storage/types'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
|
||||
const rows = listStorageBackends()
|
||||
const backends = await Promise.all(rows.map(async (backend: StorageBackendInfo & {
|
||||
secretsMasked: Record<string, string>
|
||||
}) => {
|
||||
const usage = getBackendUsageStats(backend.id)
|
||||
const capacity = await buildBackendCapacity(
|
||||
backend.id,
|
||||
backend.type,
|
||||
usage.bytes,
|
||||
backend.quotaBytes
|
||||
)
|
||||
return {
|
||||
...backend,
|
||||
usage,
|
||||
capacity
|
||||
}
|
||||
}))
|
||||
|
||||
const activeBackend = getActiveBackendRow()
|
||||
|
||||
return {
|
||||
backends,
|
||||
activeBackendId: activeBackend?.id ?? 'local',
|
||||
envOverride: isStorageEnvConfigured()
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,83 @@
|
||||
import { requireAdminAuth } from '../../../utils/access'
|
||||
import { createApiError } from '../../../utils/api-error'
|
||||
import {
|
||||
insertStorageBackend,
|
||||
isStorageEnvConfigured
|
||||
} from '../../../utils/storage-backends'
|
||||
import { validateS3Config } from '../../../utils/storage/s3'
|
||||
import { parseQuotaBytes } from '../../../utils/storage-capacity'
|
||||
import type { ServingMode } from '../../../utils/storage/types'
|
||||
|
||||
interface StorageCreateBody {
|
||||
name?: string
|
||||
config?: {
|
||||
endpoint?: string
|
||||
region?: string
|
||||
bucket?: string
|
||||
prefix?: string
|
||||
forcePathStyle?: boolean
|
||||
}
|
||||
secrets?: {
|
||||
accessKeyId?: string
|
||||
secretAccessKey?: string
|
||||
}
|
||||
servingMode?: ServingMode
|
||||
publicUrl?: string
|
||||
quotaBytes?: number | null
|
||||
enabled?: boolean
|
||||
isDefault?: boolean
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdminAuth(event)
|
||||
|
||||
if (isStorageEnvConfigured()) {
|
||||
createApiError(event, 'FORBIDDEN', '存储配置已由环境变量覆盖,无法在后台修改', 409)
|
||||
}
|
||||
|
||||
const body = await readBody<StorageCreateBody>(event).catch(
|
||||
(): StorageCreateBody => ({})
|
||||
)
|
||||
|
||||
const name = body.name?.trim() || 'S3 兼容存储'
|
||||
const config = {
|
||||
endpoint: body.config?.endpoint?.trim() ?? '',
|
||||
region: body.config?.region?.trim() || 'auto',
|
||||
bucket: body.config?.bucket?.trim() ?? '',
|
||||
prefix: body.config?.prefix?.trim() || undefined,
|
||||
forcePathStyle: body.config?.forcePathStyle ?? false
|
||||
}
|
||||
|
||||
const error = validateS3Config(config)
|
||||
if (error) {
|
||||
createApiError(event, 'INVALID_REQUEST', error, 400)
|
||||
}
|
||||
|
||||
const accessKeyId = body.secrets?.accessKeyId?.trim() ?? ''
|
||||
const secretAccessKey = body.secrets?.secretAccessKey?.trim() ?? ''
|
||||
if (!accessKeyId || !secretAccessKey) {
|
||||
createApiError(event, 'INVALID_REQUEST', 'Access Key 与 Secret Key 不能为空', 400)
|
||||
}
|
||||
|
||||
if (body.servingMode && body.servingMode !== 'proxy' && body.servingMode !== 'public') {
|
||||
createApiError(event, 'INVALID_REQUEST', '访问方式无效', 400)
|
||||
}
|
||||
|
||||
const quotaBytes = parseQuotaBytes(body.quotaBytes)
|
||||
if (!quotaBytes) {
|
||||
createApiError(event, 'INVALID_REQUEST', '请填写有效的存储套餐总量', 400)
|
||||
}
|
||||
|
||||
const id = insertStorageBackend({
|
||||
name,
|
||||
config,
|
||||
secrets: { accessKeyId, secretAccessKey },
|
||||
servingMode: body.servingMode,
|
||||
publicUrl: body.publicUrl?.trim(),
|
||||
quotaBytes,
|
||||
enabled: body.enabled,
|
||||
isDefault: body.isDefault
|
||||
})
|
||||
|
||||
return { success: true, id }
|
||||
})
|
||||
Reference in new issue
Block a user