Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-22955 output: Fixed bug by ensuring we generate an SVG candidate …

…even for regular format requests
  • Loading branch information...
commit 27c1c305b1641d2410c32ee20227c647b927c874 1 parent f7d6a55
@samhemelryk samhemelryk authored
Showing with 59 additions and 19 deletions.
  1. +59 −19 theme/image.php
View
78 theme/image.php
@@ -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)) {
@@ -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;
}
@@ -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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.