FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

[cake-4.x] nameCallback not working #532

Closed challgren closed 4 years ago

challgren commented 4 years ago

I'm using dev-cake-4.x and the nameCallback function is not uploading the renamed file.

Expected behavior: File is uploaded to decedent_id/nameCallback

Actual behavior File is uploaded to decedent_id/originalFilename

Possible Solutions/Issues Previously in UploadBehavior::beforeSave() $data['name'] was set using the $path->filename(). The DefaultTransformer::transform() would use $data['name'] but now it uses $this->data->getClientFileName() instead of the nameCallback provided name.

In my AppWriter.php the $path argument is decedent_id/originalFilename when I would expect it to be the decedent_id/nameCallback

Config

$this->addBehavior('Josegonzalez/Upload.Upload', [
            'attachment' => [
                'path' => '{field-value:decedent_id}{DS}',
                'fields' => [
                    'dir' => 'attachment_dir',
                    'type' => 'attachment_mine_type',
                    'size' => 'attachment_size',
                ],
                'writer' => 'App\File\Writer\AppWriter',
                'nameCallback' => function (Table $table, EntityInterface $entity, UploadedFileInterface $data, string $field, array $settings) {
                    $pathparts = pathinfo($data->getClientFilename());

                    $filename = Text::uuid() . '.' . strtolower($pathparts['extension']);

                    return $filename;
                },
                'filesystem' => [
                    'adapter' => $this->adapter,
                    'visibility' => 'private',
                ],
                'keepFilesOnDelete' => false,
            ],
        ]);
ADmad commented 4 years ago

This could be solved by passing ProcessorInterface instance as argument to TransformerInterface::transform() method.

challgren commented 4 years ago

Closing as PR #533 is open

mstroink commented 4 years ago

Please reopen the issue as #533 is closed and not merged.

ADmad commented 4 years ago

Fixed in #536