Problem/Motivation

When guessing MIME type for files stored in remote file systems, for example dummy-remote://test.png,MIME type returned is always application/octet-stream.

That's because MimeTypeGuesser::guess always try to replace the $path passed in with its realpath.

But remote stream wrapper return NULL for ->realpath(), hence the default MIME type applies.

Proposed resolution

Replace the $path only if a sensible realpath is returned by the wrapper.

Remaining tasks

review

User interface changes

none

API changes

none

Data model changes

none

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Status: Active » Needs review
FileSize
1.43 KB
2.25 KB

Patch

The last submitted patch, 2: 2760797-2-test-only.diff, failed testing.

twistor’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php
@@ -53,8 +53,11 @@ public function __construct(StreamWrapperManagerInterface $stream_wrapper_manage
+      // Get the real path from the stream wrapper, if available. Files stored
+      // in remote file systems will not have one.
+      if ($real_path = $wrapper->realpath()) {
+        $path = $real_path;
+      }

This should check !== FASLE, since realpath() could return '0'.

According to StreamWrapperInterface::realpath(), it should return FALSE, not NULL if unsupported.

mondrake’s picture

Status: Needs work » Needs review
FileSize
697 bytes
2.28 KB

Thank you @twistor. Done #4.

twistor’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 4e547f5246e09ad41ce5caba8ead3bf4a7baec59 to 8.2.x and 3de9785 to 8.1.x. Thanks!

Nice find and fix. Committing to both branches as the bug fix is patch eligible.

  • alexpott committed 4e547f5 on 8.2.x
    Issue #2760797 by mondrake, twistor: MIME type guessing fails for remote...

  • alexpott committed 3de9785 on 8.1.x
    Issue #2760797 by mondrake, twistor: MIME type guessing fails for remote...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.