FriendsOfCake / cakephp-upload

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

Solved: Using transformer does not upload video and entity isn't saved #585

Closed magic-77 closed 2 years ago

magic-77 commented 2 years ago

josegonzalez/cakephp-upload Version: 4.0.2 CakePHP 3.9.2

I'm using one field "media" to upload either an Image or Video For the Image i need to use an "transformer" But as soon i try to upload an video nothing happen. No Error is Thrown

$this->addBehavior('Josegonzalez/Upload.Upload', [
            'media' => [
                'path' => 'webroot{DS}uploads{DS}saved_searches{DS}',
                'keepFilesOnDelete' => false,
                'restoreValueOnFailure' => false,
                'nameCallback' => function (RepositoryInterface $table, EntityInterface $entity, $data, $field, $settings) {
                    $extension = pathinfo($data['name'], PATHINFO_EXTENSION);
                    $filename = $this->sanitizeFileName(pathinfo($data['name'], PATHINFO_FILENAME), uniqid());

                    return $filename . '.' . $extension;
                },
                'transformer' => function (RepositoryInterface $table, EntityInterface $entity, $data, $field, $settings) {
                    $filename = pathinfo($data['name'], PATHINFO_FILENAME);
                    $extension = pathinfo($data['name'], PATHINFO_EXTENSION);
                    $tmp = tempnjosegonzalez/cakephp-uploadam(sys_get_temp_dir(), 'ss_upload') . '.' . $extension;

                    if ($extension == 'jpg' || $extension == 'jpeg') {
                        $targetWidth = 1800;
                        $targetHeight = 684;

                        $size = new Box($targetWidth, $targetHeight);
                        $mode = ManipulatorInterface::THUMBNAIL_INSET;
                        $imagine = new Imagine();

                        $imagine->open($data['tmp_name'])
                            ->thumbnail($size, $mode)
                            ->save($tmp);
                    }

                    return [
                        $tmp => $filename . '.' . $extension,
                    ];
                },
            ]
        ]);

Removing the "transformer" will upload video and save entity

$this->addBehavior('Josegonzalez/Upload.Upload', [
            'media' => [
                'path' => 'webroot{DS}uploads{DS}saved_searches{DS}',
                'keepFilesOnDelete' => false,
                'restoreValueOnFailure' => false,
                'nameCallback' => function (RepositoryInterface $table, EntityInterface $entity, $data, $field, $settings) {
                    $extension = pathinfo($data['name'], PATHINFO_EXTENSION);
                    $filename = $this->sanitizeFileName(pathinfo($data['name'], PATHINFO_FILENAME), uniqid());

                    return $filename . '.' . $extension;
                }
            ]
        ]);
magic-77 commented 2 years ago

I could solve it be returning a proper Array

return [
    $data['tmp_name'] => $data['name'],
];