Renaming of file breaks Permalink

We have a website with a lot of filedownloads.
They are linked in a writer field (type: file).

Now we realized, that the permalink to such a file breaks, if the file is renamed.
We thought that this should not be the case, because of the uuid.

Here is what we are experiencing:

  1. Startingpoint.
    original-file-name.txt
    Uuid: iwDUjMVnLSXr0ovt

    File is linked in a writer field (type: file), filename: Original-file-name.txt

    > Link works

  2. Change of file name with «rename».
    Then open link in frontend.
    new-file-name.txt
    Uuid: iwDUjMVnLSXr0ovt

    Correct URL: domain.com/@/file/iwDUjMVnLSXr0ovt

    It opens error page

    > New-file-name ist not present in the Media folder

The links work if we do the renaming and then just visually check the link in the writer field. Which then shows the correct file with the new name (new-file-name.txt). Which for me looks like the original file is correctly handled by the backend and gets automatically updated, but the file only gets created in the Media-folder if there is a request from the backend?

I just now found this issue which seems to be related. But is it really the wanted behaviour for files?

Have you tried to use $file->changeName() instead of rename()?

Oh sorry, this was not clear enough. I meant renaming the Files in the panel, after they are uploaded.

Ok I now found what happens. Seems to be the problem described here with the missing UUID in the Cache. So I assume there are two ways to solve this?

  1. Use $field->permalinksToUrls() on the writer fields. This seems to fix the problem. But are there any performance problems with this?
  2. Use a hook on file.changeName:after and call \Kirby\Uuid\Uuids::populate(type: ‘file’)?
'file.changeName:after' => function (Kirby\Cms\File $newFile, Kirby\Cms\File $oldFile) {
            \Kirby\Uuid\Uuids::populate(type: 'file');
        }