From 5fb4353182a3e9b7e86e02180ef305388c99859d Mon Sep 17 00:00:00 2001 From: Miloslav Pavelka Date: Mon, 29 Aug 2016 19:15:14 +0200 Subject: [PATCH] Adding support for serving static files from content folder and its subfolders. --- lib/Pico.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/Pico.php b/lib/Pico.php index d88568a..7ebff2a 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -123,6 +123,14 @@ class Pico */ protected $requestFile; + /** + * Absolute path to the content file being served + * + * @see Pico::getRequestFile() + * @var boolean|false + */ + protected $requestFileIsStatic; + /** * Raw, not yet parsed contents to serve * @@ -300,6 +308,16 @@ class Pico // load raw file content $this->triggerEvent('onContentLoading', array(&$this->requestFile)); + if ($this->getRequestFileIsStatic()) { + // discover mime type + $mimeType = mime_content_type($this->requestFile); + $fp = fopen($this->requestFile, 'rb'); + header("Content-Type: ".$mimeType); + header("Content-Length: " . filesize($this->requestFile)); + fpassthru($fp); + exit; + } + $notFoundFile = '404' . $this->getConfig('content_ext'); if (file_exists($this->requestFile) && (basename($this->requestFile) !== $notFoundFile)) { $this->rawContent = $this->loadFileContent($this->requestFile); @@ -652,6 +670,16 @@ class Pico return; } } + + // check if non-content file already exists + if (file_exists($this->requestFile) + && pathinfo($this->requestFile, PATHINFO_EXTENSION) != $this->getConfig('content_ext')) + { + $this->requestFileIsStatic = true; + return; + } + + $this->requestFileIsStatic = false; $this->requestFile .= $this->getConfig('content_ext'); } } @@ -667,6 +695,17 @@ class Pico return $this->requestFile; } + /** + * Returns true if the requested file is a static file + * + * @see Pico::discoverRequestFile() + * @return string|null file path + */ + public function getRequestFileIsStatic() + { + return $this->requestFileIsStatic; + } + /** * Returns the raw contents of a file *