Issue #22 - Added headers to cache files and changed filenames to a sha1 hash of its contents.

This commit is contained in:
Daniel Thee Roperto
2016-11-09 11:31:54 +11:00
parent 32e44ea86c
commit f086fc1af7
3 changed files with 20 additions and 22 deletions

View File

@@ -28,7 +28,6 @@ namespace auth_outage\local\controllers;
use auth_outage\local\outage;
use coding_exception;
use DOMDocument;
use DOMElement;
use invalid_parameter_exception;
use moodle_url;
@@ -43,9 +42,6 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class maintenance_static_page {
/** @var int */
private static $nextfile = 1;
/**
* Gets the cli maintenance template file location.
* @return string
@@ -199,18 +195,17 @@ class maintenance_static_page {
return $url; // External URL, leave it.
}
$file = self::$nextfile++;
if ($type != '') {
$file .= '.'.$type;
}
$path = self::get_resources_folder().'/'.$file;
// PHPUnit will use www.example.com as wwwroot and we don't to copy the file.
if (!PHPUNIT_TEST) {
copy($url, $path);
if (PHPUNIT_TEST) {
$contents = '';
} else {
$contents = file_get_contents($url);
}
$filename = sha1($contents).'.'.$type;
$filepath = self::get_resources_folder().'/'.$filename;
file_put_contents($filepath, $contents);
$url = (string)new moodle_url('/auth/outage/maintenance.php?file='.$file);
$url = (string)new moodle_url('/auth/outage/maintenance.php?file='.$filename);
return $url;
}
}

View File

@@ -44,12 +44,15 @@ if (isset($_GET['file'])) {
}
// Detect type, we only support css or PNG images.
$type = substr($file, -3);
if ($type == 'css') {
header('Content-type: text/css');
} else {
header('Content-type: image/png');
}
header('Content-Type: '.(substr($file, -3) == 'css' ? 'text/css' : 'image/png'));
// Use cache.
$lifetime = 60 * 60 * 24; // 1 day.
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
header('Cache-Control: public, max-age='.$lifetime);
header('Accept-Ranges: none');
readfile($file);
return;
}

View File

@@ -88,7 +88,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
maintenance_static_page::create_from_html($html);
$generated = file_get_contents(maintenance_static_page::get_template_file());
self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php/', $generated);
self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php?file=', $generated);
self::assertNotContains($link1, $generated);
self::assertNotContains($link2, $generated);
self::assertContains($link3, $generated);
@@ -104,7 +104,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
maintenance_static_page::create_from_html($html);
$generated = file_get_contents(maintenance_static_page::get_template_file());
self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php/', $generated);
self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php?file=', $generated);
self::assertNotContains($link1, $generated);
self::assertNotContains($link2, $generated);
self::assertContains($link3, $generated);
@@ -119,6 +119,6 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
$generated = file_get_contents(maintenance_static_page::get_template_file());
self::assertNotContains($link, $generated);
self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php/', $generated);
self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php?file=', $generated);
}
}