#Help with the Path API

46 messages · Page 1 of 1 (latest)

languid goblet
#

I am working on a feature right now where i need to create a config.json file on the Rust side and read that file on the JS side.

I've looked at the FS and Path API's - but i am a little confused.

How do i save a file to a specific location like confDir on the rust side - righht now i am just writing a file to config/json local to the app install directory using an std::fs::write. Which i know is not a good solution.

Any help would be great 🙂

steel crypt
#

So if you want an app specific dir (like appConfigDir) you need access to something that implement tauri's Manager trait, meaning App, AppHandle and Window. All of them can be get somewhere like App in the setup hook or AppHandle and Window in tauri commands.

And then it'd be fairly easy, something like this: ```rs
let mut config = app_handle.path_resolver().app_config_dir().unwrap();
config_dir.push("config.json");
std::fs::write(&config, "content");

#

and then you can swap out the write function with something more custom if its behavior doesn't fit

languid goblet
#

Also, is there anyway to create a custom path for the path resolver?

steel crypt
languid goblet
#

I am probably just missing something obvious though.

#
 const config = await readTextFile('config.json', { dir: BaseDirectory.AppConfig })
 const config_json = JSON.parse(config)
 console.log(config_json)

How do i pass my custom path to the dir member of the FSOptions type?

steel crypt
#

if we're talking about the app install dir then this is intentionally not exposed in the frontend (though the resource dir is the same dir on windows) because a) it's read-only on windows if installed via admin (default right now) and b) it's always read-only after installation on linux and macos

languid goblet
steel crypt
#

and for paths in general you either have to have your own "spec" so like, just re-use the same path conventions on both sides, or send the path from rust to js via events or the command's return value

languid goblet
languid goblet
#

Question - does the dev server for tauri not run in Admin? My sidecar keeps getting a Access Denied when trying to write to the AppConfig directory.

#

I can write to that directory using the tauri api - but not from my sidecars --output arg

languid goblet
#

is their anyway to elevate just that process?

#

for some reason, the sidecar i am using needs elevated perms to write to a specific directory.

steel crypt
#

no built-in way no

#

but if it's writing to the same AppConfig dir your tauri app writes to i don't think it's actually a permission issue. sounds more like it doesn't know any better and just throws that error

#

or maybe some other app has a lock on that file (because it's reading/writing it)

languid goblet
#

yeah, it's not a lock, i am trying to download a file from the internet to that location. A release asset from a github repo.

#

i think it may be the first issue - as it throws the error in terminal no matter what unless i am in an admin terminal

languid goblet
# steel crypt no built-in way no

Thank you again - the only jank solution i can think of is to download to the local app directory and then use the tauri fs api to write the zip file to the AppConfig dir so that i can unpack the zip archive and use the files.

#

But that ... seems like a really annoying workaround.

steel crypt
#

i mean, you could download it in the tauri app (no need for tauri's fs api if you use rust, you can pipe it from reqwest straight into the fs with rust's File (or use our upload plugin which also has a download function if you want to do it in js)

#

but yes, indeed really weird that it doesn't work

#

or wait

#

i'm a bit confused by the "AppConfig" and "local app directory" mentions here. If one of them means the app's install dir then it can't work because it's read-only

#

but if you mean that the tauri app wouldn't be able to write it either so nevermind me

languid goblet
steel crypt
#

still looking for a better name for the plugin that conveys that it can do both

#

if you have any ideas hit me up kkushLUL

languid goblet
#

the upload plugin did not work either

#

Access Denied

#

yet, i can easily read and write to that directory using tauri

steel crypt
#

the plugin expects a file path that includes the file name

#

so in this case the acess denied error may come from it trying to open/write a directory as a file

languid goblet
#

i'll try that now

steel crypt
languid goblet
#

You were absolutely correct - that was the issue.