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.

Issue fork drupal-3566708

Command icon 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

dim-dev created an issue. See original summary.

dim-dev’s picture

StatusFileSize
new1.64 KB
dim-dev’s picture

Assigned: dim-dev » Unassigned
godotislate’s picture

Version: 10.6.x-dev » main
Status: Patch (to be ported) » Needs work
Issue tags: +Needs tests

Thank you for reporting this!

Core development starts with the main branch (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.

sivaji_ganesh_jojodae made their first commit to this issue’s fork.