@@ -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
0 commit comments