What is the permissions to set for FS plugin? #12954
-
Hello, I want to write / read a file in app local data and another in app config. So for that I have set up a very simple service (code is still very wip): import {writeFile, BaseDirectory, readFile} from '@tauri-apps/plugin-fs';
/**
* write the data in the file
* /!\ if the file doesn't exist the file is created
* /!\ if the file exist the file is overwritten
* @param data string the data you want to write in the file
* @param file 'config.json' | 'history.json' name of the file with extension
*/
export async function writeContentInFile(data: string, file: 'config.txt' | 'history.txt'): Promise<boolean> {
const encoder: TextEncoder = new TextEncoder();
const dataUint: Uint8Array = encoder.encode(data);
try {
if (file === 'config.txt') {
await writeFile(file, dataUint, {baseDir: BaseDirectory.AppConfig});
} else {
await writeFile(file, dataUint, {baseDir: BaseDirectory.AppLocalData});
}
return true;
} catch (e) {
console.log(e);
return false;
}
}
/**
* read the content of file and return it
* @param file 'config.json' | 'history.json' name of the file with extension
* @return string
*/
export async function getContentFromFile(file: 'config.txt' | 'history.txt'): Promise<string> {
const decoder: TextDecoder = new TextDecoder();
try {
if (file === 'config.txt') {
const data: Uint8Array = await readFile(file, {baseDir: BaseDirectory.AppConfig});
return decoder.decode(data);
} else {
const data: Uint8Array = await readFile(file, {baseDir: BaseDirectory.AppLocalData});
return decoder.decode(data);
}
} catch (e) {
console.log(e);
return '';
}
} Now, according to the documentation, the But when I use my function, I get (from the So I'm telling myself that my permission is not set up correctly (see below). What permission do we have to add to get the permission to read / write in local data, for example? A clear permission config example in each subsection of this section could be very useful, since they're affect the use of each function. {
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": [
"main"
],
"permissions": [
"core:default",
"opener:default",
{
"identifier": "fs:allow-write",
"allow": [
{
"path": "$APPLOCALDATA/*"
},
{
"path": "$APPCONFIG/*"
}
]
},
{
"identifier": "fs:allow-exists",
"allow": [
{
"path": "$APPLOCALDATA/*"
},
{
"path": "$APPCONFIG/*"
}
]
},
{
"identifier": "fs:allow-create",
"allow": [
{
"path": "$APPLOCALDATA/*"
},
{
"path": "$APPCONFIG/*"
}
]
},
{
"identifier": "fs:allow-appconfig-read",
"allow": [
{
"path": "$APPLOCALDATA/*"
},
{
"path": "$APPCONFIG/*"
}
]
},
{
"identifier": "fs:allow-applocaldata-read",
"allow": [
{
"path": "$APPLOCALDATA/*"
},
{
"path": "$APPCONFIG/*"
}
]
},
{
"identifier": "fs:allow-applocaldata-write",
"allow": [
{
"path": "$APPLOCALDATA/*"
},
{
"path": "$APPCONFIG/*"
}
]
},
{
"identifier": "fs:allow-appconfig-write",
"allow": [
{
"path": "$APPLOCALDATA/*"
},
{
"path": "$APPCONFIG/*"
}
]
},
{
"identifier": "http:default",
"allow": [
{
"url": "http://*"
},
{
"url": "https://*"
}
]
},
{
"identifier": "http:allow-fetch-read-body",
"allow": [
{
"url": "http://*"
},
{
"url": "https://*"
}
]
}
]
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
It will create the file but not the directory and i assume it's the directory that's still missing here. So you'll also need mkdir and its permission should be |
Beta Was this translation helpful? Give feedback.
It will create the file but not the directory and i assume it's the directory that's still missing here. So you'll also need mkdir and its permission should be
$APPLOCALDATA
(or config) without the/*