gongxiangp2/components/upload/file-handlers.ts
chenpei 752b7bf999 fix: 修复批量上传和视频扩展名问题
主要修复:

- 修复批量上传闭包陷阱,防止文件丢失

- 添加 fileName 参数传递原始文件名,保留正确扩展名

- 添加取消检查回调,改进批量上传取消功能

其他改进:

- 优化搜索功能和管理后台

- 添加 FRP 故障诊断文档

- 更新环境验证和网络检测
2026-02-03 11:12:50 +08:00

97 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 文件处理函数
import type { BatchFileItem } from './upload-types'
import { BATCH_LIMITS } from './upload-constants'
import { isValidMainFileType, extractSpuFromFileName } from './upload-utils'
import { requiresRenderer, shouldAutoFillGeneral } from '@/lib/material-validation'
/**
* 获取批量上传限制
*/
export function getBatchLimit(materialType: string): number {
if (materialType === '音乐') return BATCH_LIMITS.music
if (materialType === '种草视频') return BATCH_LIMITS.video
if (materialType === '3D场景') return BATCH_LIMITS['3d']
if (materialType === '品牌VI灵感') return BATCH_LIMITS.brandVI
return Infinity
}
/**
* 处理批量文件添加
*/
export function createBatchFileItems(
files: File[],
materialType: string,
categoryName: string,
sceneName: string,
platformName: string,
productCategoryName: string,
renderer: string
): BatchFileItem[] {
return files.map((file, index) => {
const extractedSpu = extractSpuFromFileName(file.name)
return {
id: `${Date.now()}-${index}`,
file,
expanded: index === 0, // 默认展开第一个
productModel: extractedSpu,
productModelSource: extractedSpu ? 'filename' : undefined, // 从文件名提取的SPU,优先级最低,可被覆盖
categoryName: shouldAutoFillGeneral(materialType) ? '通用' : categoryName,
sceneName: shouldAutoFillGeneral(materialType) ? '通用' : sceneName,
platformName: platformName,
productCategoryName: productCategoryName,
renderer: requiresRenderer(materialType) ? renderer : undefined,
}
})
}
/**
* 验证并过滤文件
*/
export function validateAndFilterFiles(
files: File[],
materialType: string
): { valid: File[]; invalid: File[] } {
const valid: File[] = []
const invalid: File[] = []
for (const file of files) {
if (isValidMainFileType(file, materialType)) {
valid.push(file)
} else {
invalid.push(file)
}
}
return { valid, invalid }
}
/**
* 创建单文件转批量文件的项
*/
export function createBatchItemFromSingleFile(
file: File,
materialType: string,
categoryName: string,
sceneName: string,
platformName: string,
productCategoryName: string,
renderer: string
): BatchFileItem {
const extractedSpu = extractSpuFromFileName(file.name)
return {
id: `${Date.now()}-0`,
file,
expanded: true,
productModel: extractedSpu,
productModelSource: extractedSpu ? 'filename' : undefined, // 从文件名提取的SPU,优先级最低,可被覆盖
categoryName: shouldAutoFillGeneral(materialType) ? '通用' : categoryName,
sceneName: shouldAutoFillGeneral(materialType) ? '通用' : sceneName,
platformName: platformName,
productCategoryName: productCategoryName,
renderer: requiresRenderer(materialType) ? renderer : undefined,
}
}