MIME detection fails when verifying a file with a remote URI

Created on 16 January 2019, almost 6 years ago
Updated 31 August 2023, about 1 year ago

The function getMime uses a variable $file_path which relies on the realPath function. This doesn't appear to work for remote URIs. In our case, we're using stage_file_proxy. This means that on our testing/dev branches it's not possible to save a node without getting the MIME error, as the file isn't correctly loaded.

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom spencer95@gmail.com Swansea/Cardiff

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom nicrodgers Monmouthshire, UK

    Patch applies successfully and works with and without stage_file_proxy

  • Status changed to Needs work over 1 year ago
  • 🇪🇸Spain manuel.adan 🌌
    1. +++ b/src/MimeDetectService.php
      @@ -87,10 +87,21 @@ class MimeDetectService implements MimeDetectServiceInterface {
      +      if (\Drupal::moduleHandler()->moduleExists('stage_file_proxy')) {
      +        $server = \Drupal::config('stage_file_proxy.settings')->get('origin');
      +        $fetch_manager = \Drupal::service('stage_file_proxy.fetch_manager');
      

      Dependency injection should be used here to make use of the module handler, config manager and Stage File Proxy fetch manager.

    2. +++ b/src/MimeDetectService.php
      @@ -87,10 +87,21 @@ class MimeDetectService implements MimeDetectServiceInterface {
      +        $file_path = implode('/', [$server, $file_dir, $relative_path]);
      

      It seems that path to file is composed using the remote server name, which ends in a non local file path (eg.: http://www.example.com/sites/default/files/image.jpg). The following mime detection method (file UNIX command) is unable to manage remote URLs, it only works with local files, so the file itself should be retrieved.

    I think that there is no point of checking remote files since they should be already checked on their environment. A better approach here would be just to skip non-local files.

  • @nicrodgers opened merge request.
  • Status changed to Needs review about 1 year ago
  • 🇬🇧United Kingdom nicrodgers Monmouthshire, UK

    I think that there is no point of checking remote files since they should be already checked on their environment. A better approach here would be just to skip non-local files.

    Good point. I've taken this approach, by skipping remote files in hook_file_validate.
    Hiding the patches, latest code is in the MR.

Production build 0.71.5 2024