Skip to content

Commit d36b652

Browse files
authored
Merge pull request #4214 from rldhont/etag-project-illustration
Add ETAG header to project illustration
2 parents d48a18d + 0b071b1 commit d36b652

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

lizmap/modules/view/controllers/media.classic.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,53 @@
1515

1616
class mediaCtrl extends jController
1717
{
18+
/**
19+
* Check if cache can be used because it is impossible
20+
* to use cache on other request type that GET or HEAD.
21+
*
22+
* @return bool
23+
*/
24+
protected function canBeCached()
25+
{
26+
return in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD'));
27+
}
28+
29+
/**
30+
* @param jResponse $resp
31+
* @param string $etag
32+
*
33+
* @return jResponse the response updated
34+
*/
35+
protected function setEtagCacheHeaders($resp, $etag)
36+
{
37+
if ($this->canBeCached()) {
38+
$resp->addHttpHeader('ETag', $etag);
39+
$resp->addHttpHeader('Cache-Control', 'no-cache');
40+
}
41+
42+
return $resp;
43+
}
44+
45+
protected function defaultIllustrationPath()
46+
{
47+
// default illustration
48+
$themePath = jApp::wwwPath().'themes/'.jApp::config()->theme.'/';
49+
50+
return $themePath.'css/img/250x250_mappemonde.jpg';
51+
}
52+
53+
protected function defaultIllustrationEtag()
54+
{
55+
if ($this->canBeCached()) {
56+
$etag = 'default-illustration-';
57+
$etag .= filemtime($this->defaultIllustrationPath());
58+
59+
return sha1($etag);
60+
}
61+
62+
return '';
63+
}
64+
1865
/**
1966
* Returns error.
2067
*
@@ -334,7 +381,24 @@ public function illustration()
334381
}
335382
$rep->mimeType = $mime;
336383

384+
// Etag header and cache control
385+
$etag = '';
386+
if ($this->canBeCached()) {
387+
$etag = 'illustration';
388+
$etag .= '-'.$lrep->getKey().'~'.$lproj->getKey();
389+
$etag .= '-'.$type;
390+
$etag .= '-'.filemtime($rep->fileName);
391+
$etag = sha1($etag);
392+
}
393+
394+
if ($etag !== '' && $rep->isValidCache(null, $etag)) {
395+
return $rep;
396+
}
397+
337398
$rep->setExpires('+1 days');
399+
if ($etag !== '') {
400+
$this->setEtagCacheHeaders($rep, $etag);
401+
}
338402

339403
return $rep;
340404
}
@@ -351,12 +415,18 @@ public function defaultIllustration()
351415
$rep->doDownload = false;
352416

353417
// default illustration
354-
$themePath = jApp::wwwPath().'themes/'.jApp::config()->theme.'/';
355-
$rep->fileName = $themePath.'css/img/250x250_mappemonde.jpg';
418+
$rep->fileName = $this->defaultIllustrationPath();
356419
$rep->outputFileName = 'lizmap_mappemonde.jpg';
357420
$rep->mimeType = 'image/jpeg';
421+
$etag = $this->defaultIllustrationEtag();
422+
if ($etag !== '' && $rep->isValidCache(null, $etag)) {
423+
return $rep;
424+
}
358425

359426
$rep->setExpires('+7 days');
427+
if ($etag !== '') {
428+
$this->setEtagCacheHeaders($rep, $etag);
429+
}
360430

361431
return $rep;
362432
}

0 commit comments

Comments
 (0)