'use client' import { useState } from 'react' interface StandardModelSkuZipUploadProps { selectedFile: File | null existingFileName?: string existingFileSize?: string | number uploading?: boolean error?: string onSelectFile: (file: File) => void onRemoveFile: () => void } function formatFileSize(bytes: string | number | undefined): string { const size = typeof bytes === 'string' ? parseInt(bytes, 10) : Number(bytes || 0) if (!size || Number.isNaN(size)) { return '未知' } const units = ['B', 'KB', 'MB', 'GB'] const index = Math.min(Math.floor(Math.log(size) / Math.log(1024)), units.length - 1) return `${(size / Math.pow(1024, index)).toFixed(2)} ${units[index]}` } function isSupportedZipFile(file: File): boolean { const lowerName = file.name.toLowerCase() return ['.zip', '.rar', '.7z'].some((ext) => lowerName.endsWith(ext)) } export default function StandardModelSkuZipUpload({ selectedFile, existingFileName = '', existingFileSize, uploading = false, error, onSelectFile, onRemoveFile, }: StandardModelSkuZipUploadProps) { const [isDragging, setIsDragging] = useState(false) const hasExistingFile = !!existingFileName.trim() const handleDrop = (event: React.DragEvent) => { event.preventDefault() event.stopPropagation() setIsDragging(false) const file = event.dataTransfer.files?.[0] if (!file || !isSupportedZipFile(file)) { return } onSelectFile(file) } return (
模型压缩包 {selectedFile ? '已选择' : hasExistingFile ? '可重传' : '待上传'} {uploading && ( 上传处理中 )}
{selectedFile ? `待上传:${selectedFile.name}(${formatFileSize(selectedFile.size)})` : hasExistingFile ? `当前模型:${existingFileName}(${formatFileSize(existingFileSize)})` : '当前还没有模型压缩包,请先选择要上传的文件。'}
{error && (
{error}
)}
{ const file = event.target.files?.[0] if (file && isSupportedZipFile(file)) { onSelectFile(file) } event.currentTarget.value = '' }} /> {/* biome-ignore lint/a11y/noStaticElementInteractions: 上传槽位需要拖拽和点击交互 */}
) }