dmitrybubyakin / nova-medialibrary-field

Laravel Nova field for managing the Spatie media library
MIT License
262 stars 62 forks source link

Custom FileNamer doesn't work when adding new resource #190

Open michalsrodek opened 1 month ago

michalsrodek commented 1 month ago

I use a custom file namer that changes the name of the uploaded file:

class FileNamer extends \Spatie\MediaLibrary\Support\FileNamer\FileNamer
{
    public function originalFileName(string $fileName): string
    {
        return Auth::user()->id.'-'.pathinfo($fileName, PATHINFO_FILENAME);
    }

    public function conversionFileName(string $fileName, Conversion $conversion): string
    {
        return pathinfo($fileName, PATHINFO_FILENAME).'-'.$conversion->getName();
    }

    public function responsiveFileName(string $fileName): string
    {
        return pathinfo($fileName, PATHINFO_FILENAME);
    }
}

It works just fine when I edit an existing resource. The problem arises when I create a resource. When I upload a new file, before saving the resource in Nova, the file gets uploaded, but when I check the preview, I can see the original name. However, when I save the resource, the file name changes and then everything works just fine.