d2ef1ce4d8
新增 image-video-download.ts(选择键解析/打包请求体/响应头计数/下载 job 状态), image-video-api.ts 增加 requestVideoZipDownload(POST /download-zip, blob)。 TDD: task-106.test.ts 8 用例先 RED 后 GREEN。
73 lines
2.9 KiB
TypeScript
73 lines
2.9 KiB
TypeScript
/** 视频批量打包下载任务模型(任务 106):选择键→打包请求体、下载 job 状态与响应头计数;纯逻辑。 */
|
|
|
|
export type BatchDownloadStatus = 'idle' | 'running' | 'done' | 'failed'
|
|
|
|
export interface BatchDownloadJob {
|
|
status: BatchDownloadStatus
|
|
message?: string
|
|
fileCount?: number
|
|
errorCount?: number
|
|
}
|
|
|
|
/** 解析 "taskId:videoIndex" 卡片选择键;非法键返回 null。 */
|
|
export function parseVideoSelectionKey(key: string): { taskId: number; videoIndex: number } | null {
|
|
const match = /^(\d+):(\d+)$/.exec(typeof key === 'string' ? key.trim() : '')
|
|
if (!match) return null
|
|
return { taskId: Number(match[1]), videoIndex: Number(match[2]) }
|
|
}
|
|
|
|
/** 选择键 → POST /download-zip 请求体(去重、跳过非法键)。 */
|
|
export function toVideoZipRequest(keys: readonly string[]): { items: Array<{ task_id: number; video_index: number }> } {
|
|
const items: Array<{ task_id: number; video_index: number }> = []
|
|
const seen = new Set<string>()
|
|
for (const key of keys) {
|
|
const parsed = parseVideoSelectionKey(key)
|
|
if (!parsed) continue
|
|
const token = `${parsed.taskId}:${parsed.videoIndex}`
|
|
if (seen.has(token)) continue
|
|
seen.add(token)
|
|
items.push({ task_id: parsed.taskId, video_index: parsed.videoIndex })
|
|
}
|
|
return { items }
|
|
}
|
|
|
|
/** 大小写不敏感解析 zip 响应头里的成功/失败计数。 */
|
|
export function downloadZipHeaderCounts(headers: Record<string, string | number | undefined> | undefined): { fileCount?: number; errorCount?: number } {
|
|
const read = (name: string): number | undefined => {
|
|
if (!headers || typeof headers !== 'object') return undefined
|
|
const lower = name.toLowerCase()
|
|
for (const key of Object.keys(headers)) {
|
|
if (key.toLowerCase() === lower) {
|
|
const value = headers[key]
|
|
if (typeof value === 'number' && Number.isFinite(value)) return Math.floor(value)
|
|
if (typeof value === 'string' && /^\d+$/.test(value.trim())) return Number(value.trim())
|
|
return undefined
|
|
}
|
|
}
|
|
return undefined
|
|
}
|
|
const fileCount = read('x-archive-file-count')
|
|
const errorCount = read('x-archive-error-count')
|
|
if (fileCount === undefined && errorCount === undefined) return {}
|
|
const counts: { fileCount?: number; errorCount?: number } = {}
|
|
if (fileCount !== undefined) counts.fileCount = fileCount
|
|
if (errorCount !== undefined) counts.errorCount = errorCount
|
|
return counts
|
|
}
|
|
|
|
export function createBatchDownloadJob(): BatchDownloadJob {
|
|
return { status: 'idle' }
|
|
}
|
|
|
|
export function startBatchDownload(job: BatchDownloadJob): BatchDownloadJob {
|
|
return { status: 'running' }
|
|
}
|
|
|
|
export function succeedBatchDownload(job: BatchDownloadJob, fileCount: number, errorCount: number): BatchDownloadJob {
|
|
return { status: 'done', fileCount, errorCount }
|
|
}
|
|
|
|
export function failBatchDownload(job: BatchDownloadJob, message: string): BatchDownloadJob {
|
|
return { status: 'failed', message }
|
|
}
|