Problem/Motivation
The problem is that Drupal's ExtensionMimeTypeGuesser method cannot detect the MIME types of temporary files, as they do not have a meaningful extension.
The format of the file path
/tmp/Fhknp
The guessMimeType() ExtensionMimeTypeGuesser method always returns application/octet-stream, as it cannot find the extension.
Steps to reproduce
Create a Form API form that uses the file render element, and run the following code snippet inside the form validator:
Test to upload pdf file.
$validator = Validation::createValidator();
$upload_file = $form_state->getValue('files')[0];
$violations = $validator->validate($upload_file, [
new File([
‘mimeTypes’ => [
‘application/pdf’,
],
]),
]);
foreach ($violations as $violation) {
$violation_file = $violation->getRoot();
$form_state->setErrorByName(‘files’, $violation->getMessage());
}
Proposed resolution
Introduce a new FileMimeTypeGuesser service that can detect MIME types based on file contents rather than extensions. This resolves the issue for temporary files. A patch has been provided.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | temporary_file_mime_type.patch | 1.64 KB | dim-dev |
Issue fork drupal-3566708
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dim-dev commentedComment #3
dim-dev commentedComment #4
godotislateThank you for reporting this!
Core development starts with the
mainbranch (meaning Drupal 12), and then gets backported from there. Since this patch introduces a new service, this would not be eligible for 10.6.x.To move forward, the patch will need to be turned into a merge request: https://www.drupal.org/docs/develop/git/using-gitlab-to-contribute-to-dr...
In addition, there will need to be an automated test written that demonstrates the issue.
Once those two steps are complete, the issue can be moved to Needs review for community members to have a look.