Skip to content

Commit 7b2ac54

Browse files
committed
fix: read error in ImageDispenser
1 parent 78539c5 commit 7b2ac54

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

src/Service/ImageDispenser.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Contracts\Filesystem\Factory as FilesystemManager;
88
use Illuminate\Contracts\Filesystem\FileNotFoundException;
99
use Illuminate\Contracts\Filesystem\Filesystem;
10-
use Illuminate\Http\Request;
1110
use Illuminate\Http\Response;
1211
use Intervention\Image\Encoders\AutoEncoder;
1312
use Intervention\Image\Interfaces\ImageInterface;
@@ -19,6 +18,7 @@
1918
use ReliqArts\GuidedImage\Contract\ImageDispenser as ImageDispenserContract;
2019
use ReliqArts\GuidedImage\Contract\ImageManager;
2120
use ReliqArts\GuidedImage\Demand\Dummy;
21+
use ReliqArts\GuidedImage\Demand\ExistingImage;
2222
use ReliqArts\GuidedImage\Demand\Resize;
2323
use ReliqArts\GuidedImage\Demand\Thumbnail;
2424
use RuntimeException;
@@ -108,7 +108,7 @@ public function getResizedImage(Resize $demand)
108108
if ($this->cacheDisk->exists($cacheFilePath)) {
109109
$image = $this->makeImageWithEncoding($this->cacheDisk->path($cacheFilePath));
110110
} else {
111-
$image = $this->makeImageWithEncoding($this->getImageFullUrl($guidedImage));
111+
$image = $this->makeImageWithEncoding($this->getImageFullPath($guidedImage));
112112
$sizingMethod = $demand->allowUpSizing() ? 'resize' : 'resizeDown';
113113
if ($demand->maintainAspectRatio()) {
114114
$sizingMethod = $demand->allowUpSizing() ? 'scale' : 'scaleDown';
@@ -125,7 +125,7 @@ public function getResizedImage(Resize $demand)
125125
return new Response(
126126
$this->cacheDisk->get($cacheFilePath),
127127
self::RESPONSE_HTTP_OK,
128-
$this->getImageHeaders($cacheFilePath, $demand->getRequest(), $image) ?: []
128+
$this->getImageHeaders($cacheFilePath, $demand, $image) ?: []
129129
);
130130
} catch (RuntimeException $exception) {
131131
return $this->handleRuntimeException($exception, $guidedImage);
@@ -187,7 +187,7 @@ public function getImageThumbnail(Thumbnail $demand)
187187
} else {
188188
/** @var ImageInterface $image */
189189
$image = $this->imageManager
190-
->read($this->getImageFullUrl($guidedImage))
190+
->read($this->getImageFullPath($guidedImage))
191191
->{$method}(
192192
$width,
193193
$height
@@ -203,7 +203,7 @@ public function getImageThumbnail(Thumbnail $demand)
203203
return new Response(
204204
$this->cacheDisk->get($cacheFilePath),
205205
self::RESPONSE_HTTP_OK,
206-
$this->getImageHeaders($cacheFilePath, $demand->getRequest(), $image) ?: []
206+
$this->getImageHeaders($cacheFilePath, $demand, $image) ?: []
207207
);
208208
} catch (RuntimeException $exception) {
209209
return $this->handleRuntimeException($exception, $guidedImage);
@@ -235,13 +235,15 @@ public function emptyCache(): bool
235235
*
236236
* @return array image headers
237237
*/
238-
private function getImageHeaders(string $cacheFilePath, Request $request, ImageInterface $image): array
238+
private function getImageHeaders(string $relativeCacheFilePath, ExistingImage $demand, ImageInterface $image): array
239239
{
240-
$filePath = $image->origin()->filePath();
241-
$lastModified = $this->cacheDisk->lastModified($cacheFilePath);
240+
$request = $demand->getRequest();
241+
$fullCacheFilePath = $this->cacheDisk->path($relativeCacheFilePath);
242+
$lastModified = $this->cacheDisk->lastModified($relativeCacheFilePath);
242243
$modifiedSince = $request->header('If-Modified-Since', '');
243244
$etagHeader = trim($request->header('If-None-Match', ''));
244-
$etagFile = $this->fileHelper->hashFile($filePath);
245+
$etagFile = $this->fileHelper->hashFile($fullCacheFilePath);
246+
$originalImageRelativePath = $demand->getGuidedImage()->getUrl(true);
245247

246248
// check if image hasn't changed
247249
if ($etagFile === $etagHeader || strtotime($modifiedSince) === $lastModified) {
@@ -256,7 +258,7 @@ private function getImageHeaders(string $cacheFilePath, Request $request, ImageI
256258
$this->getDefaultHeaders(),
257259
[
258260
'Content-Type' => $image->origin()->mediaType(),
259-
'Content-Disposition' => sprintf('inline; filename=%s', basename($image->origin()->filePath())),
261+
'Content-Disposition' => sprintf('inline; filename=%s', basename($originalImageRelativePath)),
260262
'Last-Modified' => date(DATE_RFC822, $lastModified),
261263
'Etag' => $etagFile,
262264
]
@@ -307,12 +309,9 @@ private function makeImageWithEncoding(mixed $data): ImageInterface
307309
return $this->imageManager->read($encodedImage->toFilePointer());
308310
}
309311

310-
/**
311-
* @throws RuntimeException
312-
*/
313-
private function getImageFullUrl(GuidedImage $guidedImage): string
312+
private function getImageFullPath(GuidedImage $guidedImage): string
314313
{
315-
return $this->uploadDisk->url($guidedImage->getUrl(true));
314+
return $this->uploadDisk->path($guidedImage->getUrl(true));
316315
}
317316

318317
/**
@@ -322,8 +321,8 @@ private function handleRuntimeException(
322321
RuntimeException $exception,
323322
GuidedImage $guidedImage
324323
): BinaryFileResponse {
325-
$errorMessage = 'Intervention image creation failed with NotReadableException;';
326-
$context = ['imageUrl' => $this->getImageFullUrl($guidedImage)];
324+
$errorMessage = 'Intervention image creation failed with RuntimeException;';
325+
$context = ['imageUrl' => $this->getImageFullPath($guidedImage)];
327326

328327
if (! $this->configProvider->isRawImageFallbackEnabled()) {
329328
$this->logger->error(

tests/Unit/Service/ImageDispenserTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final class ImageDispenserTest extends TestCase
6464

6565
private const IMAGE_NAME = 'my-image';
6666

67-
private const IMAGE_URL = '//image_url';
67+
private const IMAGE_PATH = './image_path';
6868

6969
private const IMAGE_WIDTH = 100;
7070

@@ -186,8 +186,8 @@ protected function setUp(): void
186186
->willReturn(self::LAST_MODIFIED);
187187

188188
$uploadDisk
189-
->url(self::IMAGE_URL)
190-
->willReturn(self::IMAGE_URL);
189+
->path(self::IMAGE_PATH)
190+
->willReturn(self::IMAGE_PATH);
191191

192192
$fileHelper
193193
->hashFile(Argument::type('string'))
@@ -202,7 +202,7 @@ protected function setUp(): void
202202
->willReturn(self::IMAGE_NAME);
203203
$this->guidedImage
204204
->getUrl(true)
205-
->willReturn(self::IMAGE_URL);
205+
->willReturn(self::IMAGE_PATH);
206206

207207
$this->subject = new ImageDispenser(
208208
$configProvider->reveal(),
@@ -278,7 +278,7 @@ public function testGetResizedImage(): void
278278
->willReturn(false);
279279
$this->cacheDisk
280280
->path($cacheFile)
281-
->shouldBeCalledTimes(1)
281+
->shouldBeCalledTimes(2)
282282
->willReturn($cacheFile);
283283
$this->cacheDisk
284284
->get($cacheFile)
@@ -289,7 +289,7 @@ public function testGetResizedImage(): void
289289
->read($cacheFile)
290290
->shouldNotBeCalled();
291291
$this->imageManager
292-
->read(Argument::in([self::IMAGE_URL, self::FOO_RESOURCE]))
292+
->read(Argument::in([self::IMAGE_PATH, self::FOO_RESOURCE]))
293293
->shouldBeCalledTimes(2)
294294
->willReturn($image);
295295

@@ -333,7 +333,7 @@ public function testGetResizedImageWhenImageInstanceIsExpected(): void
333333
->read($cacheFile)
334334
->shouldNotBeCalled();
335335
$this->imageManager
336-
->read(Argument::in([self::IMAGE_URL, self::FOO_RESOURCE]))
336+
->read(Argument::in([self::IMAGE_PATH, self::FOO_RESOURCE]))
337337
->shouldBeCalledTimes(2)
338338
->willReturn($image);
339339

@@ -358,7 +358,7 @@ public function testGetResizedImageWhenCacheFileExists(): void
358358
->willReturn(true);
359359
$this->cacheDisk
360360
->path($cacheFile)
361-
->shouldBeCalledTimes(1)
361+
->shouldBeCalledTimes(2)
362362
->willReturn($cacheFile);
363363
$this->cacheDisk
364364
->get($cacheFile)
@@ -370,7 +370,7 @@ public function testGetResizedImageWhenCacheFileExists(): void
370370
->shouldBeCalledTimes(2)
371371
->willReturn($image);
372372
$this->imageManager
373-
->read(self::IMAGE_URL)
373+
->read(self::IMAGE_PATH)
374374
->shouldNotBeCalled();
375375

376376
$result = $this->subject->getResizedImage(
@@ -420,14 +420,14 @@ public function testGetResizedWhenImageRetrievalFails(): void
420420
->read($cacheFile)
421421
->shouldNotBeCalled();
422422
$this->imageManager
423-
->read(Argument::in([self::IMAGE_URL, self::FOO_RESOURCE]))
423+
->read(Argument::in([self::IMAGE_PATH, self::FOO_RESOURCE]))
424424
->shouldBeCalledTimes(2)
425425
->willReturn($image);
426426

427427
$this->guidedImage
428428
->getUrl()
429429
->shouldBeCalledTimes(1)
430-
->willReturn(self::IMAGE_URL);
430+
->willReturn(self::IMAGE_PATH);
431431

432432
$this->logger
433433
->error(
@@ -476,14 +476,14 @@ public function testGetImageThumbnail(): void
476476
->willReturn($imageContent);
477477
$this->cacheDisk
478478
->path($cacheFile)
479-
->shouldBeCalledTimes(1)
479+
->shouldBeCalledTimes(2)
480480
->willReturn($cacheFile);
481481

482482
$this->imageManager
483483
->read($cacheFile)
484484
->shouldNotBeCalled();
485485
$this->imageManager
486-
->read(self::IMAGE_URL)
486+
->read(self::IMAGE_PATH)
487487
->shouldBeCalledTimes(1)
488488
->willReturn($image);
489489

@@ -527,7 +527,7 @@ public function testGetImageThumbnailWhenImageInstanceIsExpected(): void
527527
->read($cacheFile)
528528
->shouldNotBeCalled();
529529
$this->imageManager
530-
->read(Argument::in([self::IMAGE_URL, self::FOO_RESOURCE]))
530+
->read(Argument::in([self::IMAGE_PATH, self::FOO_RESOURCE]))
531531
->shouldBeCalledTimes(1)
532532
->willReturn($image);
533533

@@ -569,7 +569,7 @@ public function testGetImageThumbnailWhenCacheFileExists(): void
569569
->willReturn(true);
570570
$this->cacheDisk
571571
->path($cacheFile)
572-
->shouldBeCalledTimes(1)
572+
->shouldBeCalledTimes(2)
573573
->willReturn($cacheFile);
574574
$this->cacheDisk
575575
->get($cacheFile)
@@ -581,7 +581,7 @@ public function testGetImageThumbnailWhenCacheFileExists(): void
581581
->shouldBeCalledTimes(2)
582582
->willReturn($image);
583583
$this->imageManager
584-
->read(self::IMAGE_URL)
584+
->read(self::IMAGE_PATH)
585585
->shouldNotBeCalled();
586586

587587
$result = $this->subject->getImageThumbnail(
@@ -636,7 +636,7 @@ public function testGetImageThumbnailWhenDemandIsInvalid(): void
636636
->read($cacheFile)
637637
->shouldNotBeCalled();
638638
$this->imageManager
639-
->read(self::IMAGE_URL)
639+
->read(self::IMAGE_PATH)
640640
->shouldNotBeCalled();
641641

642642
$this->logger
@@ -691,7 +691,7 @@ public function testGetImageThumbnailWhenImageRetrievalFails(): void
691691
->read($cacheFile)
692692
->shouldNotBeCalled();
693693
$this->imageManager
694-
->read(self::IMAGE_URL)
694+
->read(self::IMAGE_PATH)
695695
->shouldBeCalledTimes(1)
696696
->willThrow(RuntimeException::class);
697697

0 commit comments

Comments
 (0)