在配置的作用域上不允许使用未捕获(承诺中)路径



问题

我一直收到这个错误,read-dircreateDir工作得很好,我在这里没有错误。

代码

文件树.js

import { readDir,createDir, BaseDirectory } from '@tauri-apps/api/fs';
import { appDir } from '@tauri-apps/api/path';
let defineEntries = async () => {
console.log("Loading Data");
entries = await  readDir('test', new Uint8Array([]), { dir: BaseDirectory.Desktop, recursive: true });
console.log("Success");
}

Tauri.conf.json

"tauri": {
"allowlist": {
"fs": {
"all": true,
"readFile": true,
"writeFile": true,
"readDir": true,
"copyFile": true,
"createDir": true,
"removeDir": true,
"removeFile": true,
"renameFile": true,
"scope": ["$DESKTOP/*", "$DESKTOP/**", "$DESKTOP/test/*"]
},
"path": {
"all": true
}
}

版本

> tauriplayground@0.1.0 tauri
> tauri "info"

Environment
› OS: Windows 10.0.19044 X64
› Webview2: 103.0.1264.37
› MSVC: 
- Visual Studio Build Tools 2019
› Node.js: 16.14.2
› npm: 8.5.0
› pnpm: Not installed!
› yarn: 1.22.19
› rustup: 1.24.3
› rustc: 1.61.0
› cargo: 1.61.0
› Rust toolchain: stable-x86_64-pc-windows-msvc 
Packages
› @tauri-apps/cli [NPM]: 1.0.0
› @tauri-apps/api [NPM]: 1.0.1
› tauri [RUST]: 1.0.0,
› tauri-build [RUST]: 1.0.0,
› tao [RUST]: 0.11.2,
› wry [RUST]: 0.18.3,
App
› build-type: bundle
› CSP: unset
› distDir: ../build
› devPath: http://localhost:3000/
› framework: React
App directory structure
├─ node_modules
├─ public
├─ src
├─ src-tauri
└─ tree

我尝试过的

我将tauri.conf.json设置为:

  • "范围":["$APP/","$APP/**","$APP/树/"]
  • "范围":["$DESKTOP/**">
  • "范围":["$DESKTOP/","$DESKTOP/**","$SDESKTOP/test/"]

并在Filetree.js代码中替换了Desktop。当我使用应用时

错误

FileTree.js:23 
Uncaught (in promise) path not allowed on the configured scope: test

来自控制台的错误日志

我解决了上面的问题

新问题

我现在没有孩子从文件条目

文件结构为:

  • parent1:文件夹
    • child1:文件夹
    • child2:文件夹
  • parent2:文件夹

控制台-没有子的FileEntry

我不确定解决方案是什么,但一个想法可能是使readDir()函数不像await readDir('test', new Uint8Array([]), { dir: BaseDirectory.Desktop });那样递归,或者如果您知道文件夹的名称,那么请分别读取这些名称。如果你的应用程序只使用一种类型的文件/文件扩展名,那么你可以循环访问文件条目,并检查文件名是否以该扩展名结尾(确保包括"."之类的".exe"),如果没有,则读取目录。

https://tauri.app/v1/api/js/fs/#fs.BaseDirectory.Home"建议只允许列出用于优化捆绑包大小和安全性的API">

Tauri.conf.json

"fs": {
"all":true,
"scope": ["**"]
}

src/main

import { readDir, BaseDirectory } from "@tauri-apps/api/fs";
// Reads the `Desktop` directory recursively
const entries = await readDir("", {
dir: BaseDirectory.Desktop,
recursive: true,
});
function processEntries(entries) {
for (const entry of entries) {
console.log(`Entry: ${entry.path}`);
if (entry.children) {
processEntries(entry.children);
}
}
}
processEntries(entries);

进一步阅读:https://github.com/tauri-apps/tauri/issues/4130

相关内容

最新更新