Append a cache buster to upload images
authorAlexander Ebert <ebert@woltlab.com>
Thu, 15 Sep 2022 12:24:48 +0000 (14:24 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 15 Sep 2022 12:24:48 +0000 (14:24 +0200)
See https://www.woltlab.com/community/thread/296221-cache-in-template-uploadfieldcomponent/

wcfsetup/install/files/lib/system/file/upload/UploadFile.class.php

index 3dbb9a03140aa0092b8e5a5b3b27b794c73e425a..addd9ea944109094f72ea3a29eb49f7a40ff4dd0 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace wcf\system\file\upload;
 
+use Laminas\Diactoros\Uri;
 use wcf\system\WCF;
 use wcf\util\FileUtil;
 use wcf\util\StringUtil;
@@ -141,10 +142,21 @@ class UploadFile
         if ($this->processed) {
             if ($this->imageLink === null) {
                 // try to guess path
-                return \str_replace(WCF_DIR, WCF::getPath(), $this->location);
+                $link = \str_replace(WCF_DIR, WCF::getPath(), $this->location);
             } else {
-                return $this->imageLink;
+                $link = $this->imageLink;
             }
+
+            $url = new Uri($link);
+            $query = $url->getQuery();
+            $cacheBuster = 't=' . \TIME_NOW;
+            if ($query) {
+                $url = $url->withQuery("{$query}&{$cacheBuster}");
+            } else {
+                $url = $url->withQuery($cacheBuster);
+            }
+
+            return (string)$url;
         } else {
             if ($this->imageData !== false) {
                 return 'data:' . $this->imageData['mime'] . ';base64,' . \base64_encode(\file_get_contents($this->location));