为了安全考虑,不允许你直接访问电脑中的文件,可以通过配置访问哪些文件。该模块防止路径遍历,不允许绝对路径或父目录组件(即“/usr/path/to/file”或“。./path/to/file”路径是不允许的)。使用此 API 访问的路径必须相对于基本目录之一,因此如果需要访问任意文件系统路径,则必须在核心层上编写此类逻辑。
配置的地方在tauri.config.json中:例如下面的示例,只允许你访问APPDATA中的内容
{
"tauri": {
"allowlist": {
"fs": {
"scope": ["$APPDATA/databases/*"]
}
}
}
}
那可以配置哪些文件夹呢?每个文件夹有哪些含义呢?
注意 $APPDATA 变量的使用。该值在运行时注入,解析到 app 数据目录。可用的文件夹是:
$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.
每个文件夹什么含义?有什么作用?
$APPCONFIG:
返回应用程序配置文件建议目录的路径,解析路径为 ${configDir}/${bundleIdentifier}, bundleIdentifier是tauri.config.json里面配置的。
- 用途:存放应用的配置文件。
- 数据:应用的设置和首选项,通常是 JSON 或类似格式的文件。
使用示例:
import { appConfigDir } from '@tauri-apps/api/path';
const appConfigDirPath = await appConfigDir();
$APPDATA:
appDataDir,返回应用程序数据文件建议目录的路径。解析路径为 ${ dataDir }/${ bundleIdentifier } ,其中 bundleIdentifier 是在 tauri.config.json 中配置的值 tauri.bundle.Identity。
- 用途:存放应用的数据和配置文件,通常在 Windows 上使用。
- 数据:用户相关的应用数据和配置,类似于用户的 AppData 目录
使用示例:
import { appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
$APPLOCALDATA:
返回应用程序本地数据文件建议目录的路径。解析路径为 ${ localDataDir }/${ bundleIdentifier } ,其中 bundleIdentifier 是在 tauri.con.json 中配置的值 tauri.bundle.Identity。
- 用途:用于存放本地应用数据。
- 数据:不需要同步到其他设备的本地数据,通常是特定于机器的设置。
示例:
import { appLocalDataDir } from '@tauri-apps/api/path';
const appLocalDataDirPath = await appLocalDataDir();
$APPCACHE:
- 用途:存放应用的缓存数据。
- 数据:临时文件和缓存,用于提升性能和快速访问。
使用示例:
import { appCacheDir } from '@tauri-apps/api/path';
const appCacheDirPath = await appCacheDir();
$APPLOG:
- 用途:存放应用的日志文件。
- 数据:应用运行时生成的日志,便于调试和错误跟踪。
- Linux: Resolves to
${configDir}/${bundleIdentifier}/logs. - macOS: Resolves to
${homeDir}/Library/Logs/{bundleIdentifier} - Windows: Resolves to
${configDir}/${bundleIdentifier}/logs.
示例:
import { appLogDir } from '@tauri-apps/api/path';
const appLogDirPath = await appLogDir();
$AUDIO:
- 用途:存放音频文件。
- 数据:应用使用的音频资源或用户上传的音频文件。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_MUSIC_DIR. - macOS: Resolves to
$HOME/Music. - Windows: Resolves to
{FOLDERID_Music}.
使用示例:
import { audioDir } from '@tauri-apps/api/path';
const audioDirPath = await audioDir();
$CACHE:
- 用途:存放用户的缓存数据。
- 数据:应用运行时生成的临时文件,旨在提高加载速度。
Platform-specific
- Linux: Resolves to
$XDG_CACHE_HOMEor$HOME/.cache. - macOS: Resolves to
$HOME/Library/Caches. - Windows: Resolves to
{FOLDERID_LocalAppData}.
使用示例:
import { cacheDir } from '@tauri-apps/api/path';
const cacheDirPath = await cacheDir();
$CONFIG:
- 用途:存放用户的配置文件。
- 数据:与
$APPCONFIG类似,但通常用于更通用的配置。
Platform-specific
- Linux: Resolves to
$XDG_CONFIG_HOMEor$HOME/.config. - macOS: Resolves to
$HOME/Library/Application Support. - Windows: Resolves to
{FOLDERID_RoamingAppData}.
使用示例:
import { configDir } from '@tauri-apps/api/path';
const configDirPath = await configDir();
$DATA:
- 用途:存放用户的数据文件。
- 数据:应用使用的所有数据文件,如数据库、文本文件等。
Platform-specific
- Linux: Resolves to
$XDG_DATA_HOMEor$HOME/.local/share. - macOS: Resolves to
$HOME/Library/Application Support. - Windows: Resolves to
{FOLDERID_RoamingAppData}.
使用示例:
import { dataDir } from '@tauri-apps/api/path';
const dataDirPath = await dataDir();
$LOCALDATA:
- 用途:存放本地用户数据。
- 数据:用户特定的本地数据,不会被同步。
Platform-specific
- Linux: Resolves to
$XDG_DATA_HOMEor$HOME/.local/share. - macOS: Resolves to
$HOME/Library/Application Support. - Windows: Resolves to
{FOLDERID_LocalAppData}.
使用示例:
import { localDataDir } from '@tauri-apps/api/path';
const localDataDirPath = await localDataDir();
$DESKTOP:
- 用途:存放桌面文件。
- 数据:用户桌面上的文件和快捷方式。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_DESKTOP_DIR. - macOS: Resolves to
$HOME/Desktop. - Windows: Resolves to
{FOLDERID_Desktop}.
使用示例:
import { desktopDir } from '@tauri-apps/api/path';
const desktopPath = await desktopDir();
$DOCUMENT:
- 用途:存放文档文件。
- 数据:用户的文档、文本文件等。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_DOCUMENTS_DIR. - macOS: Resolves to
$HOME/Documents. - Windows: Resolves to
{FOLDERID_Documents}.
使用示例:
import { documentDir } from '@tauri-apps/api/path';
const documentDirPath = await documentDir();
$DOWNLOAD:
- 用途:存放下载的文件。
- 数据:用户通过应用下载的文件。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_DOWNLOAD_DIR. - macOS: Resolves to
$HOME/Downloads. - Windows: Resolves to
{FOLDERID_Downloads}.
使用示例:
import { downloadDir } from '@tauri-apps/api/path';
const downloadDirPath = await downloadDir();
$EXE:
- 用途:应用的可执行文件路径。
- 数据:应用程序的主执行文件路径。
Platform-specific
- Linux: Resolves to
$XDG_BIN_HOME/../binor$XDG_DATA_HOME/../binor$HOME/.local/bin. - macOS: Not supported.
- Windows: Not supported.
使用示例:
import { executableDir } from '@tauri-apps/api/path';
const executableDirPath = await executableDir();
$FONT:
- 用途:存放字体文件。
- 数据:应用使用的自定义字体。
Platform-specific
- Linux: Resolves to
$XDG_DATA_HOME/fontsor$HOME/.local/share/fonts. - macOS: Resolves to
$HOME/Library/Fonts. - Windows: Not supported.
使用示例:
import { fontDir } from '@tauri-apps/api/path';
const fontDirPath = await fontDir();
$HOME:
- 用途:用户的主目录路径。
- 数据:用户个人数据和文件。
Platform-specific
- Linux: Resolves to
$HOME. - macOS: Resolves to
$HOME. - Windows: Resolves to
{FOLDERID_Profile}.
使用示例:
import { homeDir } from '@tauri-apps/api/path';
const homeDirPath = await homeDir();
$PICTURE:
- 用途:存放图片文件。
- 数据:用户的图片和图像文件。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_PICTURES_DIR. - macOS: Resolves to
$HOME/Pictures. - Windows: Resolves to
{FOLDERID_Pictures}.
使用示例:
import { pictureDir } from '@tauri-apps/api/path';
const pictureDirPath = await pictureDir();
$PUBLIC:
- 用途:存放公共资源。
- 数据:所有用户都可以访问的文件和资源。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_PUBLICSHARE_DIR. - macOS: Resolves to
$HOME/Public. - Windows: Resolves to
{FOLDERID_Public}.
使用示例:
import { publicDir } from '@tauri-apps/api/path';
const publicDirPath = await publicDir();
$RUNTIME:
- 用途:运行时配置。
- 数据:与应用运行环境相关的文件和数据。
Platform-specific
- Linux: Resolves to
$XDG_RUNTIME_DIR. - macOS: Not supported.
- Windows: Not supported.
使用示例:
import { runtimeDir } from '@tauri-apps/api/path';
const runtimeDirPath = await runtimeDir();
$TEMPLATE:
- 用途:存放模板文件。
- 数据:应用使用的模板资源。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_TEMPLATES_DIR. - macOS: Not supported.
- Windows: Resolves to
{FOLDERID_Templates}.
使用示例:
import { templateDir } from '@tauri-apps/api/path';
const templateDirPath = await templateDir();
$VIDEO:
- 用途:存放视频文件。
- 数据:应用使用的视频资源或用户上传的视频。
Platform-specific
- Linux: Resolves to xdg-user-dirs'
XDG_VIDEOS_DIR. - macOS: Resolves to
$HOME/Movies. - Windows: Resolves to
{FOLDERID_Videos}.
使用示例:
import { videoDir } from '@tauri-apps/api/path';
const videoDirPath = await videoDir();
$RESOURCE:
- 用途:存放应用资源文件。
- 数据:静态资源文件,如图片、样式等。
使用示例:
import { resourceDir } from '@tauri-apps/api/path';
const resourceDirPath = await resourceDir();
$TEMP:
- 用途:存放临时文件。
- 数据:应用在运行时生成的临时文件,通常在关闭时删除。
使用示例:
import { tempdir } from '@tauri-apps/api/os';
const tempdirPath = await tempdir();