You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
虽然 FS API 提供了文件读写能力,但是却无法获取到元数据(如创建,修改时间等)。当 API 不支持时,我们就需要编写 Tauri 插件来对其进行扩展。官方提供了一个名为 tauri-plugin-fs-extra 的插件,但是截止到写文章时,并不能使用任何一种方式安装到本地,所以需要开发者手动将源代码复制到本地。
// https://github.com/tauri-apps/tauri-plugin-fs-extra/blob/dev/webview-src/index.tsimport{invoke}from'@tauri-apps/api/tauri';exportinterfacePermissions{/** * `true` if these permissions describe a readonly (unwritable) file. */readonly: boolean;/** * The underlying raw `st_mode` bits that contain the standard Unix permissions for this file. */mode: number|undefined;}/** * Metadata information about a file. * This structure is returned from the `metadata` function or method * and represents known metadata about a file such as its permissions, size, modification times, etc. */exportinterfaceMetadata{/** * The last access time of this metadata. */accessedAt: Date;/** * The creation time listed in this metadata. */createdAt: Date;/** * The last modification time listed in this metadata. */modifiedAt: Date;/** * `true` if this metadata is for a directory. */isDir: boolean;/** * `true` if this metadata is for a regular file. */isFile: boolean;/** * `true` if this metadata is for a symbolic link. */isSymlink: boolean;/** * The size of the file, in bytes, this metadata is for. */size: number;/** * The permissions of the file this metadata is for. */permissions: Permissions;/** * The ID of the device containing the file. Only available on Unix. */dev: number|undefined;/** * The inode number. Only available on Unix. */ino: number|undefined;/** * The rights applied to this file. Only available on Unix. */mode: number|undefined;/** * The number of hard links pointing to this file. Only available on Unix. */nlink: number|undefined;/** * The user ID of the owner of this file. Only available on Unix. */uid: number|undefined;/** * The group ID of the owner of this file. Only available on Unix. */gid: number|undefined;/** * The device ID of this file (if it is a special one). Only available on Unix. */rdev: number|undefined;/** * The block size for filesystem I/O. Only available on Unix. */blksize: number|undefined;/** * The number of blocks allocated to the file, in 512-byte units. Only available on Unix. */blocks: number|undefined;}interfaceBackendMetadata{accessedAtMs: number;createdAtMs: number;modifiedAtMs: number;isDir: boolean;isFile: boolean;isSymlink: boolean;size: number;permissions: Permissions;dev: number|undefined;ino: number|undefined;mode: number|undefined;nlink: number|undefined;uid: number|undefined;gid: number|undefined;rdev: number|undefined;blksize: number|undefined;blocks: number|undefined;}exportasyncfunctionmetadata(path: string): Promise<Metadata>{returnawaitinvoke<BackendMetadata>('plugin:fs-extra|metadata',{ path }).then((metadata)=>{const{ accessedAtMs, createdAtMs, modifiedAtMs, ...data}=metadata;return{accessedAt: newDate(accessedAtMs),createdAt: newDate(createdAtMs),modifiedAt: newDate(modifiedAtMs),
...data,};});}exportasyncfunctionexists(path: string): Promise<boolean>{returnawaitinvoke('plugin:fs-extra|exists',{ path });}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Window 中如何访问 FS?
当
tauri.conf.json
中的 build.withGlobalTauri 设置为true
时(默认为false
),可以通过window.__TAURI__.fs
访问 fs 下的相关 API。注意:必须将 tauri.allowlist.fs 也添加到 tauri.conf.json,否则会报错:
Unhandled Promise Rejection: The `Fs` module is not enabled. You must enable one of its APIs in the allowlist.
build.withGlobalTauri
为false
时:build.withGlobalTauri
为true
时:安全
FS 模块防止路径遍历,不允许绝对路径或相对路径(即
/usr/path/to/file
或../path/to/file
路径是不被允许的)。使用 FS API 访问的路径必须与基本目录(如App
、Home
、Cache
、Desktop
、Document
、Download
等,未完全列举)之一相关,因此如果需要访问任意文件系统路径,则必须在核心层上编写此类逻辑(自行扩展,可以查看此 issues 了解更多 "core layer" is unclear)。FS API 有一个作用域配置,强制限制你使用 glob 模式的可访问路径。 作用域配置是一组描述允许的文件夹路径的全局模式。例如,以下配置仅允许访问 $APP 目录下 databases 文件夹中的文件:
如果使用未在作用域配置上的 URL 执行任何 FS API 会因拒绝访问而导致 promise 拒绝。 请注意,作用域可配置的 API,查看 FS 安全了解更多。
FS API
因 fs 相关 API 均为 异步 I/O(Asynchronous I/O) 操作,故都以 Promise 形式返回结果。需要使用基本目录时可以通过
@tauri-apps/api/path
下的 API 获取,也都以 Promise 形式返回结果。此处以文件写入举例,更多 API 使用,请自行阅读文档。实现任意路径
从 issues 作者评论可知想要绕过 Tauri FS API 定义的基本目录,需要编写自己的 Rust 命令。此处为演示代码,仅提供参考:
Step 1
Step 2
扩展文件元数据
虽然 FS API 提供了文件读写能力,但是却无法获取到元数据(如创建,修改时间等)。当 API 不支持时,我们就需要编写 Tauri 插件来对其进行扩展。官方提供了一个名为 tauri-plugin-fs-extra 的插件,但是截止到写文章时,并不能使用任何一种方式安装到本地,所以需要开发者手动将源代码复制到本地。
源码主要分为两个部分,一部分是 Rust 实现的 Tauri 插件,另一部分是插件调用:
插件部分
插件调用
注:图中的
invoke('plugin:fs-extra|metadata', { path: '/Users/lencx/.omb/canvas'})
与metadata('/Users/lencx/.omb/canvas')
是等价的。Beta Was this translation helpful? Give feedback.
All reactions