Search code examples
imagezend-frameworkcachinghttp-headerszend-cache

When I try to cache private images (an action with modifed headers), the headers are omitted


I have the following action:

public function viewImageAction()
{
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $filename = sanitize_filename($this->_request->getParam('file'), 'jpg');
    $data = file_get_contents(APPLICATION_PATH . '/../private-files/fans-pictures/' . $filename);
    $this->getResponse()
         ->setHeader('Content-type', 'image/jpeg')
         ->setBody($data);
}

And in my index.php before the application start I have:

/** Zend Cache to avoid unecessary application load **/
require_once 'Zend/Cache.php';

$frontendOptions = array(
'lifetime' => 3600,
'default_options' => array(
    'cache' => $cache_flag,
    'cache_with_cookie_variables' => true,
    'make_id_with_cookie_variables' => false),
'regexps' => array(
    '^(/.+)?/admin/?' => array('cache' => false),
    '^(/.+)?/pictures/view-image/?' => array('cache' => true),
    '^(/.+)?/authentication/?' => array('cache' => false),
    '^(/.+)?/fan-profile/?' => array('cache' => false),
    '^(/.+)?/fan-registration/?' => array('cache' => false))
);

$backendOptions = array(
'cache_dir' => APPLICATION_PATH . '/cache/pages/');

$cache = Zend_Cache::factory(
  'Page', 'File', $frontendOptions, $backendOptions
);

$cache->start();

The cache works fine, except that if I try to access the url, like public/admin/pictures/view-image/file/63.jpg the headers come with text/html not image/jpeg.

Am I doing something wrong?

EDITED

I've tried:

'memorize_headers' => array('Content-type')

But nothing...

Also, I've notice that this type of caching (before the application start) can't be done on admin areas because the application need to run and check the session. So I need to put the chache as soon as possible to avoid the load of all components involved.

Any tips?


Solution

  • SOLUTION

    The problem is with the location of the memorize_headers parameter.

    I was trying this:

    $frontendOptions = array(
    'lifetime' => 3600,
    'default_options' => array(
        'cache' => $cache_flag,
        'cache_with_cookie_variables' => true,
        'memorize_headers' => array('Content-Type', 'Content-Encoding'),
        'make_id_with_cookie_variables' => false),
    'regexps' => array(
        '^(/.+)?/admin/?' => array('cache' => false),
        '^(/.+)?/admin/pictures/view-image/?' => array('cache' => true),
        '^(/.+)?/authentication/?' => array('cache' => false),
        '^(/.+)?/fan-profile/?' => array('cache' => false),
        '^(/.+)?/fan-registration/?' => array('cache' => false))
    );
    

    The right location of this is out of the default_options key:

    $frontendOptions = array(
    'lifetime' => 3600,
    'memorize_headers' => array('Content-Type', 'Content-Encoding'),
    'default_options' => array(
        'cache' => $cache_flag,
        'cache_with_cookie_variables' => true,
        //'cache_with_session_variables' => true,
        'make_id_with_cookie_variables' => false),
    'regexps' => array(
        '^(/.+)?/admin/?' => array('cache' => false),
        '^(/.+)?/admin/pictures/view-image/?' => array('cache' => true),
        '^(/.+)?/authentication/?' => array('cache' => false),
        '^(/.+)?/fan-profile/?' => array('cache' => false),
        '^(/.+)?/fan-registration/?' => array('cache' => false))
    );
    

    Now it works.