gongxiangp2/components/upload/file-handlers.ts
2026-05-07 16:37:22 +08:00

98 lines
2.9 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'
import { isBatchVideoMaterialType } from '@/lib/video-material-types'
/**
* 获取批量上传限制
*/
export function getBatchLimit(materialType: string): number {
if (materialType === '音乐') return BATCH_LIMITS.music
if (isBatchVideoMaterialType(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,
}
}