Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron JS write file permission problems

We are developing an Electron JS app which should get a configuration file from a server at one part. This worked until an hour ago, but now it "magically" throws a permission error. It throws a permission error when we try to write to anything. Here is what we explicitly tested:

  • app.getPath('userData')
  • "C:/test"
  • app.getAppPath()

We tried lauching it from an administrator elevated powershell, but still no success. This is our code snippet:

function canWrite(path, callback) {
    fs.access(path, fs.W_OK, function (err) {
        callback(null, !err);
    });
}

function downloadFile(url, target, target_name) {
    canWrite(target, function (err, isWritable) {
        if (isWritable){
            electronDl.download(
                BrowserWindow.getFocusedWindow(),
                url,
                {
                    directory: target,
                    filename: target_name
                }
            )
            console.log("Downloaded from: " + url + " to: " + target);
            return true;
        } else {
            console.log("No permission to write to target");
            return false;
        }
    });
}
downloadFile(REMOTEURL, app.getPath('userData'), 'sessionfile.json');

We rewrote this code, tried to change filenames, tried it without the filename (..) and are a bit out of ideas now. We furthermore implemented a file check (whether the file exists or not) and if so a deletion before executing this. We commented it out for now for debugging because it worked before.

Update: After somebody pointed out that the outer check is pretty useless, I updated the code to this (still doesn't work):

function downloadFile(url, target) {
    electronDl.download(
        BrowserWindow.getFocusedWindow(),
        url,
        {
            directory: target,
        }
    )
}
downloadFile(REMOTEURL, "C:/test");
like image 875
creyD Avatar asked Feb 05 '26 14:02

creyD


1 Answers

Since it appears that electron-dl doesn't give clear error messages, you may want to check/create the directory beforehand as you initially did.

The basic procedure could look like this:

  • Check if the target directory exists.
    • If it exists, check if it is writable.
      • If it is writable, proceed to downloading.
      • If it is not writable, print an informative error message and stop.
    • If it doesn't exist, try to create it.
      • If this works, proceed to downloading.
      • If this fails, print an informative error message and stop.

The following code implements this idea (using the synchronous versions of the fs methods for simplicity). Be sure to use the asynchronous versions if required.

const electronDl = require('electron-dl')
const fs = require('fs')

function ensureDirExistsAndWritable(dir) {
    if (fs.existsSync(dir)) {
        try {
            fs.accessSync(dir, fs.constants.W_OK)
        } catch (e) {
            console.error('Cannot access directory')
            return false
        }
    }
    else {
        try {
            fs.mkdirSync(dir)
        }
        catch (e) {
            if (e.code == 'EACCES') {
                console.log('Cannot create directory')
            }
            else {
                console.log(e.code)
            }
            return false
        }
    }
    return true
}


function downloadFile(url, target) {
    if (ensureDirExistsAndWritable(target) == false) {
        return
    }

    electronDl.download(
        BrowserWindow.getFocusedWindow(),
        url,
        {
            directory: target,
        }
    )
    .then(
        dl => console.log('Successfully downloaded to ' + dl.getSavePath())
    )
    .catch(
        console.log('There was an error downloading the file')
    )
}
like image 51
snwflk Avatar answered Feb 08 '26 05:02

snwflk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!