From e0a8b142e57430cd2541868fe35ac44c7588cda3 Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Fri, 12 Mar 2021 11:38:03 +0100 Subject: [PATCH] Use random imageHash to avoid collisions --- .../background/job/UnfurlUrlBackgroundJob.class.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php b/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php index 5345d247ba..b1a114f665 100644 --- a/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php +++ b/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php @@ -201,14 +201,17 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob private function saveImage(array $imageData, string $image): string { - $imageHash = \sha1($image); + do { + $imageHash = StringUtil::getRandomID(); + + $path = WCF_DIR . UnfurlUrl::IMAGE_DIR . \substr($imageHash, 0, 2) . '/'; + } while (!empty(glob($path . $imageHash . '.*'))); - $path = WCF_DIR . UnfurlUrl::IMAGE_DIR . \substr($imageHash, 0, 2); FileUtil::makePath($path); $extension = $this->getImageExtension($imageData); - $fileLocation = $path . '/' . $imageHash . '.' . $extension; + $fileLocation = $path . $imageHash . '.' . $extension; \file_put_contents($fileLocation, $image); -- 2.20.1