Skip to content

Detect extension if no one is provided #2285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jordisala1991
Copy link
Member

@jordisala1991 jordisala1991 commented Feb 24, 2022

Subject

I am targeting this branch, because this is BC.

Closes #2208 .

Changelog

### Fixed
- Fixed auto detect extension if the file does not provide it, for example when dealing with temporal filenames.

@jordisala1991
Copy link
Member Author

I faced the same problemas @sarim.

Does this PR fixes your problem also?

@jordisala1991
Copy link
Member Author

This is my code to upload media:

        $tmp = $this->filesystem->tempnam($this->tmp, 'tmp');

        $this->filesystem->dumpFile($tmp, base64_decode($binaryContent));

        $media = new Media();
        $media->setBinaryContent($tmp);
        $media->setContext('default');
        $media->setProviderName('sonata.media.provider.image');
        $media_sizes = getimagesize($tmp);
        $media->setWidth($media_sizes[0]);
        $media->setHeight($media_sizes[1]);

This is my hack around this issue:

        $tmp = $this->filesystem->tempnam($this->tmp, 'tmp');

        $this->filesystem->dumpFile($tmp, base64_decode($binaryContent));

        $imageSize = getimagesize($tmp);
        $extension = MimeTypes::getDefault()->getExtensions($imageSize['mime'])[0] ?? null;

        if (null !== $extension) {
            $this->filesystem->rename($tmp, $tmp . '.' . $extension);

            $tmp .= '.' . $extension;
        }

        $media = new Media();
        $media->setBinaryContent($tmp);
        $media->setContext('default');
        $media->setProviderName('sonata.media.provider.image');
        $media->setWidth($imageSize[0]);
        $media->setHeight($imageSize[1]);

It was working on previous SonataMediaBundle versions, and it works with this fix.

@sarim
Copy link

sarim commented Feb 24, 2022

Yeah my code was also working on previous versions, then I upgraded the project and had to apply to following workaround make it work in current sonata media. I won't go into details now, I think I tried my best to explain it in previous two threads, kinda felt like I was not being heard.

Here in this workaround saving the file under its original name makes it have an extension.

             $upload = $request->files->get('files')[0];

+            $file = $upload->move(\sys_get_temp_dir(), \uniqid().$upload->getClientOriginalName());
+
             $media = new Media();
-            $media->setBinaryContent($upload->getPathname());
-            $media->setName($upload->getClientOriginalName());
-            $mediaManager->save($media, $context, 'sonata.media.provider.image');
+            $media->setBinaryContent($file->getPathname());
+            $media->setName($file->getFilename());
+            $media->setContext($context);
+            $media->setProviderName('sonata.media.provider.image');
+            $mediaManager->save($media);

@jordisala1991 jordisala1991 changed the base branch from 4.x to 3.x February 24, 2022 17:55
@jordisala1991 jordisala1991 reopened this Feb 24, 2022
@jordisala1991 jordisala1991 requested review from VincentLanglet and a team February 24, 2022 17:58
@jordisala1991
Copy link
Member Author

I will need to do a manual release for this one (and a merge to 4.x also) @VincentLanglet, but I would like it to land on 3.x first.

VincentLanglet
VincentLanglet previously approved these changes Feb 24, 2022
Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a test possible ?

@jordisala1991 jordisala1991 requested review from VincentLanglet and a team February 24, 2022 18:23
@jordisala1991
Copy link
Member Author

Is a test possible ?

Yes! done

@jordisala1991 jordisala1991 merged commit e875289 into sonata-project:3.x Feb 24, 2022
@jordisala1991 jordisala1991 deleted the hotfix/extension-detection branch February 24, 2022 18:25
@VincentLanglet
Copy link
Member

@jordisala1991 The 3.x branch is not available anymore on https://master-7rqtwti-ptm4dx6rjpjko.eu-5.platformsh.site/next-releases so the changelog need to be written manually.

Will try to find time tomorrow

@jordisala1991
Copy link
Member Author

No worries, I am on it right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New media creation fails if filename doesn't contain image extension
3 participants