Files
FileHost/server/utils/storage/s3.ts
T
Yeomai 89ea2fb25d
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
feat: 新增视频床与文件床,视频支持 HLS 转码
文件床
- 新增 files/ 存储前缀与 media_type 索引列,图片、视频、文件三类资源独立
- 新增文件上传、列表、删除、批量删除接口,禁止 html/htm/svg
- 支持公开直链与 Token 访问两种模式,后台可切换,直链强制附件下载

视频床
- 新增 videos/ 存储前缀与视频上传接口,单文件上限 500MB
- 接入 ffmpeg 转码队列,并发数与单任务核心数可配,默认串行
- 五档转码质量:直接封装 / 速度 / 平衡 / 质量 / 压缩(压缩档可调码率比例)
- 码率控制避免转码后体积反超源文件,音频跟随源音轨不放大不压缩
- 转码进度实时上报,成功后删除源文件,失败时保留源文件与可用链接
- 上传时自动抽帧生成封面,视频与封面各有独立链接
- 删除视频时中断转码并清理切片、封面与源文件

媒体库
- 图库、视频库、文件库三套独立统计,含趋势、占用与来源分布
- 统一筛选、多选、批量删除、批量标签、网格/列表切换,显示方式按类型分别记忆
- 视频详情对齐图片详情,视频库支持在线播放(hls.js,兼容回退原文件)

修复
- 清理无主的切片、封面与开发期遗留的 ffmpeg 进程
- 源文件按设计删除后,索引不再被启动孤儿清理误删
- 视频统计口径与列表一致,转码中的视频不计入统计
- 转码失败时回收已写入的部分切片,避免残留
- 设置页停用检查更新(与上游版本号已不对应)

其他
- Docker 镜像安装 ffmpeg
- git remote 指向内网仓库
2026-10-17 03:14:57 +08:00

219 lines
5.9 KiB
TypeScript

import {
DeleteObjectCommand,
GetObjectCommand,
HeadBucketCommand,
HeadObjectCommand,
PutObjectCommand,
S3Client
} from '@aws-sdk/client-s3'
import type { Readable } from 'node:stream'
import { validateStorageKey } from '../image-key'
import type {
S3BackendConfig,
S3BackendSecrets,
ServingMode,
StoredImage,
StoredImageMeta,
StorageBackend
} from './types'
function contentTypeFromKey(key: string): string {
const ext = key.split('.').pop()?.toLowerCase()
switch (ext) {
case 'jpg':
case 'jpeg':
return 'image/jpeg'
case 'png':
return 'image/png'
case 'gif':
return 'image/gif'
case 'webp':
return 'image/webp'
case 'svg':
return 'image/svg+xml'
case 'ico':
return 'image/x-icon'
default:
return 'application/octet-stream'
}
}
export function buildObjectKey(prefix: string | undefined, key: string): string {
const trimmed = prefix?.replace(/^\/+|\/+$/g, '') ?? ''
return trimmed ? `${trimmed}/${key}` : key
}
export function parseS3Config(raw: string): S3BackendConfig | null {
try {
const parsed = JSON.parse(raw) as Partial<S3BackendConfig>
if (!parsed.endpoint || !parsed.region || !parsed.bucket) return null
return {
endpoint: parsed.endpoint,
region: parsed.region,
bucket: parsed.bucket,
prefix: parsed.prefix,
forcePathStyle: parsed.forcePathStyle ?? false
}
} catch {
return null
}
}
export function parseS3Secrets(raw: string): S3BackendSecrets | null {
try {
const parsed = JSON.parse(raw) as Partial<S3BackendSecrets>
if (!parsed.accessKeyId || !parsed.secretAccessKey) return null
return {
accessKeyId: parsed.accessKeyId,
secretAccessKey: parsed.secretAccessKey
}
} catch {
return null
}
}
export function validateS3Config(config: S3BackendConfig): string | null {
if (!config.endpoint.startsWith('http')) {
return 'Endpoint 需为 http(s) 地址'
}
if (!config.bucket.trim()) {
return 'Bucket 不能为空'
}
return null
}
export class S3StorageBackend implements StorageBackend {
readonly id: string
readonly type = 's3' as const
readonly servingMode: ServingMode
readonly publicUrl: string
private readonly client: S3Client
private readonly bucket: string
private readonly prefix: string | undefined
constructor(
id: string,
config: S3BackendConfig,
secrets: S3BackendSecrets,
servingMode: ServingMode = 'proxy',
publicUrl = ''
) {
this.id = id
this.servingMode = servingMode
this.publicUrl = publicUrl
this.bucket = config.bucket
this.prefix = config.prefix
this.client = new S3Client({
endpoint: config.endpoint,
region: config.region,
credentials: {
accessKeyId: secrets.accessKeyId,
secretAccessKey: secrets.secretAccessKey
},
forcePathStyle: config.forcePathStyle ?? false
})
}
private objectKey(key: string): string {
return buildObjectKey(this.prefix, key)
}
async put(key: string, bytes: Uint8Array, meta: StoredImageMeta): Promise<void> {
await this.client.send(new PutObjectCommand({
Bucket: this.bucket,
Key: this.objectKey(key),
Body: bytes,
ContentType: meta.contentType,
Metadata: {
originalName: meta.originalName,
uploadedAt: meta.uploadedAt,
userId: meta.userId != null ? String(meta.userId) : ''
}
}))
}
async head(key: string): Promise<StoredImage | null> {
if (!validateStorageKey(key)) return null
try {
const response = await this.client.send(new HeadObjectCommand({
Bucket: this.bucket,
Key: this.objectKey(key)
}))
const uploadedAt = response.Metadata?.uploadedat
?? response.LastModified?.toISOString()
?? new Date().toISOString()
const originalName = response.Metadata?.originalname
?? key.split('/').pop()
?? 'image'
const userIdRaw = response.Metadata?.userid
const userId = userIdRaw ? Number(userIdRaw) : null
return {
key,
size: response.ContentLength ?? 0,
mtimeMs: response.LastModified?.getTime() ?? Date.now(),
contentType: response.ContentType ?? contentTypeFromKey(key),
originalName,
uploadedAt,
userId: Number.isFinite(userId) ? userId : null,
backendId: this.id
}
} catch {
return null
}
}
async delete(key: string): Promise<void> {
await this.client.send(new DeleteObjectCommand({
Bucket: this.bucket,
Key: this.objectKey(key)
}))
}
async createStream(
key: string,
range?: { start: number, end: number }
): Promise<Readable> {
const response = await this.client.send(new GetObjectCommand({
Bucket: this.bucket,
Key: this.objectKey(key),
Range: range ? `bytes=${range.start}-${range.end}` : undefined
}))
if (!response.Body) {
throw new Error('Empty S3 response body')
}
return response.Body as Readable
}
async testConnection(): Promise<{ ok: boolean, message: string }> {
const probeKey = `__pichost-probe-${Date.now()}.txt`
const probeBytes = new TextEncoder().encode('pichost-probe')
try {
await this.client.send(new HeadBucketCommand({ Bucket: this.bucket }))
await this.client.send(new PutObjectCommand({
Bucket: this.bucket,
Key: buildObjectKey(this.prefix, probeKey),
Body: probeBytes,
ContentType: 'text/plain'
}))
await this.client.send(new DeleteObjectCommand({
Bucket: this.bucket,
Key: buildObjectKey(this.prefix, probeKey)
}))
return { ok: true, message: '连接成功' }
} catch (error) {
const message = error instanceof Error ? error.message : '连接失败'
return { ok: false, message }
}
}
async getUsageStats(): Promise<{ count: number, bytes: number }> {
return { count: 0, bytes: 0 }
}
}