diff --git a/src/ui/Desktop/Apps/File Manager/components/FileViewer.tsx b/src/ui/Desktop/Apps/File Manager/components/FileViewer.tsx index 006dd419..02fdef5b 100644 --- a/src/ui/Desktop/Apps/File Manager/components/FileViewer.tsx +++ b/src/ui/Desktop/Apps/File Manager/components/FileViewer.tsx @@ -78,14 +78,37 @@ export function FileViewer({ }: FileViewerProps) { const [editedContent, setEditedContent] = useState(content); const [hasChanges, setHasChanges] = useState(false); + const [showLargeFileWarning, setShowLargeFileWarning] = useState(false); + const [forceShowAsText, setForceShowAsText] = useState(false); const fileTypeInfo = getFileType(file.name); + // 文件大小限制 (1MB for warning, 10MB for hard limit) + const WARNING_SIZE = 1024 * 1024; // 1MB + const MAX_SIZE = 10 * 1024 * 1024; // 10MB + + // 检查是否应该显示为文本 + const shouldShowAsText = + fileTypeInfo.type === 'text' || + fileTypeInfo.type === 'code' || + (fileTypeInfo.type === 'unknown' && (forceShowAsText || !file.size || file.size <= WARNING_SIZE)); + + // 检查文件是否过大 + const isLargeFile = file.size && file.size > WARNING_SIZE; + const isTooLarge = file.size && file.size > MAX_SIZE; + // 同步外部内容更改 useEffect(() => { setEditedContent(content); setHasChanges(false); - }, [content]); + + // 如果是未知文件类型且文件较大,显示警告 + if (fileTypeInfo.type === 'unknown' && isLargeFile && !forceShowAsText) { + setShowLargeFileWarning(true); + } else { + setShowLargeFileWarning(false); + } + }, [content, fileTypeInfo.type, isLargeFile, forceShowAsText]); // 处理内容更改 const handleContentChange = (newContent: string) => { @@ -100,6 +123,17 @@ export function FileViewer({ setHasChanges(false); }; + // 处理用户确认打开大文件 + const handleConfirmOpenAsText = () => { + setForceShowAsText(true); + setShowLargeFileWarning(false); + }; + + // 处理用户拒绝打开大文件 + const handleCancelOpenAsText = () => { + setShowLargeFileWarning(false); + }; + if (isLoading) { return (
@@ -161,7 +195,66 @@ export function FileViewer({ {/* 文件内容 */}
- {fileTypeInfo.type === 'image' && ( + {/* 大文件警告对话框 */} + {showLargeFileWarning && ( +
+
+
+ +
+

Large File Warning

+

+ This file is {formatFileSize(file.size)} in size, which may cause performance issues when opened as text. +

+ {isTooLarge ? ( +
+

+ File is too large (> 10MB) and cannot be opened as text for security reasons. +

+
+ ) : ( +

+ Do you want to continue opening this file as text? This may slow down your browser. +

+ )} +
+
+ +
+ {!isTooLarge && ( + + )} + + +
+
+
+ )} + + {/* 图片预览 */} + {fileTypeInfo.type === 'image' && !showLargeFileWarning && (
)} - {(fileTypeInfo.type === 'text' || fileTypeInfo.type === 'code') && ( + {/* 文本和代码文件预览 */} + {shouldShowAsText && !showLargeFileWarning && (
{isEditable ? (