Introduce hook to alter the download response

Created on 31 March 2023, over 1 year ago
Updated 22 July 2024, 4 months ago

Problem/Motivation

Some of our media entities are videos hosted on Vimeo, which requires a custom download handler. Currently there is no hook in the module to inject custom logic for this kind of cases.

Proposed resolution

The issue was already outlined (and solved) in #3012528 through the introduction of MediaDownload plugins, but Berdir was asking for a less complex solution.

I would suggest to implement simple (well-doumented) hook in the download controller to allow other modules to return custom response objects.

An example implementation can be found in my sandbox project Media entity download Vimeo .

Remaining tasks

Merge request incoming

API changes

Introduction of a new hook in DownloadController.php:

/**
 * Lightweight hook to implement custom download responses for media downloads.
 *
 * @param \Drupal\media\MediaInterface $media
 *   The array of media source plugin definitions, keyed by plugin ID.
 * @param array $source_field_value
 *   The source field value as array, respecting the field delta if given.
 *
 * @return \Symfony\Component\HttpFoundation\Response|void
 *   Serve the file directly the response or redirect to a download URL.
 */
function hook_media_entity_download(MediaInterface $media, array $source_field_value) {
  $source = $media->getSource();

  if ($source->getPluginId() === 'oembed:video') {
    $url = $source_field_value['value'];
    $host = parse_url($url, PHP_URL_HOST);

    // If the URL links to our custom CDN, redirect to the video download URL.
    if ($host === 'custom-cdn.com') {
      return new TrustedRedirectResponse('https://custom-cdn.com?download=' . urlencode($url));
    }
  }
}

Data model changes

none

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇦🇹Austria Nebel54 Vienna

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

Comments & Activities

  • Issue created by @Nebel54
  • @nebel54 opened merge request.
  • 🇨🇦Canada deviantintegral

    I was pointed here from https://www.drupal.org/project/media_entity_download/issues/2951316 Add support for Stage File Proxy Needs review .

    I'm curious, given your hook effectively ends up replacing half of the code in the \Drupal\media_entity_download\Controller\DownloadController::download() method, is there a good reason to do this as a hook instead of decorating or completely replacing the download controller?

    As well, your hook implementation returns the first hook's response, even though multiple hooks may be invoked. I think most developers expect that hooks generally merge data together, instead of a "lowest weight wins". Actually, even if it did only return the response from one hook, I think in this case a "highest weight wins" would be the expected behaviour.

  • 🇧🇪Belgium jorisclaes

    patch for Drupal 10.3.1

Production build 0.71.5 2024