Skip to content

Commit be70d21

Browse files
committed
Converted thumbnail response to psr7. MediaRequestHandler is fully psr7 now.
1 parent 940473f commit be70d21

2 files changed

Lines changed: 46 additions & 29 deletions

File tree

src/Controllers/MediaRequestHandler.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,25 @@ public function respondWithMedia(Media $Media, $variant, $responseID, $responseD
342342
return $response;
343343
}
344344

345+
public function respondWithThumbnail(Media $Media, $variant, $responseID, $responseData = []): ResponseInterface
346+
{
347+
if ($this->responseBuilder != MediaBuilder::class) {
348+
throw new Exception('Media responses require MediaBuilder for putting together a response.');
349+
}
350+
$className = $this->responseBuilder;
351+
$responseBuilder = new $className($responseID, $responseData);
352+
353+
$responseBuilder->setContentType($Media->ThumbnailMIMEType);
354+
355+
$response = new MediaResponse($responseBuilder);
356+
$response = $this->setCache($response);
357+
358+
$response = $response->withHeader('ETag', "media-$Media->ID-$variant")
359+
->withHeader('Content-Length', filesize($responseID));
360+
361+
return $response;
362+
}
363+
345364
public function respondEmpty($responseID, $responseData = [])
346365
{
347366
if ($this->responseBuilder != EmptyBuilder::class) {
@@ -550,22 +569,6 @@ public function handleDeleteRequest(ActiveRecord $Record): ResponseInterface
550569

551570
public function handleThumbnailRequest(Media $Media = null): ResponseInterface
552571
{
553-
// send caching headers
554-
if (!headers_sent()) {
555-
// @codeCoverageIgnoreStart
556-
$expires = 60*60*24*365;
557-
header("Cache-Control: public, max-age=$expires");
558-
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time()+$expires));
559-
header('Pragma: public');
560-
// @codeCoverageIgnoreEnd
561-
}
562-
563-
// thumbnails are immutable for a given URL, so no need to actually check anything if the browser wants to revalidate its cache
564-
if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
565-
header('HTTP/1.0 304 Not Modified');
566-
exit();
567-
}
568-
569572
// get media
570573
if (!$Media) {
571574
if (!$mediaID = $this->shiftPath()) {
@@ -575,6 +578,17 @@ public function handleThumbnailRequest(Media $Media = null): ResponseInterface
575578
}
576579
}
577580

581+
$_server = $this->request->getServerParams();
582+
583+
// thumbnails are immutable for a given URL, so no need to actually check anything if the browser wants to revalidate its cache
584+
if (!empty($_server['HTTP_IF_NONE_MATCH']) || !empty($_server['HTTP_IF_MODIFIED_SINCE'])) {
585+
$this->responseBuilder = EmptyBuilder::class;
586+
$response = $this->respondEmpty($Media->ID);
587+
$response->withDefaults(304);
588+
589+
return $response;
590+
}
591+
578592
// get format
579593
if (preg_match('/^(\d+)x(\d+)(x([0-9A-F]{6})?)?$/i', $this->peekPath(), $matches)) {
580594
$this->shiftPath();
@@ -597,20 +611,13 @@ public function handleThumbnailRequest(Media $Media = null): ResponseInterface
597611
// get thumbnail media
598612
try {
599613
$thumbPath = $Media->getThumbnail($maxWidth, $maxHeight, $fillColor, $cropped);
614+
615+
$this->responseBuilder = MediaBuilder::class;
616+
$response = $this->respondWithThumbnail($Media, "$maxWidth-$maxHeight-$fillColor-$cropped", $thumbPath);
617+
return $response;
600618
} catch (Exception $e) {
601619
return $this->throwNotFoundError();
602620
}
603-
604-
// emit
605-
if (!headers_sent()) {
606-
// @codeCoverageIgnoreStart
607-
header("ETag: media-$Media->ID-$maxWidth-$maxHeight-$fillColor-$cropped");
608-
header("Content-Type: $Media->ThumbnailMIMEType");
609-
header('Content-Length: '.filesize($thumbPath));
610-
readfile($thumbPath);
611-
// @codeCoverageIgnoreEnd
612-
}
613-
exit();
614621
}
615622

616623

src/Models/Media/Media.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,18 @@ public function getThumbnail($maxWidth, $maxHeight, $fillColor = false, $cropped
311311
return $thumbPath;
312312
}
313313

314-
public function createThumbnailImage($thumbPath, $maxWidth, $maxHeight, $fillColor = false, $cropped = false)
314+
/**
315+
* Creates a thumbnail using the gd php extension.
316+
* Keeps aspect ratio.
317+
*
318+
* @param string $thumbPath
319+
* @param int $maxWidth
320+
* @param int $maxHeight
321+
* @param boolean|int $fillColor Background canvas color. See gd documentation.
322+
* @param boolean $cropped If cropped is enable the image will instead fill the smaller of width or height and cut the edges off.
323+
* @return void
324+
*/
325+
public function createThumbnailImage($thumbPath, $maxWidth, $maxHeight, $fillColor = false, $cropped = false): void
315326
{
316327
$thumbWidth = $maxWidth;
317328
$thumbHeight = $maxHeight;
@@ -430,7 +441,6 @@ public function createThumbnailImage($thumbPath, $maxWidth, $maxHeight, $fillCol
430441
}
431442

432443
chmod($thumbPath, static::$newFilePermissions);
433-
return true;
434444
}
435445

436446
// static methods

0 commit comments

Comments
 (0)