Skip to content

Commit

Permalink
MDL-22955 output: Fixed bug by ensuring we generate an SVG candidate …
Browse files Browse the repository at this point in the history
…even for regular format requests
  • Loading branch information
Sam Hemelryk committed Oct 7, 2012
1 parent f7d6a55 commit 27c1c30
Showing 1 changed file with 59 additions and 19 deletions.
78 changes: 59 additions & 19 deletions theme/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,38 @@
require("$CFG->dirroot/lib/setup.php");

$theme = theme_config::load($themename);
$imagefile = $theme->resolve_image_location($image, $component, $usesvg);

$rev = theme_get_revision();
$etag = sha1("$themename/$component/$rev/$image");

// We're not using SVG and there is no cached version of this file (in any format).
// As we're going to be caching a format other than svg, and because svg use is conditional we need to ensure that at the same
// time we cache a version of the SVG if it exists. If we don't do this other users who ask for SVG would not ever get it as
// there is a cached image already of another format.
// Remember this only gets run once before any candidate exists, and only if we want a cached revision.
if (!$usesvg && $rev > -1) {
$imagefile = $theme->resolve_image_location($image, $component, true);
if (!empty($imagefile) && is_readable($imagefile)) {
$cacheimage = cache_image($image, $imagefile, $candidatelocation);
$pathinfo = pathinfo($imagefile);
// There is no SVG equivilant, we've just successfully cached an image of another format.
if ($pathinfo['extension'] !== 'svg') {
// Serve the file as we would in a normal request.
if (connection_aborted()) {
die;
}
// make sure nothing failed
clearstatcache();
if (file_exists($cacheimage)) {
send_cached_image($cacheimage, $etag);
}
send_uncached_image($imagefile);
exit;
}
}
}

// Either SVG was requested or we've cached a SVG version and are ready to serve a regular format.
$imagefile = $theme->resolve_image_location($image, $component, $usesvg);
if (empty($imagefile) or !is_readable($imagefile)) {
if ($rev > -1) {
if (!file_exists($candidatelocation)) {
Expand All @@ -150,23 +177,7 @@
}

if ($rev > -1) {
$pathinfo = pathinfo($imagefile);
$cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];

clearstatcache();
if (!file_exists(dirname($cacheimage))) {
@mkdir(dirname($cacheimage), $CFG->directorypermissions, true);
}

// Prevent serving of incomplete file from concurrent request,
// the rename() should be more atomic than copy().
ignore_user_abort(true);
if (@copy($imagefile, $cacheimage.'.tmp')) {
rename($cacheimage.'.tmp', $cacheimage);
@chmod($cacheimage, $CFG->filepermissions);
@unlink($cacheimage.'.tmp'); // just in case anything fails
}
ignore_user_abort(false);
$cacheimage = cache_image($image, $imagefile, $candidatelocation);
if (connection_aborted()) {
die;
}
Expand Down Expand Up @@ -253,4 +264,33 @@ function get_contenttype_from_ext($ext) {
return 'image/vnd.microsoft.icon';
}
return 'document/unknown';
}

/**
* Caches a given image file.
*
* @param string $image The name of the image that was requested.
* @param string $imagefile The location of the image file we want to cache.
* @param string $candidatelocation The location to cache it in.
* @return string The path to the cached image.
*/
function cache_image($image, $imagefile, $candidatelocation) {
global $CFG;
$pathinfo = pathinfo($imagefile);
$cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];

clearstatcache();
if (!file_exists(dirname($cacheimage))) {
@mkdir(dirname($cacheimage), $CFG->directorypermissions, true);
}

// Prevent serving of incomplete file from concurrent request,
// the rename() should be more atomic than copy().
ignore_user_abort(true);
if (@copy($imagefile, $cacheimage.'.tmp')) {
rename($cacheimage.'.tmp', $cacheimage);
@chmod($cacheimage, $CFG->filepermissions);
@unlink($cacheimage.'.tmp'); // just in case anything fails
}
return $cacheimage;
}

0 comments on commit 27c1c30

Please sign in to comment.