Chapters
40 Chapters
|
4:39:58
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
22.
Mime Type Validation
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial is built on Symfony 4 but works great in Symfony 5!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.87.10
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.9.0
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.22
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.0.3
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.2.3
"symfony/console": "^4.0", // v4.2.3
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.2.3
"symfony/framework-bundle": "^4.0", // v4.2.3
"symfony/property-access": "4.2.*", // v4.2.3
"symfony/property-info": "4.2.*", // v4.2.3
"symfony/security-bundle": "^4.0", // v4.2.3
"symfony/serializer": "4.2.*", // v4.2.3
"symfony/twig-bundle": "^4.0", // v4.2.3
"symfony/validator": "^4.0", // v4.2.3
"symfony/web-server-bundle": "^4.0", // v4.2.3
"symfony/yaml": "^4.0", // v4.2.3
"twig/extensions": "^1.5" // v1.5.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.1.0
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.2.3
"symfony/dotenv": "^4.0", // v4.2.3
"symfony/maker-bundle": "^1.0", // v1.11.3
"symfony/monolog-bundle": "^3.0", // v3.3.1
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.2.3
"symfony/stopwatch": "4.2.*", // v4.2.3
"symfony/var-dumper": "^3.3|^4.0", // v4.2.3
"symfony/web-profiler-bundle": "4.2.*" // v4.2.3
}
}
10 Comments
this article has been very useful for me! thank you so much
How can I force a custom mime type for my file uploader? I am trying to upload .usdz files and the uploader converts them to .zip
If I use this:
'constraints' => [
new File([
'mimeTypes' => [
'model/vnd.usdz+zip',
'model/vnd.pixar.usd',
'model/usd',
'model/usdz'
],
'mimeTypesMessage' => 'Please upload a valid .usdz 3D file.',
])
]
in my FormType it will not let me upload the file, and if I do not use a constraint it will just convert it to .zip.
Thank you for the help.
Hey JavierMendezK!
Excellent question! The validator logic for the File validator is in this section - https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Validator/Constraints/FileValidator.php#L174-L206
I can't remember for certain, but my guess is that you will end up on this line: https://github.com/symfony/symfony/blob/31d81e2ca6c2862d78a90941c1ad6c3d4a14288d/src/Symfony/Component/Validator/Constraints/FileValidator.php#L178
Internally, THAT class uses, out-of-the-box, 2 "guessers" to guess the extension: https://github.com/symfony/symfony/blob/31d81e2ca6c2862d78a90941c1ad6c3d4a14288d/src/Symfony/Component/Mime/MimeTypes.php#L57-L58
My guess is that one of those two is incorrectly guessing the mime type - it just probably isn't aware of the .usdz extension.
Fortunately, you can create your own mime type guesser - create a class, make it implement
MimeTypeGuesserInterface, and fill in the logic. Symfony should automatically see this and start using it - you can add custom logic to recognize your .usdz files. But let me know if your new class isn't immediately used... there's one "path way" in the code that should be there in the core of Symfony to hook this all up, I'm just not seeing it. If your class isn't used, try autowiring a MimeTypesInterface argument into whatever controller or service you're working inside of. You don't even need to USE that class... but in case the path way is missing in Symfony, just autowiring this, I think, will activate it.Cheers!
So why do I need to have my own guesser if MimeTypes.php already includes model/vnd.pixar.usd as a valid MimeType ?
I followed https://symfony.com/index.p... as your recommendation but I guess I am missing the "logic" in:
public function guessMimeType(string $path): ?string
{
// inspect the contents of the file stored in $path to guess its
// type and return a valid MIME type ... or null if unknown
return '...';
}
to implement support for my own extension.
Hey JavierMendezK !
Hmm, where are you seeing this? I'm not seeing it on this list for Symfony 5.4 (next release) or 5.3 - https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Mime/MimeTypes.php#L153 - do you have
model/vnd.pixar.usdon your list?This is a fair point :). I was hoping that you knew something about these files - like you could open up just the first line of the file using fopen and look for something that distinguishes these files :). But, if you really do have
model/vnd.pixar.usdin your mime type list, then we should dig deeper and see why the type is being seen as a zip. I would put some debug code into the 2 core "guessers" - https://github.com/symfony/symfony/blob/f32af4683a04f440cda7ad8068d6cd05600d5e0e/src/Symfony/Component/Mime/FileBinaryMimeTypeGuesser.php#L22 and https://github.com/symfony/symfony/blob/f32af4683a04f440cda7ad8068d6cd05600d5e0e/src/Symfony/Component/Mime/FileinfoMimeTypeGuesser.php#L22 to figure out which one of these is incorrectly determining the type. That might shed some light on the underlying problem.Cheers!
The line is in the file "vendor/symfony/mime/MimeTypes.php" line 1482
Hey JavierMendezK!
Ah, cool! What version of symfony/mime are you using? I can't find any mention of
model/vnd.pixar.usdin any version of Symfony that I'm checking... but I believe you that it's there.Regardless, I would add some debugging code to those two classes I mentioned above to see which one is returning the incorrect result. That may give us a clue to the problem and the fix.
Cheers!
It is on the link you posted on line 1537:
https://github.com/symfony/...
and I am using 5.2.14
Ooooooh! I was looking for
model/vnd.pixar.usd- for some reason THAT one had gotten stuck in my head.The point remains though - I think you'll need to put some debugging code on those 2 mime type guesser classes to see what's going wrong. Or, if you want, you could make your custom guesser just look for the
.usdzfile extension. Just make sure that, if you do this, that file is used in a "safe" way - e.g. if it actually is a file that contains PHP code, don'teval()it ;). That's a joke - but you get the idea: you'll be validating the filename, but the contents could, in theory, be anthing.Btw, part of the problem it's coming back as a .zip is that, from my reading, these USDZ files are zip files... just with a special structure inside. So... that's tricky!
Cheers!
Thanks.
"Houston: no signs of life"
Start the conversation!