MM Media & the Image module

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

MM Media & the Image module

Adam Franco
Administrator
Is anyone else using the core Image module to provide thumbnails and other resized versions of images?

We found that when images are accessed under the Image module's size-specific paths, the image module specifically drops all caching-related headers set by other modules (in particular those set by mm_media) on the original file. The result is that no images utilizing size-specific paths can be cached by our Varnish reverse-proxy cache.

I fixed this with the following hook implementation in our custom 'tweaks' module, but thought I'd bring this issue to the attention of you all in case you have a better solution or have a way to integrate this into mm_media directly.

/**
 * Implements hook_file_download().
 *
 * The image module's image_file_download() explicitly ignores all headers returned
 * by mm_media_file_download() that control caching privacy. This breaks Varnish
 * caching for all images. This implementation will re-run hook_file_download for the
 * original URL and NOT drop the cache-control headers supplied by mm_media.
 */
function middlebury_monster_menus_file_download($uri) {
  $path = file_uri_target($uri);

  // Private file access for image style derivatives.
  if (strpos($path, 'styles/') === 0) {
    $args = explode('/', $path);
    // Discard the first part of the path (styles).
    array_shift($args);
    // Get the style name from the second part.
    $style_name = array_shift($args);
    // Remove the scheme from the path.
    array_shift($args);

    // Then the remaining parts are the path to the image.
    $original_uri = file_uri_scheme($uri) . '://' . implode('/', $args);

    // Check that the file exists and is an image.
    if ($info = image_get_info($uri)) {
      // Check the permissions of the original to grant access to this image.
      $headers = mm_media_file_download($original_uri);
      // Confirm there's at least one module granting access and none denying access.
      if (!empty($headers) && !in_array(-1, $headers)) {

        // Return just the cache-control headers.
        if (isset($headers['Cache-Control'])) {
          return array('Cache-Control' => $headers['Cache-Control']);
        }

      }
    }
    return -1;
  }
}


Best,
Adam

--

Adam Franco
Senior Software Engineer - Web Applications
Library and Information Services
Middlebury College
Middlebury, VT 05753
[hidden email]
802.443.2244

---

You are currently subscribed to monster_menus as: [hidden email].

To unsubscribe click here: http://lists.middlebury.edu/u?id=685503.6b071f880fe6a965a128164e6d09ea81&n=T&l=monster_menus&o=706067

(It may be necessary to cut and paste the above URL if the line is broken)

or send a blank email to [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MM Media & the Image module

Dan Wilga-2
We actually use a (slightly modified version of) the adaptive_image module. Because it doesn't insert itself at the hook_file_download stage, but instead implements new image styles, it Just Works with mm_media.

On 9/15/14 1:52 PM, Adam Franco wrote:
Is anyone else using the core Image module to provide thumbnails and other resized versions of images?

We found that when images are accessed under the Image module's size-specific paths, the image module specifically drops all caching-related headers set by other modules (in particular those set by mm_media) on the original file. The result is that no images utilizing size-specific paths can be cached by our Varnish reverse-proxy cache.

I fixed this with the following hook implementation in our custom 'tweaks' module, but thought I'd bring this issue to the attention of you all in case you have a better solution or have a way to integrate this into mm_media directly.

/**
 * Implements hook_file_download().
 *
 * The image module's image_file_download() explicitly ignores all headers returned
 * by mm_media_file_download() that control caching privacy. This breaks Varnish
 * caching for all images. This implementation will re-run hook_file_download for the
 * original URL and NOT drop the cache-control headers supplied by mm_media.
 */
function middlebury_monster_menus_file_download($uri) {
  $path = file_uri_target($uri);

  // Private file access for image style derivatives.
  if (strpos($path, 'styles/') === 0) {
    $args = explode('/', $path);
    // Discard the first part of the path (styles).
    array_shift($args);
    // Get the style name from the second part.
    $style_name = array_shift($args);
    // Remove the scheme from the path.
    array_shift($args);

    // Then the remaining parts are the path to the image.
    $original_uri = file_uri_scheme($uri) . '://' . implode('/', $args);

    // Check that the file exists and is an image.
    if ($info = image_get_info($uri)) {
      // Check the permissions of the original to grant access to this image.
      $headers = mm_media_file_download($original_uri);
      // Confirm there's at least one module granting access and none denying access.
      if (!empty($headers) && !in_array(-1, $headers)) {

        // Return just the cache-control headers.
        if (isset($headers['Cache-Control'])) {
          return array('Cache-Control' => $headers['Cache-Control']);
        }

      }
    }
    return -1;
  }
}



---

You are currently subscribed to monster_menus as: [hidden email].

To unsubscribe click here: http://lists.middlebury.edu/u?id=685503.6b071f880fe6a965a128164e6d09ea81&n=T&l=monster_menus&o=706081

(It may be necessary to cut and paste the above URL if the line is broken)

or send a blank email to [hidden email]

Loading...