Hello,
I need to preserve filename of certain files after they have been uploaded via the panel. This includes filename case.
Now, I am aware that there is a technical limitation with filenames that they need to be converted url-safe, and I don’t have a problem with that. What I need instead is a way to restore the original filename, say when the file is being downloaded. Saving the filename into the content file in a hook would be fine, as in:
return [
'hooks' => [
'file.create:after' => function ($file) {
// Save original filename to file contents file, but how?
}
]
]
But the hook doesn’t seem to have the original filename preserved in function parameters, and it doesn’t seem to be available via file methods either. Is there a way to accomplish this currently at all?
Hm, Kirby 2 had an option to prevent filename sanitization. Doesn’t seem to be possible in Kirby 3, though, at least I can’t find anything.
You could store the original filename in the session with a file.create:before
hook (the $upload
parameter contains the (hashed) original filename) and then retrieve this name in the file.create:after
hook.
'hooks' => [
'file.create:before' => function($file, $upload) {
$filename = $upload->filename();
$filename = substr($filename, strpos($filename, ".") + 1);
kirby()->session()->set('ofilename', $filename);
},
'file.create:after' => function($file) {
$filename = kirby()->session()->get('ofilename');
$file->update([
'originalname' => $filename,
]);
}
]
2 Likes
That works, thanks! There was just a small typo $file->upate
=> $file->update
Ah, that was why it didn’t work at first, and then started working when I copied the update code again. I’m so blind at times.