From f4ee6c8add45c1b85aa6260f826e493a7ab66f53 Mon Sep 17 00:00:00 2001 From: nelsonlaquet Date: Sun, 1 Sep 2013 14:28:29 -0500 Subject: [PATCH] Fixed caching --- app/controllers/TracksController.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/app/controllers/TracksController.php b/app/controllers/TracksController.php index 289f7f34..2863740a 100644 --- a/app/controllers/TracksController.php +++ b/app/controllers/TracksController.php @@ -75,14 +75,22 @@ ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $format['index']); $response = Response::make('', 200); + $filename = $track->getFileFor('MP3'); if (Config::get('app.sendfile')) { - $response->header('X-Sendfile', $track->getFileFor('MP3')); + $response->header('X-Sendfile', $filename); } else { - $response->header('X-Accel-Redirect', $track->getFileFor('MP3')); + $response->header('X-Accel-Redirect', $filename); } - $response->header('Content-Disposition', 'filename="' . $track->getFilenameFor('MP3') . '"'); + $time = gmdate(filemtime($filename)); + + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $time == $_SERVER['HTTP_IF_MODIFIED_SINCE']) { + header('HTTP/1.0 304 Not Modified'); + exit(); + } + + $response->header('Last-Modified', $time); $response->header('Content-Type', $format['mime_type']); return $response; @@ -110,15 +118,24 @@ ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $format['index']); $response = Response::make('', 200); + $filename = $track->getFileFor($format); if (Config::get('app.sendfile')) { - $response->header('X-Sendfile', $track->getFileFor('MP3')); + $response->header('X-Sendfile', $filename); $response->header('Content-Disposition', 'attachment; filename="' . $track->getDownloadFilenameFor($formatName) . '"'); } else { - $response->header('X-Accel-Redirect', $track->getFileFor('MP3')); + $response->header('X-Accel-Redirect', $filename); $response->header('Content-Disposition', 'attachment; filename=' . $track->getDownloadFilenameFor($formatName)); } + $time = gmdate(filemtime($filename)); + + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $time == $_SERVER['HTTP_IF_MODIFIED_SINCE']) { + header('HTTP/1.0 304 Not Modified'); + exit(); + } + + $response->header('Last-Modified', $time); $response->header('Content-Type', $format['mime_type']); return $response;