UNPKG

capacitor-advanced-file-manager

Version:

Advanced file manager plugin for Capacitor with comprehensive file system operations including browse, create, edit, delete, move, copy, and search files and directories.

429 lines (346 loc) 11.2 kB
# 🌐 Web端突破文件限制完全指南 ## 🔒 Web端文件访问限制概述 ### 浏览器安全限制 - **同源策略**:只能访问同域名资源 - **沙盒机制**:无法直接访问本地文件系统 - **用户交互要求**:文件操作需用户主动触发 - **存储配额限制**:localStorage、sessionStorage 容量有限 - **CORS限制**:跨域资源访问受限 ### 传统解决方案的局限性 - `<input type="file">` - 只能选择文件,无法访问目录结构 - `File API` - 只能处理用户选择的文件 - `localStorage` - 容量限制(通常5-10MB) - `sessionStorage` - 会话结束即清除 ## 🚀 突破限制的8大技术方案 ### 1. 🗄️ OPFS (Origin Private File System) **优势**: - 持久化存储,不受配额限制 - 可以存储大文件(GB级别) - 支持目录结构 - 性能优秀 **使用方法**: ```typescript import { WebFileBreakthroughUtils } from 'capacitor-advanced-file-manager/web-enhanced'; // 初始化 OPFS const opfs = await WebFileBreakthroughUtils.useOPFS(); // 存储大文件 const largeFile = new Blob(['x'.repeat(100 * 1024 * 1024)]); // 100MB await opfs.storeFile('large-file.bin', largeFile); // 读取文件 const file = await opfs.readFile('large-file.bin'); console.log('文件大小:', file.size); // 列出所有文件 const files = await opfs.listFiles(); console.log('OPFS中的文件:', files); ``` **浏览器支持**:Chrome 86+, Edge 86+ ### 2. 💾 IndexedDB 增强存储 **优势**: - 兼容性好,支持所有现代浏览器 - 可以存储二进制数据 - 支持事务和索引 - 容量大(通常可达几GB) **使用方法**: ```typescript // 初始化 IndexedDB const idb = await WebFileBreakthroughUtils.useIndexedDB('MyFileStorage'); // 存储文件 const file = new File(['Hello World'], 'test.txt'); await idb.storeFile('file1', file, { tags: ['important'] }); // 读取文件 const result = await idb.readFile('file1'); if (result) { console.log('文件内容:', await result.file.text()); console.log('元数据:', result.metadata); } // 列出所有文件 const fileList = await idb.listFiles(); console.log('存储的文件:', fileList); ``` ### 3. 🎯 高级拖拽访问 **优势**: - 可以访问整个目录结构 - 包括隐藏文件和子目录 - 无需用户多次选择 - 支持大量文件 **使用方法**: ```typescript // 设置拖拽区域 const dropZone = document.getElementById('drop-zone'); const handler = WebFileBreakthroughUtils.enableAdvancedDragDrop(dropZone); // 监听文件拖拽 dropZone.addEventListener('filesDropped', (e) => { const files = e.detail.files; const fileMap = e.detail.fileMap; console.log(`拖拽了 ${files.length} 个文件`); // 显示目录结构 for (const [path, file] of fileMap) { console.log(`${path} - ${file.size} bytes`); } }); ``` **HTML 设置**: ```html <div id="drop-zone" style=" width: 300px; height: 200px; border: 2px dashed #ccc; text-align: center; line-height: 200px; "> 拖拽文件或文件夹到这里 </div> ``` ### 4. 🌊 Web Streams 大文件处理 **优势**: - 可以处理超大文件 - 内存占用低 - 支持流式处理 - 实时处理数据 **使用方法**: ```typescript // 创建文件流 const file = new File(['x'.repeat(1000000)], 'large.txt'); const stream = WebFileBreakthroughUtils.createFileStream(file, 64 * 1024); // 流式处理 const reader = stream.getReader(); let totalSize = 0; while (true) { const { done, value } = await reader.read(); if (done) break; totalSize += value.length; console.log(`已处理: ${totalSize} bytes`); // 在这里处理数据块 // 例如:计算哈希、压缩、上传等 } ``` ### 5. 🔧 Service Worker 缓存 **优势**: - 可以拦截网络请求 - 实现离线文件访问 - 持久化缓存 - 支持复杂的缓存策略 **使用方法**: ```typescript // 设置 Service Worker 缓存 const swCache = await WebFileBreakthroughUtils.setupServiceWorkerCache(); // 缓存文件 const file = new Blob(['缓存的内容']); await swCache.cacheFile('/cached-file/important.txt', file); // 获取缓存的文件 const cachedFile = await swCache.getCachedFile('/cached-file/important.txt'); if (cachedFile) { console.log('从缓存获取文件:', await cachedFile.text()); } ``` ### 6. 🔗 WebRTC P2P 文件传输 **优势**: - 绕过服务器限制 - 直接在浏览器间传输 - 支持大文件传输 - 实时传输 **使用方法**: ```typescript // 创建 P2P 连接 const p2p = WebFileBreakthroughUtils.createP2PFileTransfer(); // 发送端 const connection = await p2p.createConnection('peer-id-123'); const file = new File(['Hello P2P'], 'message.txt'); await p2p.sendFile('peer-id-123', file); // 接收端需要建立对应的连接和数据通道处理 ``` ### 7. ⚡ WebAssembly 高性能处理 **优势**: - 接近原生性能 - 支持复杂算法 - 可以进行文件压缩、加密等 - 跨平台兼容 **使用方法**: ```typescript // 加载 WASM 模块 const wasmProcessor = await WebFileBreakthroughUtils.loadWasmFileProcessor('/file-processor.wasm'); // 压缩文件 const fileData = new Uint8Array(await file.arrayBuffer()); const compressed = wasmProcessor.compressFile(fileData); // 计算哈希 const hash = wasmProcessor.calculateHash(fileData); console.log('文件哈希:', hash); ``` ### 8. 📦 分片下载突破限制 **优势**: - 可以下载大文件 - 支持断点续传 - 并发下载提高速度 - 绕过单次下载限制 **使用方法**: ```typescript // 分片下载大文件 const blob = await WebFileBreakthroughUtils.chunkedDownload( 'https://example.com/large-file.zip', { chunkSize: 1024 * 1024, // 1MB 分片 maxConcurrent: 4, // 4个并发连接 onProgress: (progress) => { console.log(`下载进度: ${(progress * 100).toFixed(1)}%`); } } ); console.log('下载完成,文件大小:', blob.size); // 创建下载链接 const downloadUrl = await WebFileBreakthroughUtils.createDownloadLink( blob, 'downloaded-file.zip', { autoDownload: true } ); ``` ## 🔧 集成使用示例 ### 完整的Web文件管理器示例 ```typescript import { WebEnhancedFileManager, WebFileBreakthroughUtils } from 'capacitor-advanced-file-manager/web-enhanced'; class AdvancedWebFileManager { private vfs: any; private opfs: any; private idb: any; async initialize() { // 初始化增强的Web文件系统 const result = await WebEnhancedFileManager.initializeWebFS({ useOPFS: true, enableIndexedDBCache: true, useWebWorkers: true, enableStreaming: true, maxCacheSize: 100 * 1024 * 1024 // 100MB }); if (result.success) { console.log('✅ Web文件系统初始化成功'); // 初始化各种存储方案 this.opfs = await WebFileBreakthroughUtils.useOPFS(); this.idb = await WebFileBreakthroughUtils.useIndexedDB(); this.vfs = await WebEnhancedFileManager.createVirtualFS(); } } async handleFileUpload(files: File[]) { for (const file of files) { try { // 小文件存储到 IndexedDB if (file.size < 10 * 1024 * 1024) { // 10MB await this.idb.storeFile(file.name, file); console.log(`✅ ${file.name} 存储到 IndexedDB`); } // 大文件存储到 OPFS else { await this.opfs.storeFile(file.name, file); console.log(`✅ ${file.name} 存储到 OPFS`); } // 添加到虚拟文件系统 await WebEnhancedFileManager.addToVirtualFS(this.vfs, file, `/${file.name}`); } catch (error) { console.error(`❌ ${file.name} 存储失败:`, error); } } } async setupDragDrop() { const dropZone = document.getElementById('file-drop-zone'); if (!dropZone) return; // 启用高级拖拽 const handler = WebFileBreakthroughUtils.enableAdvancedDragDrop(dropZone); dropZone.addEventListener('filesDropped', async (e: any) => { const files = e.detail.files; await this.handleFileUpload(files); }); } async downloadLargeFile(url: string) { try { const blob = await WebFileBreakthroughUtils.chunkedDownload(url, { chunkSize: 2 * 1024 * 1024, // 2MB chunks maxConcurrent: 6, onProgress: (progress) => { this.updateProgressBar(progress * 100); } }); // 存储下载的文件 const filename = url.split('/').pop() || 'downloaded-file'; await this.opfs.storeFile(filename, blob); console.log('✅ 大文件下载并存储成功'); } catch (error) { console.error('❌ 下载失败:', error); } } private updateProgressBar(percentage: number) { const progressBar = document.getElementById('progress-bar'); if (progressBar) { progressBar.style.width = `${percentage}%`; progressBar.textContent = `${percentage.toFixed(1)}%`; } } } // 使用示例 const fileManager = new AdvancedWebFileManager(); await fileManager.initialize(); await fileManager.setupDragDrop(); ``` ## 🌟 最佳实践 ### 1. 渐进式增强 ```typescript // 检查浏览器能力 const capabilities = await WebEnhancedFileManager.checkWebCapabilities(); if (capabilities.opfs) { // 使用 OPFS } else if (capabilities.indexedDB) { // 降级到 IndexedDB } else { // 使用内存存储 } ``` ### 2. 错误处理和降级 ```typescript async function storeFileWithFallback(file: File) { try { // 尝试 OPFS await opfs.storeFile(file.name, file); } catch (error) { try { // 降级到 IndexedDB await idb.storeFile(file.name, file); } catch (error2) { // 最后降级到内存 await vfs.addFile(file); } } } ``` ### 3. 性能优化 ```typescript // 使用 Web Workers 处理大文件 if (file.size > 50 * 1024 * 1024) { // 50MB await WebEnhancedFileManager.processFileWithWorker( file, 'compress', { quality: 0.8 } ); } ``` ## 🔍 浏览器兼容性 | 技术方案 | Chrome | Firefox | Safari | Edge | |---------|--------|---------|--------|------| | OPFS | 86+ | ❌ | ❌ | 86+ | | IndexedDB | ✅ | ✅ | ✅ | ✅ | | File System Access API | 86+ | ❌ | ❌ | 86+ | | Web Streams | 78+ | 65+ | 14.1+ | 79+ | | WebRTC | ✅ | ✅ | ✅ | ✅ | | WebAssembly | ✅ | ✅ | ✅ | ✅ | | Service Worker | ✅ | ✅ | ✅ | ✅ | ## 🚨 注意事项 1. **隐私和安全**:始终遵循浏览器安全策略 2. **用户体验**:提供清晰的权限请求说明 3. **性能考虑**:大文件操作使用流式处理 4. **兼容性**:提供降级方案 5. **存储管理**:定期清理不需要的缓存文件 ## 📚 更多资源 - [File System Access API 文档](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API) - [Origin Private File System 规范](https://fs.spec.whatwg.org/) - [IndexedDB 完整指南](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) - [Web Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) 通过这些技术方案的组合使用,可以在Web端实现接近原生应用的文件管理功能!🚀