Skip to content

Commit 3b3a4db

Browse files
committed
fix: pathPrefix handling, improve comments
1 parent 17d04ff commit 3b3a4db

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/Http/Controllers/ImageTransformerController.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,51 @@ class ImageTransformerController extends \Illuminate\Routing\Controller
2727
{
2828
use ManagesImageCache, ResolvesOptions;
2929

30-
public function transformWithPrefix(Request $request, string $pathPrefix, string $options, string $path)
30+
/**
31+
* Transform an image with a specified path prefix and custom options.
32+
*/
33+
public function transformWithPrefix(Request $request, string $pathPrefix, string $options, string $path): Response
3134
{
3235
return $this->handleTransform($request, $pathPrefix, $options, $path);
3336
}
3437

35-
public function transformDefault(Request $request, string $options, string $path)
38+
/**
39+
* Transform an image with the default path prefix and custom options.
40+
*/
41+
public function transformDefault(Request $request, string $options, string $path): Response
3642
{
3743
return $this->handleTransform($request, null, $options, $path);
3844
}
3945

40-
protected function handleTransform(Request $request, ?string $pathPrefix, string $options, ?string $path = null)
46+
/**
47+
* Handle the image transformation logic.
48+
*/
49+
protected function handleTransform(Request $request, ?string $pathPrefix, string $options, ?string $path = null): Response
4150
{
4251
$realPath = $this->handlePath($pathPrefix, $path);
4352

4453
$options = $this->parseOptions($options);
4554

46-
// Check cache
4755
if (config()->boolean('image-transform-url.cache.enabled')) {
4856
$cachePath = $this->getCachePath($pathPrefix, $path, $options);
4957

5058
if (File::exists($cachePath)) {
5159
if (Cache::has('image-transform-url:'.$cachePath)) {
52-
// serve file from storage
5360
return $this->imageResponse(
5461
imageContent: File::get($cachePath),
5562
mimeType: File::mimeType($cachePath),
5663
cacheHit: true
5764
);
5865
} else {
59-
// Cache expired, delete the cache file and continue
6066
File::delete($cachePath);
6167
}
6268
}
6369
}
6470

6571
if (
6672
config()->boolean('image-transform-url.rate_limit.enabled') &&
67-
! in_array(App::environment(), config()->array('image-transform-url.rate_limit.disabled_for_environments'))) {
73+
! in_array(App::environment(), config()->array('image-transform-url.rate_limit.disabled_for_environments'))
74+
) {
6875
$this->rateLimit($request, $path);
6976
}
7077

@@ -109,7 +116,6 @@ protected function handleTransform(Request $request, ?string $pathPrefix, string
109116

110117
}
111118

112-
// We use the mime type instead of the extension to determine the format, because this is more reliable.
113119
$originalMimetype = File::mimeType($realPath);
114120

115121
$format = $this->getStringOptionValue($options, 'format', $originalMimetype);
@@ -141,20 +147,21 @@ protected function handleTransform(Request $request, ?string $pathPrefix, string
141147

142148
/**
143149
* Handle the path and ensure it is valid.
150+
*
151+
* @param-out string $pathPrefix
144152
*/
145153
protected function handlePath(?string &$pathPrefix, ?string &$path): string
146154
{
147155
if ($path === null) {
148156
$path = $pathPrefix;
149-
$pathPrefix = null;
150157
}
151158

152-
$allowedSourceDirectories = config('image-transform-url.source_directories', []);
153-
154-
if (! $pathPrefix) {
155-
$pathPrefix = config('image-transform-url.default_source_directory') ?? array_key_first($allowedSourceDirectories);
159+
if (is_null($pathPrefix)) {
160+
$pathPrefix = config()->string('image-transform-url.default_source_directory', (string) array_key_first(config()->array('image-transform-url.source_directories')));
156161
}
157162

163+
$allowedSourceDirectories = config()->array('image-transform-url.source_directories', []);
164+
158165
abort_unless(array_key_exists($pathPrefix, $allowedSourceDirectories), 404);
159166

160167
$basePath = $allowedSourceDirectories[$pathPrefix];

0 commit comments

Comments
 (0)