feat: 实现视频分片上传与断点续传

- 后端:init/upload/status/complete 四个分片上传接口
- 后端:基于 Redis 的上传会话管理,支持断点续传
- 后端:分片 MD5 校验、幂等上传、顺序合并
- 测试:覆盖完整上传流程、断点续传、幂等性、哈希校验、未完成合并、状态查询
- 前端:Vue 3 分片上传组件,支持并发上传与进度展示
This commit is contained in:
yiyiis
2026-05-11 20:49:55 +08:00
parent 2d3521dec1
commit 6487f03c4a
10 changed files with 1461 additions and 376 deletions

View File

@@ -1,30 +1,68 @@
import { postForm, postJson } from './client'
import { normalizeVideoList } from './normalize'
import type { Video } from './types'
export function publishVideo(input: { title: string; description: string; play_url: string; cover_url: string }) {
return postJson<Video>('/video/publish', input, { authRequired: true })
}
export type UploadResponse = { url: string; play_url?: string; cover_url?: string }
export function uploadVideo(file: File) {
const fd = new FormData()
fd.append('file', file)
return postForm<UploadResponse>('/video/uploadVideo', fd, { authRequired: true })
}
export function uploadCover(file: File) {
const fd = new FormData()
fd.append('file', file)
return postForm<UploadResponse>('/video/uploadCover', fd, { authRequired: true })
}
export async function listByAuthorId(authorId: number) {
const videos = await postJson<Video[] | null>('/video/listByAuthorID', { author_id: authorId })
return normalizeVideoList(videos)
}
export function getDetail(id: number) {
return postJson<Video>('/video/getDetail', { id })
}
import { postForm, postJson } from './client'
import { normalizeVideoList } from './normalize'
import type { Video } from './types'
export function publishVideo(input: { title: string; description: string; play_url: string; cover_url: string }) {
return postJson<Video>('/video/publish', input, { authRequired: true })
}
export type UploadResponse = { url: string; play_url?: string; cover_url?: string }
export function uploadVideo(file: File) {
const fd = new FormData()
fd.append('file', file)
return postForm<UploadResponse>('/video/uploadVideo', fd, { authRequired: true })
}
export function uploadCover(file: File) {
const fd = new FormData()
fd.append('file', file)
return postForm<UploadResponse>('/video/uploadCover', fd, { authRequired: true })
}
export async function listByAuthorId(authorId: number) {
const videos = await postJson<Video[] | null>('/video/listByAuthorID', { author_id: authorId })
return normalizeVideoList(videos)
}
export function getDetail(id: number) {
return postJson<Video>('/video/getDetail', { id })
}
// --- Chunk Upload API ---
export type InitChunkUploadResponse = {
upload_id: string
uploaded_chunks: number[]
}
export function initChunkUpload(input: {
filename: string
file_size: number
chunk_size: number
total_chunks: number
file_hash: string
}) {
return postJson<InitChunkUploadResponse>('/video/chunk/init', input, { authRequired: true })
}
export function uploadChunk(uploadId: string, chunkIndex: number, chunkHash: string, blob: Blob) {
const fd = new FormData()
fd.append('upload_id', uploadId)
fd.append('chunk_index', String(chunkIndex))
fd.append('chunk_hash', chunkHash)
fd.append('file', blob)
return postForm<{ chunk_index: number }>('/video/chunk/upload', fd, { authRequired: true })
}
export function chunkStatus(uploadId: string) {
return postJson<{ upload_id: string; uploaded_chunks: number[]; total_chunks: number }>(
'/video/chunk/status',
{ upload_id: uploadId },
{ authRequired: true },
)
}
export function completeChunkUpload(uploadId: string) {
return postJson<UploadResponse>('/video/chunk/complete', { upload_id: uploadId }, { authRequired: true })
}