# Asset Protocol 访问问题诊断 ## 🔍 问题分析 ### 现象 - ✅ **桌面资源可以访问**: `C:\Users\imeep\Desktop\**` - ❌ **mixvideo资源无法访问**: `C:\Users\imeep\.mixvideo\**` ### 可能原因 #### 1. **AssetProtocol Scope 配置问题** 当前配置: ```json { "scope": [ "$HOME/**", "$DESKTOP/**", "$DOCUMENT/**", "$DOWNLOAD/**", "$APPDATA/**", "$LOCALAPPDATA/**", "C:\\Users\\**", "C:\\**" ] } ``` #### 2. **Windows 环境变量解析问题** - `$HOME` 在 Windows 上可能不指向 `C:\Users\imeep` - `$APPDATA` 可能不包含 `.mixvideo` 目录 #### 3. **隐藏文件夹权限问题** - `.mixvideo` 是隐藏文件夹,可能有特殊权限限制 ## 🔧 解决方案 ### 方案1: 添加具体路径 (推荐) ```json { "assetProtocol": { "enable": true, "scope": [ "$HOME/**", "$DESKTOP/**", "$DOCUMENT/**", "$DOWNLOAD/**", "$APPDATA/**", "$LOCALAPPDATA/**", "C:\\Users\\imeep\\**", "C:\\Users\\imeep\\.mixvideo\\**", "C:\\**" ] } } ``` ### 方案2: 使用通配符路径 ```json { "assetProtocol": { "enable": true, "scope": [ "C:\\Users\\*\\**", "C:\\Users\\*\\.mixvideo\\**", "**" ] } } ``` ### 方案3: 临时使用全局访问 (仅调试) ```json { "assetProtocol": { "enable": true, "scope": ["**"] } } ``` ## 🧪 测试步骤 ### 1. 在浏览器控制台测试 ```javascript // 测试 convertFileSrc 转换 import { convertFileSrc } from '@tauri-apps/api/core' // 测试桌面文件 (应该成功) const desktopPath = 'C:\\Users\\imeep\\Desktop\\test.txt' const desktopUrl = convertFileSrc(desktopPath) console.log('Desktop URL:', desktopUrl) // 测试 mixvideo 文件 (当前失败) const mixvideoPath = 'C:\\Users\\imeep\\.mixvideo\\temp\\video_segments\\test.mp4' const mixvideoUrl = convertFileSrc(mixvideoPath) console.log('Mixvideo URL:', mixvideoUrl) // 尝试访问 fetch(mixvideoUrl) .then(response => console.log('Mixvideo access:', response.status)) .catch(error => console.error('Mixvideo error:', error)) ``` ### 2. 检查实际的环境变量 在 Tauri 后端添加调试命令: ```rust #[tauri::command] fn debug_paths() -> Result { use std::env; let mut paths = std::collections::HashMap::new(); // 获取环境变量 if let Ok(home) = env::var("HOME") { paths.insert("HOME", home); } if let Ok(userprofile) = env::var("USERPROFILE") { paths.insert("USERPROFILE", userprofile); } if let Ok(appdata) = env::var("APPDATA") { paths.insert("APPDATA", appdata); } if let Ok(localappdata) = env::var("LOCALAPPDATA") { paths.insert("LOCALAPPDATA", localappdata); } // 检查 .mixvideo 目录 let mixvideo_path = format!("{}/.mixvideo", env::var("USERPROFILE").unwrap_or_default()); paths.insert("MIXVIDEO_PATH", mixvideo_path.clone()); paths.insert("MIXVIDEO_EXISTS", std::path::Path::new(&mixvideo_path).exists().to_string()); Ok(serde_json::to_value(paths).unwrap()) } ``` ### 3. 文件权限检查 ```javascript // 检查文件是否存在 import { invoke } from '@tauri-apps/api/core' const checkFile = async (path) => { try { const exists = await invoke('check_file_exists', { filePath: path }) console.log(`File exists (${path}):`, exists) return exists } catch (error) { console.error(`Check file error (${path}):`, error) return false } } // 测试不同路径 checkFile('C:\\Users\\imeep\\Desktop\\test.txt') checkFile('C:\\Users\\imeep\\.mixvideo\\temp\\video_segments\\test.mp4') ``` ## 🔧 立即修复 ### 修改 tauri.conf.json ```json { "app": { "security": { "assetProtocol": { "enable": true, "scope": [ "$HOME/**", "$DESKTOP/**", "$DOCUMENT/**", "$DOWNLOAD/**", "$APPDATA/**", "$LOCALAPPDATA/**", "C:\\Users\\imeep\\**", "C:\\Users\\imeep\\.mixvideo\\**", "C:\\**" ] } } } } ``` ### 重启应用 ```bash # 重启 Tauri 开发服务器以应用配置更改 pnpm tauri dev ``` ## 🔍 调试技巧 ### 1. 查看 Tauri 日志 在开发者工具的 Console 中查看详细错误信息: ```javascript // 启用详细日志 window.__TAURI__.logger.setLevel('debug') ``` ### 2. 检查网络请求 在开发者工具的 Network 标签中: - 查看 asset:// 协议的请求 - 检查返回的状态码 - 查看错误信息 ### 3. 比较成功和失败的URL ```javascript // 成功的桌面文件 const successUrl = convertFileSrc('C:\\Users\\imeep\\Desktop\\test.txt') console.log('Success URL:', successUrl) // 失败的 mixvideo 文件 const failUrl = convertFileSrc('C:\\Users\\imeep\\.mixvideo\\test.mp4') console.log('Fail URL:', failUrl) // 比较 URL 格式差异 ``` ## 🚨 常见错误和解决方案 ### 错误1: "Asset protocol access denied" **原因**: 文件路径不在 scope 范围内 **解决**: 添加具体路径到 scope 配置 ### 错误2: "File not found" **原因**: 文件确实不存在或路径错误 **解决**: 检查文件路径和文件是否存在 ### 错误3: "Permission denied" **原因**: 文件权限问题 **解决**: 检查文件权限,可能需要管理员权限 ### 错误4: 环境变量解析失败 **原因**: Windows 环境变量在 Tauri 中解析不正确 **解决**: 使用绝对路径而不是环境变量 ## 📋 检查清单 - [ ] 确认 assetProtocol.enable 为 true - [ ] 检查 scope 配置包含目标路径 - [ ] 重启 Tauri 应用以应用配置更改 - [ ] 确认文件确实存在 - [ ] 检查文件权限 - [ ] 测试 convertFileSrc 转换结果 - [ ] 查看浏览器控制台错误信息 - [ ] 检查网络请求状态 ## 🎯 预期结果 修复后应该能够: - ✅ 访问 `C:\Users\imeep\.mixvideo\**` 下的所有文件 - ✅ 正常播放 mixvideo 目录中的视频文件 - ✅ 加载 mixvideo 目录中的图片资源 - ✅ 在控制台看到成功的网络请求 --- *按照以上步骤应该能够解决 asset protocol 访问问题*