使用Github Workflows构建Tauri应用

下面这个例子是,当release分支有commit发生时,自动获取当前应用的版本信息,并使用缓存,分别构建前端和rust,并将构建好的包上传至指定tag的release上。在 platform处定义平台,这里我只选定了Win ...

2023-12-28 · 1 min · 404 words · kutake

[Tauri]Uint8Array与 Vec<u8>的坑

问题 当尝试传递 Uint8Array类型的图片数据给rust后端时,会发生 invalid type: map, expected a sequence 原因在于,tauri将 Uint8Array是作为对象而不是一个 Vec,要接受它,需要使用 HashMap<String, u8>来接收。 ...

2023-12-28 · 1 min · 133 words · kutake

Tauri 调用终端api

当你想从Tauri调用终端并运行类似 ls的命令的话,需要使用 @tauri-apps/api/shell的api,步骤如下 编辑 tauri.conf.json下的 tauri.allowlist.shell { "tauri": { "allowlist": { "shell": { "all": true, // enable all shell APIs "execute": true, // enable process spawn APIs "sidecar": true, // enable spawning sidecars "open": true // enable opening files/URLs using the default program } } } } 其他api暂时不讨论,只说 execute,假设我们需要使用git命令(案例来自官网) 编辑 scope定义好需要用到的命令,要是没有定义则无法调用 { "scope": [ { "name": "run-git-commit", "cmd": "git", "args": ["commit", "-m", { "validator": "\\S+" }] } ] } 开始调用该命令 import { Command } from '@tauri-apps/api/shell' const c = new Command('run-git-commit', ['commit', '-m', 'the commit message']) const std = await c.execute(); std.stderr std.stdout std.code std.signal 你可以将 args设置为 true来使js端可以传入任意参数 ...

2023-11-29 · 1 min · 472 words · kutake

Tauri 文件访问api

tauri支持从$APPCONFIG, $APPDATA, $APPLOCALDATA, $APPCACHE, $APPLOG, $AUDIO, $CACHE, $CONFIG, $DATA, $LOCALDATA, $DESKTOP, $DOCUMENT, $DOWNLOAD, $EXE, $FONT, $HOME, $PICTURE, $PUBLIC, $RUNTIME, $TEMPLATE, $VIDEO, $RESOURCE, $APP, $LOG, $TEMP 访问并读或文件夹和文件,具体措施如下 编辑 tauri.conf.json内的tauri.allowlist.fs { "tauri": { "allowlist": { "fs": { "all": true, // enable all FS APIs "readFile": true, "writeFile": true, "readDir": true, "copyFile": true, "createDir": true, "removeDir": true, "removeFile": true, "renameFile": true, "exists": true } } } } 可以直接 all: true 来开放所有tauri文件api,也可以单独开放某个api 编辑 scope开放具体访问权限 { "tauri": { "allowlist": { "fs": { "scope": ["$APPDATA/img/*"] } } } } 注意,这里开放了 $APPDATA下的 database访问权限,这样可以阻止程序访问任何路径,除了 database 从js中访问 import { readBinaryFile, BaseDirectory } from '@tauri-apps/api/fs'; // 可以是任何api // Read the image file in the `$APPDATA/avatar.png` path const contents = await readBinaryFile('img/avatar.png', { dir: BaseDirectory.AppData }); // 注意!一定要加上BaseDirectory.AppData 开放了哪个目录权限,就用 dir: BaseDirectory来指定访问路径,至少用绝对路径访问的方法我还没有找到,当然,这种相对路径访问的方式更加安全 ...

2023-11-29 · 1 min · 481 words · kutake