- Issue created by @msypes
- ๐ฎ๐ณIndia Sahana _N
sahana _n โ made their first commit to this issueโs fork.
- Status changed to Needs review
12 months ago 1:05pm 3 September 2024 - ๐ฎ๐ณIndia Sahana _N
Hi,
I can reproduce the issue in Drupal 10.3.x, when I attempt to view the page with a file reference to a file with NULL as the mimetype I got this error TypeError: Drupal\file\IconMimeTypes::getIconClass(): Argument #1 ($mimeType) must be of type string, null given, called in /var/www/html/core/modules/file/file.module on line 774 in Drupal\file\IconMimeTypes::getIconClass() (line 21 of core/modules/file/src/IconMimeTypes.php).I tested the same scenario by applying the patch provided in comment #3 (update this comment number). The patch is applied cleanly and the error was cleared. So, I created MR with the same code changes.
I am attaching before & after screenshots for reference.
This issue is also reproducible in 11.x and was able to fix the issue with the same code changes.
I'll be happy to create MR for 11.x also if required. Please suggest how we can proceed further.
- Status changed to Needs work
12 months ago 2:04pm 3 September 2024 - ๐บ๐ธUnited States smustgrave
Fixes should be against 11.x
Also we should probably do some debugging vs putting a null check. Could be masking a larger issue
- ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
Despite the db column allowing NULL. our API assumes there will always be a mime type. Why do you have NULL mimetypes?
- ๐บ๐ธUnited States msypes
@kim.pepper: Are you asking me why we had NULL mimetypes in the database? I don't know. Could be from a less-than-perfect migration, from when we moved from D7. They were generally pretty old records. Did the the API always assume there was a mimetype? If not, why would the db allow it to be NULL? At any rate, the fact that it does allow NULLs should mean that the system can handle them.
- ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
\Drupal\Core\File\MimeType\MimeTypeGuesser::guessMimeType()
defaults to'application/octet-stream'
if it can't determine a mime type. Would it make sense to default to this rather than empty string? - ๐จ๐ฆCanada colan Toronto ๐จ๐ฆ
Well, it's better than getting an error, right?
- ๐ฉ๐ชGermany DiDebru
Thanks for the patch.
In our case it happens if we send a webform graphql mutation with a pdf attached. - Merge request !11965[#3469280] Use a default mime type if none found โ (Open) created by kim.pepper
- Status changed to Postponed: needs info
4 months ago 11:02pm 28 April 2025 - ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
kim.pepper โ changed the visibility of the branch 3469280-File-mime-type-null to hidden.
- ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
kim.pepper โ changed the visibility of the branch 3469280-drupalfileiconmimetypes-doesnt-handle to hidden.
- ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
Added a default mime type constant of
\Drupal\Core\File\MimeType\MimeTypeGuesser::DEFAULT_MIME_TYPE
with value'application/octet-stream'
and use that instead of empty string.Hiding some older patches and MRs.
- ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
Updated IS to reflect latest proposed resolution.
- ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
Updated title.
- ๐บ๐ธUnited States smustgrave
Seems pretty straight forward, not sure test coverage is needed here.
- ๐ฌ๐งUnited Kingdom alexpott ๐ช๐บ๐
We have other code that assumes getMimeType() returns a string...
Consider
$mimetype = $file->getMimeType(); $mimetype = explode('/', $mimetype);
in
\Drupal\media\Plugin\media\Source\File::getThumbnail
This will also cause a deprection.
I think we should change \Drupal\file\Entity\File::getMimeType() to return the default value - or it could even do something like File::preCreate()... and guess the mimetype if we have a URI
/** * {@inheritdoc} */ public function getMimeType() { $mimetype = $this->get('filemime')->value; if ($mimetype === NULL) { $uri = $this->getFileUri(); $mimetype = $uri !== NULL ? \Drupal::service('file.mime_type.guesser')->guess($uri) : MimeTypeGuesser::DEFAULT_MIME_TYPE; } return $mimetype; }
We should also update the interface to tell people not to return a NULL
I think this would be a good change because then all code can rely on getMimeType() returning something sensible rather than having to deal with the NULL situation (which must be very rare given the preCreate().
- First commit to issue fork.
- ๐ฆ๐บAustralia kim.pepper ๐โโ๏ธ๐ฆ๐บSydney, Australia
Re: #23 I updated \Drupal\file\Entity\File::getMimeType() to return the default mime type if NULL, and also the docs on \Drupal\file\FileInterface::getMimeType() to match this behaviour.
- Status changed to RTBC
about 2 months ago 3:22pm 29 June 2025 - ๐ฆ๐บAustralia larowlan ๐ฆ๐บ๐.au GMT+10
One minor change, fine to self RTBC
- ๐ฆ๐บAustralia larowlan ๐ฆ๐บ๐.au GMT+10
We can clean up a bit more here
\Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase::getSourceFiles
has code that checks for NULL, which is no longer possible.In searching for callers of this method, the number of places we're treating the return as a string is an excellent case to make this change.