feat: 实现视频分片上传与断点续传
- 后端:init/upload/status/complete 四个分片上传接口 - 后端:基于 Redis 的上传会话管理,支持断点续传 - 后端:分片 MD5 校验、幂等上传、顺序合并 - 测试:覆盖完整上传流程、断点续传、幂等性、哈希校验、未完成合并、状态查询 - 前端:Vue 3 分片上传组件,支持并发上传与进度展示
This commit is contained in:
55
backend/internal/video/chunk_entity.go
Normal file
55
backend/internal/video/chunk_entity.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package video
|
||||
|
||||
const ChunkSize = 5 << 20 // 5 MB
|
||||
|
||||
type ChunkUploadSession struct {
|
||||
UploadID string `json:"upload_id"`
|
||||
AccountID uint `json:"account_id"`
|
||||
Filename string `json:"filename"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
ChunkSize int64 `json:"chunk_size"`
|
||||
TotalChunks int `json:"total_chunks"`
|
||||
FileHash string `json:"file_hash"`
|
||||
UploadedBits []bool `json:"uploaded_bits"`
|
||||
}
|
||||
|
||||
func (s *ChunkUploadSession) UploadedChunks() []int {
|
||||
var indices []int
|
||||
for i, uploaded := range s.UploadedBits {
|
||||
if uploaded {
|
||||
indices = append(indices, i)
|
||||
}
|
||||
}
|
||||
return indices
|
||||
}
|
||||
|
||||
func (s *ChunkUploadSession) IsComplete() bool {
|
||||
for _, b := range s.UploadedBits {
|
||||
if !b {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type InitChunkUploadRequest struct {
|
||||
Filename string `json:"filename" binding:"required"`
|
||||
FileSize int64 `json:"file_size" binding:"required,min=1"`
|
||||
ChunkSize int64 `json:"chunk_size" binding:"required,min=1"`
|
||||
TotalChunks int `json:"total_chunks" binding:"required,min=1"`
|
||||
FileHash string `json:"file_hash" binding:"required"`
|
||||
}
|
||||
|
||||
type UploadChunkRequest struct {
|
||||
UploadID string `form:"upload_id" binding:"required"`
|
||||
ChunkIndex int `form:"chunk_index" binding:"min=0"`
|
||||
ChunkHash string `form:"chunk_hash" binding:"required"`
|
||||
}
|
||||
|
||||
type ChunkStatusRequest struct {
|
||||
UploadID string `json:"upload_id" binding:"required"`
|
||||
}
|
||||
|
||||
type CompleteChunkUploadRequest struct {
|
||||
UploadID string `json:"upload_id" binding:"required"`
|
||||
}
|
||||
Reference in New Issue
Block a user