feat: 新增视频床与文件床,视频支持 HLS 转码
ci / ci (22, ubuntu-latest) (push) Canceled after 0s

文件床
- 新增 files/ 存储前缀与 media_type 索引列,图片、视频、文件三类资源独立
- 新增文件上传、列表、删除、批量删除接口,禁止 html/htm/svg
- 支持公开直链与 Token 访问两种模式,后台可切换,直链强制附件下载

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

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

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

其他
- Docker 镜像安装 ffmpeg
- git remote 指向内网仓库
This commit is contained in:
Yeomai committed 2026-10-17 03:14:57 +08:00
1 parent 05cc3895f1
commit 89ea2fb25d
61 files changed
+6637 -154

No files matched your search

+178
View File
@@ -0,0 +1,178 @@
import { describe, expect, it } from 'vitest'
import {
buildAttachmentDisposition,
isForbiddenFileExtension,
normalizeFileContentType,
normalizeVideoContentType,
sanitizeDownloadName
} from './file-bed'
import {
extensionFromFilename,
generateFileKey,
generateVideoKey,
isVideoExtension,
validateFileKey,
validateStorageKey,
validateVideoKey
} from './image-key'
import { parseFileBedAccessMode } from './env'
describe('isForbiddenFileExtension', () => {
it('blocks html, htm and svg regardless of case', () => {
expect(isForbiddenFileExtension('html')).toBe(true)
expect(isForbiddenFileExtension('HTM')).toBe(true)
expect(isForbiddenFileExtension('svg')).toBe(true)
})
it('allows other extensions', () => {
expect(isForbiddenFileExtension('pdf')).toBe(false)
expect(isForbiddenFileExtension('')).toBe(false)
})
})
describe('sanitizeDownloadName', () => {
it('keeps unicode names and strips path segments', () => {
expect(sanitizeDownloadName('C:\\path/设计稿.pdf')).toBe('设计稿.pdf')
})
it('replaces reserved characters', () => {
expect(sanitizeDownloadName('a"b<c>.txt')).toBe('a_b_c_.txt')
})
it('falls back when the name is empty', () => {
expect(sanitizeDownloadName('')).toBe('file')
})
})
describe('buildAttachmentDisposition', () => {
it('includes an ascii fallback and a utf-8 filename', () => {
const value = buildAttachmentDisposition('报告.pdf')
expect(value.startsWith('attachment; ')).toBe(true)
expect(value).toContain('filename="__.pdf"')
expect(value).toContain(`filename*=UTF-8''${encodeURIComponent('报告.pdf')}`)
})
})
describe('normalizeFileContentType', () => {
it('keeps valid mime types', () => {
expect(normalizeFileContentType('application/pdf; charset=binary', 'pdf'))
.toBe('application/pdf')
})
it('infers from the extension when the declared type is invalid', () => {
expect(normalizeFileContentType('not a mime', 'pdf')).toBe('application/pdf')
expect(normalizeFileContentType(undefined, 'mp4')).toBe('video/mp4')
})
it('falls back to octet-stream without an extension', () => {
expect(normalizeFileContentType('', '')).toBe('application/octet-stream')
})
})
describe('extensionFromFilename', () => {
it('lowercases and sanitizes the extension', () => {
expect(extensionFromFilename('a/b/报告.PDF')).toBe('pdf')
expect(extensionFromFilename('archive.tar.gz')).toBe('gz')
})
it('returns empty for files without a usable extension', () => {
expect(extensionFromFilename('README')).toBe('')
expect(extensionFromFilename('.env')).toBe('')
})
})
describe('validateFileKey', () => {
it('accepts files/ keys with or without extension', () => {
expect(validateFileKey('files/2026/10/abc123.pdf')).toBe(true)
expect(validateFileKey('files/abc123')).toBe(true)
})
it('rejects traversal, absolute and image keys', () => {
expect(validateFileKey('files/../../etc/passwd')).toBe(false)
expect(validateFileKey('/files/a.pdf')).toBe(false)
expect(validateFileKey('images/2026/10/a.webp')).toBe(false)
expect(validateFileKey('../files/a.pdf')).toBe(false)
})
it('keeps image keys working through validateStorageKey', () => {
expect(validateStorageKey('images/2026/10/a.webp')).toBe(true)
expect(validateStorageKey('files/2026/10/a.pdf')).toBe(true)
expect(validateStorageKey('other/2026/10/a.pdf')).toBe(false)
})
})
describe('generateFileKey', () => {
const date = new Date('2026-10-16T00:00:00Z')
it('builds a dated key with the sanitized extension', () => {
expect(generateFileKey('pdf', date)).toMatch(/^files\/2026\/10\/[A-Za-z0-9]{12}\.pdf$/)
})
it('drops unknown extensions and supports flat layout', () => {
expect(generateFileKey('../../etc', date)).toMatch(/^files\/2026\/10\/[A-Za-z0-9]{12}$/)
expect(generateFileKey('', date, 'flat')).toMatch(/^files\/[A-Za-z0-9]{12}$/)
})
})
describe('parseFileBedAccessMode', () => {
it('parses known modes and rejects others', () => {
expect(parseFileBedAccessMode('public')).toBe('public')
expect(parseFileBedAccessMode(' TOKEN ')).toBe('token')
expect(parseFileBedAccessMode('other')).toBeNull()
expect(parseFileBedAccessMode(null)).toBeNull()
})
})
describe('isVideoExtension', () => {
it('accepts common video containers', () => {
expect(isVideoExtension('mp4')).toBe(true)
expect(isVideoExtension('MP4')).toBe(true)
expect(isVideoExtension('webm')).toBe(true)
expect(isVideoExtension('mkv')).toBe(true)
})
it('rejects non-video extensions', () => {
expect(isVideoExtension('png')).toBe(false)
expect(isVideoExtension('pdf')).toBe(false)
expect(isVideoExtension('')).toBe(false)
})
})
describe('validateVideoKey', () => {
it('accepts videos/ keys with a video extension', () => {
expect(validateVideoKey('videos/2026/10/abc123.mp4')).toBe(true)
expect(validateVideoKey('videos/2026/10/abc123.MKV')).toBe(true)
})
it('rejects other folders, non-video extensions and traversal', () => {
expect(validateVideoKey('files/2026/10/abc123.mp4')).toBe(false)
expect(validateVideoKey('videos/2026/10/abc123.txt')).toBe(false)
expect(validateVideoKey('videos/../../etc/passwd')).toBe(false)
expect(validateVideoKey('/videos/a.mp4')).toBe(false)
})
it('is covered by validateStorageKey', () => {
expect(validateStorageKey('videos/2026/10/a.mp4')).toBe(true)
})
})
describe('generateVideoKey', () => {
const date = new Date('2026-10-16T00:00:00Z')
it('builds a dated key and keeps the extension', () => {
expect(generateVideoKey('mp4', date)).toMatch(/^videos\/2026\/10\/[A-Za-z0-9]{12}\.mp4$/)
expect(generateVideoKey('', date)).toMatch(/^videos\/2026\/10\/[A-Za-z0-9]{12}\.mp4$/)
})
})
describe('normalizeVideoContentType', () => {
it('keeps declared video mime types', () => {
expect(normalizeVideoContentType('video/mp4', 'mp4')).toBe('video/mp4')
})
it('falls back to the extension when the declared type is not a video', () => {
expect(normalizeVideoContentType('application/octet-stream', 'mkv'))
.toBe('video/x-matroska')
expect(normalizeVideoContentType(undefined, 'm4v')).toBe('video/x-m4v')
})
})