From 5004471770489e46e503ce4cd60f1f58acbcdd26 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 29 Sep 2023 00:10:28 -0400 Subject: [PATCH] Move size to pixels conversion in its own method in Util\Proxy --- src/Content/Post/Entity/PostMedia.php | 45 +++++---------------------- src/Model/Post/Media.php | 44 ++++---------------------- src/Util/Proxy.php | 17 ++++++++++ 3 files changed, 30 insertions(+), 76 deletions(-) diff --git a/src/Content/Post/Entity/PostMedia.php b/src/Content/Post/Entity/PostMedia.php index ca064d4c7e..deb4ec1b36 100644 --- a/src/Content/Post/Entity/PostMedia.php +++ b/src/Content/Post/Entity/PostMedia.php @@ -168,25 +168,9 @@ class PostMedia extends BaseEntity */ public function getPhotoPath(string $size = ''): string { - $url = '/photo/media/'; - switch ($size) { - case Proxy::SIZE_MICRO: - $url .= Proxy::PIXEL_MICRO . '/'; - break; - case Proxy::SIZE_THUMB: - $url .= Proxy::PIXEL_THUMB . '/'; - break; - case Proxy::SIZE_SMALL: - $url .= Proxy::PIXEL_SMALL . '/'; - break; - case Proxy::SIZE_MEDIUM: - $url .= Proxy::PIXEL_MEDIUM . '/'; - break; - case Proxy::SIZE_LARGE: - $url .= Proxy::PIXEL_LARGE . '/'; - break; - } - return $url . $this->id; + return '/photo/media/' . + (Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') . + $this->id; } /** @@ -197,24 +181,9 @@ class PostMedia extends BaseEntity */ public function getPreviewPath(string $size = ''): string { - $url = '/photo/preview/'; - switch ($size) { - case Proxy::SIZE_MICRO: - $url .= Proxy::PIXEL_MICRO . '/'; - break; - case Proxy::SIZE_THUMB: - $url .= Proxy::PIXEL_THUMB . '/'; - break; - case Proxy::SIZE_SMALL: - $url .= Proxy::PIXEL_SMALL . '/'; - break; - case Proxy::SIZE_MEDIUM: - $url .= Proxy::PIXEL_MEDIUM . '/'; - break; - case Proxy::SIZE_LARGE: - $url .= Proxy::PIXEL_LARGE . '/'; - break; - } - return $url . $this->id; + return '/photo/preview/' . + (Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') . + $this->id; + } } diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index 2d7df08096..64f44c198d 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -1012,25 +1012,9 @@ class Media */ public static function getPreviewUrlForId(int $id, string $size = ''): string { - $url = DI::baseUrl() . '/photo/preview/'; - switch ($size) { - case Proxy::SIZE_MICRO: - $url .= Proxy::PIXEL_MICRO . '/'; - break; - case Proxy::SIZE_THUMB: - $url .= Proxy::PIXEL_THUMB . '/'; - break; - case Proxy::SIZE_SMALL: - $url .= Proxy::PIXEL_SMALL . '/'; - break; - case Proxy::SIZE_MEDIUM: - $url .= Proxy::PIXEL_MEDIUM . '/'; - break; - case Proxy::SIZE_LARGE: - $url .= Proxy::PIXEL_LARGE . '/'; - break; - } - return $url . $id; + return '/photo/preview/' . + (Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') . + $id; } /** @@ -1042,24 +1026,8 @@ class Media */ public static function getUrlForId(int $id, string $size = ''): string { - $url = DI::baseUrl() . '/photo/media/'; - switch ($size) { - case Proxy::SIZE_MICRO: - $url .= Proxy::PIXEL_MICRO . '/'; - break; - case Proxy::SIZE_THUMB: - $url .= Proxy::PIXEL_THUMB . '/'; - break; - case Proxy::SIZE_SMALL: - $url .= Proxy::PIXEL_SMALL . '/'; - break; - case Proxy::SIZE_MEDIUM: - $url .= Proxy::PIXEL_MEDIUM . '/'; - break; - case Proxy::SIZE_LARGE: - $url .= Proxy::PIXEL_LARGE . '/'; - break; - } - return $url . $id; + return '/photo/media/' . + (Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') . + $id; } } diff --git a/src/Util/Proxy.php b/src/Util/Proxy.php index aa0f66bbff..423e676ae6 100644 --- a/src/Util/Proxy.php +++ b/src/Util/Proxy.php @@ -211,4 +211,21 @@ class Proxy return $matches[1] . self::proxifyUrl(htmlspecialchars_decode($matches[2])) . $matches[3]; } + public static function getPixelsFromSize(string $size): int + { + switch ($size) { + case Proxy::SIZE_MICRO: + return Proxy::PIXEL_MICRO; + case Proxy::SIZE_THUMB: + return Proxy::PIXEL_THUMB; + case Proxy::SIZE_SMALL: + return Proxy::PIXEL_SMALL; + case Proxy::SIZE_MEDIUM: + return Proxy::PIXEL_MEDIUM; + case Proxy::SIZE_LARGE: + return Proxy::PIXEL_LARGE; + default: + return 0; + } + } } -- 2.39.5