'use client' import { useState } from 'react' import { hasAllowedFileExtension, hasDragLeftDropZone, prepareFileDrag, } from './drag-drop-utils' 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 { return hasAllowedFileExtension(file, ['zip', 'rar', '7z']) } export default function StandardModelSkuZipUpload({ selectedFile, existingFileName = '', existingFileSize, uploading = false, error, onSelectFile, onRemoveFile, }: StandardModelSkuZipUploadProps) { const [isDragging, setIsDragging] = useState(false) const [validationError, setValidationError] = useState('') const hasExistingFile = !!existingFileName.trim() const handleDrop = (event: React.DragEvent) => { prepareFileDrag(event, uploading) setIsDragging(false) if (uploading) { setValidationError('文件正在上传,请等待完成后再更换') return } const file = event.dataTransfer.files?.[0] if (!file || !isSupportedZipFile(file)) { setValidationError('压缩包仅支持 ZIP、RAR、7Z 格式') return } setValidationError('') onSelectFile(file) } return (
模型压缩包 {selectedFile ? '已选择' : hasExistingFile ? '可重传' : '待上传'} {uploading && ( 上传处理中 )}
{selectedFile ? `待上传:${selectedFile.name}(${formatFileSize(selectedFile.size)})` : hasExistingFile ? `当前模型:${existingFileName}(${formatFileSize(existingFileSize)})` : '当前还没有模型压缩包,请先选择要上传的文件。'}
{(error || validationError) && (
{error || validationError}
)}
{ const file = event.target.files?.[0] if (file && isSupportedZipFile(file)) { setValidationError('') onSelectFile(file) } else if (file) { setValidationError('压缩包仅支持 ZIP、RAR、7Z 格式') } event.currentTarget.value = '' }} /> {/* biome-ignore lint/a11y/noStaticElementInteractions: 上传槽位需要拖拽和点击交互 */}
) }