How to overwrite a file?

I am trying to overwrite the same file by saving a jpg, but it doesn’t seem to work even though file is opened with overwrite: true set.

Having a Folder, I create a file with overwrite: true and save my image as jpg into that file.
Then change the quality of jpg compression and save again, but the file doesn’t update anymore.

I have also tried creating a new File instance once again with the same name, but it also doesn’t update the file.

Any ideas?

Can you share a code snippet?

@kerrishotts I’ll try. My code at the moment is much more elaborate than the snippet.
This works first time, but if I call export function again with different quality data is never written to the file.

  1. Ask user to select a folder, this gives folderEntry with access.
const getFile = async (filename, folderEntry) => {
  return await folderEntry.createFile(`${filename}.jpg`, {
        overwrite: true,
      })
}

const export = async (quality, token, documentId) => {
  await batchPlay([
  {
          _obj: "save",
          as: {
            _obj: "JPEG",
            extendedQuality: quality,
            matteColor: {
              _enum: "matteColor",
              _value: "none",
            },
          },
          in: {
            _path: token,
            _kind: "local",
          },
          documentID: documentId,
          lowerCase: true,
          saveStage: {
            _enum: "saveStageType",
            _value: "saveBegin",
          },
        }
])
}

const file = await getFile("Test", folderEntry)
const token = await fs.createSessionToken(file)

await export(token, 12, app.activeDocument._id)

I have also tried re-creating the file in the folder with overwrite: true and re-creating session token before trying to export again – same thing, data is not updated.
If after export I call await file.getMetadata() I can see that bytes didn’t change even though with lower quality there should be less data written into the file

I have also tried deleting the file. Here it gets even more funny.

I have a permission to folderEntry where I have created file.
If I call await file.delete() file gets deleted, but I can see an error that FileSystemProvider: You don't have permission on this file 'Test_14.jpg'.
If right after this I try to create the file again it seems to get created and I see a new file descriptor, I create a new token and call my export function. But export descriptor fails with error Error: no such file or directory

@kerrishotts
I have also tried using temporary folder and create a file there. Same problem, writing to the file again or even re-creating the file to overwrite doesn’t update file contents anymore

I have also tried creating a file in temporary folder and then deleting it but I get the same exception FileSystemProvider: You don't have permission on this file

It looks like one of the main problems is that awaiting for the save descriptor doesn’t guarantee that saving actually completed writing data into filesystem.
So calling file.getMetadata() right after saving results in No such file or directly because even though Entry was created, file was not yet saved properly so there is no file yet.

Has anyone found a workaround for the FileSystemProvider: You don't have permission on this file error?

I’m trying to delete a file that was created by a previous run of my plugin, and then rename a new file to that name.

I get this error also when ‘await entry.delete()’ is executed before saving the new file, so I don’t believe it’s caused by the save operation running in the background.

Other files returned by getEntries are deleted without this error and after the save command.

It could mean the file is in “open/locked” state by something. So it should close the reading of the file first before changing it.

I think in “fs” module you would use close for it: https://developer.adobe.com/photoshop/uxp/2022/uxp-api/reference-js/Modules/fs/#closefd-callback

But you need file desctiptor and I don’t know if FSP can provide it. Maybe you could switch everything to “fs” module.

Thanks.

  1. I was able to get fs to delete the file created by a previous run of my plugin like this:

let path = app.activeDocument.path
let lastIndexOf = path.lastIndexOf(“\”)
path = path.slice(0, lastIndexOf+1);
path = path.replaceAll(“\”, “/”)
fileName = “test_file_to_delete.tif”
const directFS = require(‘fs’)
fileDeleted = await directFS.unlink(“file:”+path+fileName);

  1. Also, I discovered that if I moved the delete operation for other files ahead of the save operation for the new file, LFS deleted all of the files without throwing the error “FileSystemProvider: You don’t have permission on this file”.

I’m unclear as to the reason the sequence of deleting unrelated files is important.

I’d still like to use fs to save a log file to the same directory as the new TIF.

Based on the examples here:

Fs requires double backlashes instead of single forward slashes on a Mac, like this:
file:c:\path\to\allowed\file.json

Which of these functions will return a string with double slashes on a Mac?
anEntry.nativePath
homedir()
document.path

And will they be the required back slashes, or will the slashes have to be reversed, like on a PC?

Thanks again!